March 29, 2012

Endless Endless
Lab Rat
85 posts

[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:

  1. RootWindow::RootWindow(QWidget *parent) :
  2.     QMainWindow(parent)
  3. {
  4.     showFullScreen();
  5.     myStackedWidget = new QStackedWidget(this);
  6.     monitorScreen = new MonitorWindow();
  7.     myStackedWidget->addWidget(monitorScreen);
  8.     connect(monitorScreen, SIGNAL(activateScreen(int)), this, SLOT(setCurrentScreen(int)));
  9. }

In the monitorwindow.cpp file:
  1. MonitorWindow::MonitorWindow(QWidget *parent) :
  2.     QWidget(parent),
  3.     ui(new Ui::MonitorWindow)
  4. {
  5.     ui->setupUi(this);
  6.  
  7.     // Instantiate status bar and connect it
  8.     statusBar = new StatusBar(this);
  9.     connect(this, SIGNAL(statusChanged(int)), statusBar, SLOT(updateStatusSlot(int)));
  10.     connect(this, SIGNAL(faultStatusChanged(int)), statusBar, SLOT(updateFaultSlot(int)));
  11.     connect(statusBar, SIGNAL(showSiteMap()), this, SLOT(sendSiteMapRequest()));
  12. }

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?

3 replies

March 29, 2012

d2uriel d2uriel
Lab Rat
68 posts

Maybe something like:

  1. connect(this->statusBar, SIGNAL(something()), /*maybe a cast here*/this->parent(), SLOT(somethingElse()));

?

No idea if it will work :P.

 Signature 

“Do not judge, and you will never be mistaken.” - Jean-Jacques Rousseau

March 29, 2012

mlong mlong
Robot Herder
1537 posts

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:

  1. class MonitorWindow ...
  2. {
  3. ...
  4. signal:
  5.     void showSiteMap();
  6. ...
  7. }

And in the c’tor do:

  1. MonitorWindow::MonitorWindow(...)
  2. {
  3. ...
  4.    statusBar = new StatusBar(...);
  5.    
  6.    connect(statusBar, SIGNAL(showSiteMap()), this, SIGNAL(showSiteMap()));
  7. ...
  8. }

Then in your root window you can connect to MonitorWindow’s “showSiteMap()” signal.

  1. RootWindow::RootWindow(QWidget *parent) :
  2.     QMainWindow(parent)
  3. {
  4. ...
  5.     monitorScreen = new MonitorWindow();
  6.    
  7.     connect(monitorScreen, SIGNAL(showSiteMap()), this, SLOT(doWhatever()));
  8. ...
  9. }

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

March 30, 2012

Andre Andre
Robot Herder
6422 posts

Note that the SIGNAL/SIGNAL connection in the example of mlong above is not a mistake. You can connect signals together.

 
  ‹‹ Impact of Qt on ICU ?      Make QTableView rows expandable and collapsable ››

You must log in to post a reply. Not a member yet? Register here!