Emitting a singal from QChartViews mouseMoveEvent - c++

I am trying to pass the variable "event" from QChartView::mouseMoveEvent(QMouseEvent* event) to my MainWindow, so I can use it in my MainWindow. Since I am new to Qt and dont know exactly how to do it, I tried to subclass QChartView and override the function mouseMoveEvent, but I can only call static functions from this method, which are in my MainWindow class, but I need to call a non static function, so I can access variables and functions in my MainWindow class.
Is it possible to solve this problem with signals? My first attempt was this:
connect(this, ChartView::mouseMoveEvent(QMouseEvent* event), ui->chartView, SLOT(showInformation(QMouseEvent*)));
But it didnt work. Is there a way to emit a signal which executes a function in my MainWindow every time when ChartView::mouseMoveEvent(QMouseEvent* event) is called?

Related

connecting QGraphicsview signal to UI slot

I am new to QT and C++ and i have legacy qt-c++ code here which i cant get to work.
Probably its something about the lifetime of the calling objects but hey, but please tell me what i am missing.
In a QT .ui i have various Frames and Widgets containing Frames and Widgets containing a QVBoxLayout which we shall call "myLayout"
On click in the .ui file i use
myWidget = new mywidget(some params);
myLayout->addWidget(myWidget);
where myWidget is declared as mywidget *myWidget; in the header file
myWidget is a QWidget which internally adds a QVBoxLayout to itself and adds a QGraphicsView. Using the MouseReleaseEvent i emit a signal from the QWidget.
now when i try to connect the signal slot (which i do in cpp file from the ui)
connect(myWidget, SIGNAL(mySignal(QString)), this, SLOT(mySlot(QString)));
the signals never catch the slot. the slot is public, the signal isnt.
What did i do wrong? Can somebody help. Feel free to ask further questions since i dont really know whats important in c++ questions;)
edit:
the signal gets emitted by QGraphicsObjects which themselfs connect to a slot in the QGraphicsView. This Slot is called and debuggable. at the end of the Routine an emit mySignal("..."); is called.
Maybe you forgot to add Q_OBJECT macro in your widget declaration. Qt documentation: http://doc.qt.io/qt-5/qobject.html#Q_OBJECT

How to externally call a signal in Qt?

I know how to call a signal from inside the class where the signal is located: by using emit. But what if I want to call it externally, from the parent object?
The reason why I want to do is is because a given QPushButton is connected to a slot which picks the button that called it by using sender(). Now I want the same functionallity of that slot to be called but not after a manual click in the button, but from within the code itself. Normally I would just programatically call for the slot, but because of the usage of sender(), that way is invalid: calling the slot directly will not give it the id of the button.
So how could I "externally" ask for the object for it to emit one of its signals? That is, how can I make my QPushButton emit its clicked() signal from within the code, not by clicking the button with the mouse?
You cannot emit signal directly from the parent object.
Only the class that defines a signal and its subclasses can emit the signal. (http://qt-project.org/doc/qt-4.8/signalsandslots.html#signals)
You can define a method in your QPushButton emitClicked() where you emit the signal. Then you call emitClicked() on instance of your QPushButton from the code.
The Qt headers contain this interesting line (in qobjectdefs.h):
#define emit
Which means that presence or absence of the emit keyword has no effect on the code, it's only there to make it more obvious to the human reader that you are emitting a signal. That is to say:
emit clicked();
is exactly the same (as far as the C++ compiler is concerned) as
clicked();
Given that, if you want your button to emit its clicked() signal, it's just a matter of doing this:
myButton->clicked();
(although if you wanted to be clever about it, this would work equally well):
emit myButton->clicked();
Seems that Qt Test module is just for this case.
Check out this tutorial on simulating GUI events.
Basically you use QTest::​mouseClick and pass pointer to your push button.

Qt add function call to event loop from other thread

I've stumbled across a problem I can't solve on an elegant way right now.
The situation: I have a callback function which is called from outside my application. The callback function has to update some gui object.. Since I can't call (for example) repaint() from within another thread, I have to finde some way to add a function call to the main event loop so the task gets executed at some time.
One possible way would be to use this:
QMetaObject::invokeMethod(object, "functionName", Qt::QueuedConnection, Q_ARG(float, value));
However, this just gives me the response that no such Method "Object::functionName". (which is obviously a lie!)
I've also read about connecting a signal to a slot which will be called from the event loop by setting the connection type to Qt::QueuedConnection. However, using QOjbect.:connect() won't work since I don't knwo which object the signal needs to get. Nice would be something like
QObject::emit(object, SIGNAL(function(flaot)), arg);
QMetaObject::invokeMethod is usually what you should use in this kind of situation. Make sure that:
object is a QObject subclass with the Q_OBJECT macro at the top
functionName is either declared in the slots section or has the Q_INVOKABLE macro

How to simulate a QTreeWidget itemClicked signal without making a derived class?

I am unable to find a proper simulation for ItemClicked() SIGNAL for QTreeWidget.
Is there a way to simulate it so that ItemClicked Signal is generated ?
e.g: we can emit ItemClicked in a derived class of QTreeWidget but cannot (as a QT rule) outside of it.
You can't use the emit call for class A to emit class B's signals. But note that the documentation for signals and slots says:
"You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)"
So you can work around this by declaring a signal in class A of the same signature as the one you want class B to emit, and connecting the signals together:
connect(
myclass, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
treewidget, SIGNAL(itemClicked(QTreeWidgetItem*, int))
);
Then emit itemClicked from myclass. If I'm not mistaken, it will work for this case...and fire the treewidget's itemClicked signal for you.

How to access to parent widget on qt?

I have an inherited QTreeWidget (called PackList) class and its parent is a KXmlGuiWindow.
How can I access to the parent's slots?
I've tried getParent()->mySlot() from the QTreeWidget class but I've got
error: no matching function for call to 'PackList::mySlot()'
Does anybody know the correct way? Thanks
If you know the parent's class, you will have to cast parentWidget() to that class and then call your slot. Keep in mind whether or not it's a slot makes no difference in this case. You are just calling a method.
((KXmlGuiWindow*)parentWidget())->mySlot();
You can make the call without casting by wiring up your signal to the slot.
connect( this, SIGNAL(mySignal()), parentWidget(), SLOT(mySlot()) );
Lastly, you can use QMetaObject::invokeMethod to call it if you don't want to cast it. That's probably overkill.
I'm not sure I fully understand your question.
However, you can access the parent widget of a widget with parentWidget().
Then, you should be able to call any public slot :
parentWidget()->a_slot();