Slots and signals from different classes
-
Hi,
A slot is a function. When a signal is emitted, Qt calls the functions that are connected to that signal.
When you make a signal-slot connection, you also give Qt 2 pointers:
- A pointer to the object that emits the signal (called the "sender")
- A pointer to the object that should run its member function when the signal is emitted (called the "receiver")
Example:
@
// Class header
class MyObject : public QObject {
...signals:
void mySignal();public slots:
void mySlot();
}
@@
// Logic
MyObject *obj1 = new MyObject();
MyObject *obj2 = new MyObject();connect(obj1, SIGNAL(mySignal()),
obj2, SLOT(mySlot()));
@Every time obj1 emits mySignal(), Qt calls obj2->mySlot(). However, nothing will happen if obj2 emits mySignal(), because that signal is not connected to any slots.
-
I understand the basic concept of slots and signals, but I do not understand how to connect a signal and slot that are in different classes
-
[quote author="nicky j" date="1390198966"]I understand the basic concept of slots and signals, but I do not understand how to connect a signal and slot that are in different classes[/quote]Pass the object pointers, signal name, and slot name to connect().
@
connect(pointerToSender, SIGNAL(signalName()),
pointerToReceiver, SLOT(slotName()));
@ -
I tried that with the code i posted above, but it does not work. I am trying to send a signal from sideBar_appView.cpp and have it call a slot in mainView.cpp. What am I doing wrong?
-
mainView_object_pointer is a pointer to a QObject. QObject does not have a slot called newAppTab(). (When you run your program in Qt Creator, you will get a warning message that gives you this hint -- open the "Application Output" tab at the bottom).
Solution: You need to use a pointer to a mainView.
P.S. It is also a better design to make the connection in the parent, not in the child. The child is not supposed to know about the parent.
-
How do I set up the connection in the parent, not in the child? On that same note, how do I determine who is the parent and who is the child?
-
[quote author="nicky j" date="1390258400"]How do I set up the connection in the parent, not in the child?[/quote]Note: I only suggested this as a "best practice". You don't strictly have to do this; your code will work once you change the type of 'mainView_object_pointer' and assign it.
Anyway, call connect() in the parent's constructor instead of the child's constructor. However, since the parent doesn't have direct access to the child's button, the child will need to emit a signal when the button is clicked:
@
// sidebar_appview.cpp
connect(viewAllAppsButton, SIGNAL(clicked()), this. SIGNAL(newAppTabRequested()));
@You can now connect sidebar_appView->newTabRequested() to a slot, and that slot will be called when viewAllAppsButton is clicked.
[quote]On that same note, how do I determine who is the parent and who is the child? [/quote]Major hint: What parameters do your widget constructors take?
-
So than how can I create a parent that has multiple children?
Example code would be nice! -
How can I establish a clear hierarchy between several classes and have signals call slots in other classes. the mainView_object_pointer works, but I have no idea why or how.
-
[quote]On that same note, how do I determine who is the parent and who is the child?
...
How can I establish a clear hierarchy between several classes[/quote]This is a conceptual thing. Drawing class diagrams can help you design your hierarchy.
Basically, the children should be contained inside the parent. E.g. if you have a MainWindow that contains a few buttons, the MainWindow should be the parent of the buttons.
[quote]So than how can I create a parent that has multiple children?
Example code would be nice![/quote]You set the parent through the constructor. That's what I was trying to hint at before: The last parameter for all QObject constructors is called "parent" (this shows up in the documentation, and in your IDE's auto-complete feature).
@
QWidget* w1 = new QWidget();
QWidget* w2= new QWidget(w1);
QWidget* w3 = new QWidget(w1);
@
Here, w1 has no parent. w2 and w3 have w1 as their parent.Alternatively, you can use the "setParent()":http://qt-project.org/doc/qt-5/qobject.html#setParent function.
[quote]How can I... have signals call slots in other classes. the mainView_object_pointer works, but I have no idea why or how.[/quote]I'd like to better understand what you know and what you don't know. Could you please describe, in your own words: How do signals and slots work within the same class? (let's forget the case of multiple classes for now)
-
Ok that was really helpful, thanks!
How I think slots & signals work:
when a certain action is performed (e.g. triggered, clicked), a SIGNAL is emitted. This signal corresponds to a certain SLOT. I believe a slot is similar to a method/function. Therefore, when an action takes place, a signal is emitted and the code in the corresponding slot is executed. I know my understanding is pretty simplistic and/or flawed. In the connect() function, you must declare: the thing emitting the signal, the type of signal, where the slot is located, and the name of the slot.Please correct me if I'm wrong.
-
You're welcome. :)
[quote]when a certain action is performed (e.g. triggered, clicked), a SIGNAL is emitted.[/quote]Yes, that is one of the major uses of signals+slots: to allow functions to be called easily when the user clicks the mouse, presses a key, etc.
Signals+slots can do a lot more than that still. You can emit signals whenever you want. You can signal when you finish processing something, when you encounter an error, when a timer expires, etc.
Heck, you can even make an object emit signals at random times for fun.
[quote]This signal corresponds to a certain SLOT. I believe a slot is similar to a method/function. Therefore, when an action takes place, a signal is emitted and the code in the corresponding slot is executed.[/quote]You've gotten the gist of it correct.
Slots ARE functions. Any function in your program can be turned into a slot. After you connect a signal to a function, that function will be called each time the signal is emitted.
However, the relationship doesn't need to be one-to-one. A signal can be connected to multiple slots: When the signal is emitted, all those functions will be called one by one. A slot can also be connected to multiple signals: If any of the signals are emitted, the slot (function) will be called. You can see a diagram of these multi-connections "here":http://qt-project.org/doc/qt-5/signalsandslots.html.
[quote]In the connect() function, you must declare: the thing emitting the signal, the type of signal, where the slot is located, and the name of the slot.[/quote]Correct. The connection function is:
@
connect(thingEmittingTheSignal, SIGNAL(typeOfSignal()),
whereTheSlotIsLocated, SLOT(nameOfSlot()));
@With this "formula" it is easy to connect signals to slots in the same class, or across different classes.
@
QPushButton* b1 = new QPushButton();
QPushButton* b2 = new QPushButton();b1->show(); // Show b1 only, keep b2 hidden
// Connect different objects
connect(b1, SIGNAL(clicked()),
b2, SLOT(show()));// Connect the same object
connect(b2, SIGNAL(clicked()),
b2, SLOT(hide()));
@Notice that QPushButton::show() is a slot, but can also be called directly as b1->show().
When b1 is clicked, b2->show() is called.
When b2 is clicked, b2->hide() is called.Note: 'this' is an object pointer, just like 'b1' and 'b2' in this example.
Has your original question been answered yet?
-
Yes my question has been mostly answered. I understand slots and signals much better now, thanks! I am still having trouble getting my signal to call a slot in a different class. I have two classes: mainView and its child browseTab. I need a signal emitted from browseTab to call a slot in mainView. I created a pointer called @mainView *mainView_object_pointer@
When I place this in the connect() function it does not work. however if I make it
@QObject *mainView_object_pointer@
it works fine. Why do I have to use QObject in the declaration instead of mainView? -
[quote author="nicky j" date="1391125534"]Why do I have to use QObject in the declaration instead of mainView?[/quote]This is not related to signals and slots at all.
“error: cannot initialize a member subobject of type ‘mainView *’ with an rvalue of type ‘QObject *’"
This is basic C++: You cannot assign a base object pointer to a variable whose type is a derived object pointer (mainView*).
@
class Base {...};
class Derived : public Base {...};...
Base* var1 = new Derived() // OK
Derived* var2 = new Base(); // Error
@Your constructors were trying to assign the value of parent (which is a QObject*) into mainView_object_pointer.