Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Signal/Slot connection that spans 3 classes
QtWS25 Last Chance

[Solved] Signal/Slot connection that spans 3 classes

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Endless
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      d2uriel
      wrote on last edited by
      #2

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

      No idea if it will work :P.

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #3

        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()));
        

        ...
        }
        @

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved