Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Calling slots between classes where one class is not the parent
nicky j last edited by
I am wondering how I can call a slot from a different class when the class with the slot is not necessarily the parent. I have a bunch of classes that have parents in this order:
mainWindow-->mainWidget-->tabWidget-->browseTab. basically mainWindow is the parent of mainWidget which is the parent of tabWidget, etc. How can I have a button in browseTab, that, when pressed, calls a slot in mainWindow. I know how to make it call a signal in its parent class (tabWidget), but I am stuck on how to make it call a slot in its 'grandparent' or 'great grandparent' classes. Thanks!
There are a few different ways you can do this:
1) Chain signals together:
This example shows how to propagate a signal
from a BrowseTab button to a MainWidget.
It follows Qt naming conventions: Class names start with an uppercase letter, variable names start with a lowercase letter.
// BrowseTab constructor
this->button = new QPushButton("Special Button");
// TabWidget constructor
this->browseTab = new BrowseTab(this);
// MainWidget constructor
this->tabWidget = new TabWidget(this);
2) Find a way to pass the pointer of the button up to the MainWidget. Then, call QObject::connect() in the MainWidget to connect the button's signal to the MainWidget's slot.