Calling slots between classes where one class is not the parent
-
Hello,
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! -
Hi,
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");
connect(button, SIGNAL(clicked()),
this, SIGNAL(buttonClicked()));
}// TabWidget constructor
{
this->browseTab = new BrowseTab(this);
connect(browseTab, SIGNAL(buttonclicked()),
this, SIGNAL(browseTabButtonClicked()));
}// MainWidget constructor
{
this->tabWidget = new TabWidget(this);
connect(tabWidget, SIGNAL(browseTabButtonclicked()),
this, SLOT(doStuff()));
}
@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.