[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:
    @
    RootWindow::RootWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    showFullScreen();
    myStackedWidget = new QStackedWidget(this);
    monitorScreen = new MonitorWindow();
    myStackedWidget->addWidget(monitorScreen);
    connect(monitorScreen, SIGNAL(activateScreen(int)), this, SLOT(setCurrentScreen(int)));
    }
    @
    In the monitorwindow.cpp file:
    @
    MonitorWindow::MonitorWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MonitorWindow)
    {
    ui->setupUi(this);

    // 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?



  • Maybe something like:
    @connect(this->statusBar, SIGNAL(something()), /maybe a cast here/this->parent(), SLOT(somethingElse()));@
    ?

    No idea if it will work :P.


  • Moderators

    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 ...
    {
    ...
    signal:
    void showSiteMap();
    ...
    }
    @

    And in the c'tor do:
    @
    MonitorWindow::MonitorWindow(...)
    {
    ...
    statusBar = new StatusBar(...);

    connect(statusBar, SIGNAL(showSiteMap()), this, SIGNAL(showSiteMap()));
    ...
    }
    @

    Then in your root window you can connect to MonitorWindow's "showSiteMap()" signal.

    @
    RootWindow::RootWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    ...
    monitorScreen = new MonitorWindow();

    connect(monitorScreen, SIGNAL(showSiteMap()), this, SLOT(doWhatever()));
    

    ...
    }
    @



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.