[Solved] Signal/Slot connection that spans 3 classes
I have the RootWindow class which is a QMainWindow. I then instantiate the MonitorWindow class, a QWidget, from RootWindow. Next, in the constructor of MonitorWindow, I instantiate another class called StatusBar, which shows a status bar on the MonitorWindow QWidget. Here are the code snippets that do the above:
In the rootwindow.cpp file:
In the monitorwindow.cpp file:
- MonitorWindow::MonitorWindow(QWidget *parent) :
- ui(new Ui::MonitorWindow)
- // Instantiate status bar and connect it
- statusBar = new StatusBar(this);
- connect(this, SIGNAL(statusChanged(int)), statusBar, SLOT(updateStatusSlot(int)));
- connect(this, SIGNAL(faultStatusChanged(int)), statusBar, SLOT(updateFaultSlot(int)));
- connect(statusBar, SIGNAL(showSiteMap()), this, SLOT(sendSiteMapRequest()));
What I’d like to do is add a fourth connection to the three listed immediately above that connects a signal from StatusBar to a slot in RootWindow. Since RootWindow is two levels above StatusBar, how would I go about doing that?
Create a duplicate signal in your MonitorWindow class and chain them together. For instance, if you wanted to expose the statusbar’s showSiteMap signal, you could do:
- class MonitorWindow ...
- void showSiteMap();
And in the c’tor do:
- statusBar = new StatusBar(...);
- connect(statusBar, SIGNAL(showSiteMap()), this, SIGNAL(showSiteMap()));
Then in your root window you can connect to MonitorWindow’s “showSiteMap()” signal.