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. Connection Signal/Slot not working (with window "separation")
Qt 6.11 is out! See what's new in the release blog

Connection Signal/Slot not working (with window "separation")

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.9k Views 1 Watching
  • 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.
  • Juan de DironneJ Offline
    Juan de DironneJ Offline
    Juan de Dironne
    wrote on last edited by
    #1

    I had already created a small application under Qt but now I have to develop a slightly more complex application.
    An application with specific and distinct treatments accessible by a menu.
    So I tried to separate my treatments, my windows.
    It allows me not to have everything in my "MyMainWindow.h" and "MyMainWindow.cpp" files
    I don't know if the method I'm using is the right one but I'm passing a pointer from my Main Window to my Specific Window.

    In principle it works. But is this already the right solution...?

    And it works but when I want to create a Signal/Slot connection in a window, nothing happens.
    The slot is indeed found (otherwise I would get an error) but nothing happens.
    Example with the Signal/Slot connection from the "MyXWindow.cpp" page below:

    connect(buttonPage,SIGNAL(clicked(bool)),this,SLOT(doSpecificTreatment()));
    

    I am posting my (part of my) code below (and it is also available complete on gitHub)
    https://github.com/JuanDeDironne/qt_example_project

    main.cpp

    #include<QApplication>
    #include"MyMainWindow.h"
    
    int main(int nbArg, char* argList[])
    {
      QApplication myApp(nbArg,argList);
      MyMainWindow myWindow;
    
      myWindow.show();
      myApp.exec();
    }
    

    MyMainWindow.h

    #ifndef MYMAINWINDOW_H
    #define MYMAINWINDOW_H
    
    #include<QtWidgets>
    
    class MyMainWindow : public QMainWindow
    {
      Q_OBJECT
    
      public:
      MyMainWindow();
    
      public slots:
      void launchXWindow();
      void launchYWindow();
    
      private:
      QAction *m_actionQuit;
      QAction *m_actionX;
      QAction *m_actionY;
      QMenu *m_menuFile;
    };
    
    #endif // MYMAINWINDOW_H
    

    MyMainWindow.cpp

    #include"MyMainWindow.h"
    #include"MyXWindow.h"
    #include"MyYWindow.h"
    
    MyMainWindow::MyMainWindow()
    {
      // Initialization Window
      setWindowTitle("My Example Software");
      setMinimumHeight(500);
      setMinimumWidth(500);
    
      // Definition Actions
      m_actionQuit = new QAction(QIcon("./icones/icone_quit.png"),"Quit",this);
      m_actionX = new QAction(QIcon("./icones/icone_perso.png"),"X Window",this);
      m_actionY = new QAction(QIcon("./icones/icone_configuration.png"),"Y Window",this);
    
      // Définition Menu
      m_menuFile = menuBar()->addMenu("File");
      m_menuFile->addAction(m_actionX);
      m_menuFile->addAction(m_actionY);
      m_menuFile->addAction(m_actionQuit);
    
      // Creation Connections
      connect(m_actionQuit,SIGNAL(triggered(bool)),qApp,SLOT(quit()));
      connect(m_actionX,SIGNAL(triggered(bool)),this,SLOT(launchXWindow()));
      connect(m_actionY,SIGNAL(triggered(bool)),this,SLOT(launchYWindow()));
    }
    
    
    void MyMainWindow::launchXWindow()
    {
      MyXWindow currentWindow;
      currentWindow.displayWindow(this);
    }
    
    void MyMainWindow::launchYWindow()
    {
      MyYWindow currentWindow;
      currentWindow.displayWindow(this);
    }
    

    MyMainWindow.h

    #ifndef MYXWINDOW_H
    #define MYXWINDOW_H
    
    #include"MyMainWindow.h"
    
    class MyXWindow : public QWidget
    {
      Q_OBJECT
    
      public:
      MyXWindow();
      void displayWindow(MyMainWindow*);
    
      public slots:
      void doSpecificTreatment();
    
      private:
      QString myParameterA;
      QString myParameterB;
    };
    
    #endif // MYXWINDOW_H
    

    MyMainWindow.cpp

    #include"MyXWindow.h"
    
    
    MyXWindow::MyXWindow()
    {
    
    }
    
    void MyXWindow::displayWindow(MyMainWindow* windowFonc)
    {
      // Initialization
      QWidget *widgetPage;        widgetPage  = new QWidget();
      QGridLayout *layoutPage;    layoutPage  = new QGridLayout();          layoutPage->setAlignment(Qt::AlignCenter);
      QPushButton *buttonPage;    buttonPage  = new QPushButton("Submit");
    
      // Add Label and Button
      layoutPage->addWidget(new QLabel("Here Interface of <b>X</b> Window"));
      layoutPage->addWidget(buttonPage);
    
      // Association Layout to Widget and Placement
      widgetPage->setLayout(layoutPage);
      windowFonc->setCentralWidget(widgetPage);
    
      // Creation Connection - DOES NOT WORK :(
      connect(buttonPage,SIGNAL(clicked(bool)),this,SLOT(doSpecificTreatment()));
    }
    
    void MyXWindow::doSpecificTreatment()
    {
      qDebug() << "Here X Treatment";
    }
    
    JonBJ 1 Reply Last reply
    0
    • Juan de DironneJ Juan de Dironne

      I had already created a small application under Qt but now I have to develop a slightly more complex application.
      An application with specific and distinct treatments accessible by a menu.
      So I tried to separate my treatments, my windows.
      It allows me not to have everything in my "MyMainWindow.h" and "MyMainWindow.cpp" files
      I don't know if the method I'm using is the right one but I'm passing a pointer from my Main Window to my Specific Window.

      In principle it works. But is this already the right solution...?

      And it works but when I want to create a Signal/Slot connection in a window, nothing happens.
      The slot is indeed found (otherwise I would get an error) but nothing happens.
      Example with the Signal/Slot connection from the "MyXWindow.cpp" page below:

      connect(buttonPage,SIGNAL(clicked(bool)),this,SLOT(doSpecificTreatment()));
      

      I am posting my (part of my) code below (and it is also available complete on gitHub)
      https://github.com/JuanDeDironne/qt_example_project

      main.cpp

      #include<QApplication>
      #include"MyMainWindow.h"
      
      int main(int nbArg, char* argList[])
      {
        QApplication myApp(nbArg,argList);
        MyMainWindow myWindow;
      
        myWindow.show();
        myApp.exec();
      }
      

      MyMainWindow.h

      #ifndef MYMAINWINDOW_H
      #define MYMAINWINDOW_H
      
      #include<QtWidgets>
      
      class MyMainWindow : public QMainWindow
      {
        Q_OBJECT
      
        public:
        MyMainWindow();
      
        public slots:
        void launchXWindow();
        void launchYWindow();
      
        private:
        QAction *m_actionQuit;
        QAction *m_actionX;
        QAction *m_actionY;
        QMenu *m_menuFile;
      };
      
      #endif // MYMAINWINDOW_H
      

      MyMainWindow.cpp

      #include"MyMainWindow.h"
      #include"MyXWindow.h"
      #include"MyYWindow.h"
      
      MyMainWindow::MyMainWindow()
      {
        // Initialization Window
        setWindowTitle("My Example Software");
        setMinimumHeight(500);
        setMinimumWidth(500);
      
        // Definition Actions
        m_actionQuit = new QAction(QIcon("./icones/icone_quit.png"),"Quit",this);
        m_actionX = new QAction(QIcon("./icones/icone_perso.png"),"X Window",this);
        m_actionY = new QAction(QIcon("./icones/icone_configuration.png"),"Y Window",this);
      
        // Définition Menu
        m_menuFile = menuBar()->addMenu("File");
        m_menuFile->addAction(m_actionX);
        m_menuFile->addAction(m_actionY);
        m_menuFile->addAction(m_actionQuit);
      
        // Creation Connections
        connect(m_actionQuit,SIGNAL(triggered(bool)),qApp,SLOT(quit()));
        connect(m_actionX,SIGNAL(triggered(bool)),this,SLOT(launchXWindow()));
        connect(m_actionY,SIGNAL(triggered(bool)),this,SLOT(launchYWindow()));
      }
      
      
      void MyMainWindow::launchXWindow()
      {
        MyXWindow currentWindow;
        currentWindow.displayWindow(this);
      }
      
      void MyMainWindow::launchYWindow()
      {
        MyYWindow currentWindow;
        currentWindow.displayWindow(this);
      }
      

      MyMainWindow.h

      #ifndef MYXWINDOW_H
      #define MYXWINDOW_H
      
      #include"MyMainWindow.h"
      
      class MyXWindow : public QWidget
      {
        Q_OBJECT
      
        public:
        MyXWindow();
        void displayWindow(MyMainWindow*);
      
        public slots:
        void doSpecificTreatment();
      
        private:
        QString myParameterA;
        QString myParameterB;
      };
      
      #endif // MYXWINDOW_H
      

      MyMainWindow.cpp

      #include"MyXWindow.h"
      
      
      MyXWindow::MyXWindow()
      {
      
      }
      
      void MyXWindow::displayWindow(MyMainWindow* windowFonc)
      {
        // Initialization
        QWidget *widgetPage;        widgetPage  = new QWidget();
        QGridLayout *layoutPage;    layoutPage  = new QGridLayout();          layoutPage->setAlignment(Qt::AlignCenter);
        QPushButton *buttonPage;    buttonPage  = new QPushButton("Submit");
      
        // Add Label and Button
        layoutPage->addWidget(new QLabel("Here Interface of <b>X</b> Window"));
        layoutPage->addWidget(buttonPage);
      
        // Association Layout to Widget and Placement
        widgetPage->setLayout(layoutPage);
        windowFonc->setCentralWidget(widgetPage);
      
        // Creation Connection - DOES NOT WORK :(
        connect(buttonPage,SIGNAL(clicked(bool)),this,SLOT(doSpecificTreatment()));
      }
      
      void MyXWindow::doSpecificTreatment()
      {
        qDebug() << "Here X Treatment";
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Juan-de-Dironne said in Connection Signal/Slot not working (with window "separation"):

      void MyMainWindow::launchXWindow()
      {
        MyXWindow currentWindow;
        currentWindow.displayWindow(this);
      }
      
      void MyMainWindow::launchYWindow()
      {
        MyYWindow currentWindow;
        currentWindow.displayWindow(this);
      }
      

      Do you understand that those instances are immediately destroyed once displayWindow() returns?

      Juan de DironneJ 1 Reply Last reply
      3
      • JonBJ JonB

        @Juan-de-Dironne said in Connection Signal/Slot not working (with window "separation"):

        void MyMainWindow::launchXWindow()
        {
          MyXWindow currentWindow;
          currentWindow.displayWindow(this);
        }
        
        void MyMainWindow::launchYWindow()
        {
          MyYWindow currentWindow;
          currentWindow.displayWindow(this);
        }
        

        Do you understand that those instances are immediately destroyed once displayWindow() returns?

        Juan de DironneJ Offline
        Juan de DironneJ Offline
        Juan de Dironne
        wrote on last edited by
        #3

        @JonB My method to separate my different windows in H and CPP file is not the right one...?
        And your remark on the destruction of instances explains the fact that the connect() does not work...?

        The connection established between the signal and the slot cannot work because...?
        The connect() function is called on the "currentWindow" Widget of type "MyXWindow" but it no longer exists when I click on it...?
        Basically that's it...?

        jsulmJ JonBJ 2 Replies Last reply
        0
        • Juan de DironneJ Juan de Dironne

          @JonB My method to separate my different windows in H and CPP file is not the right one...?
          And your remark on the destruction of instances explains the fact that the connect() does not work...?

          The connection established between the signal and the slot cannot work because...?
          The connect() function is called on the "currentWindow" Widget of type "MyXWindow" but it no longer exists when I click on it...?
          Basically that's it...?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Juan-de-Dironne said in Connection Signal/Slot not working (with window "separation"):

          My method to separate my different windows in H and CPP file is not the right one...?

          @JonB did not write that.
          "And your remark on the destruction of instances explains the fact that the connect() does not work...?" - yes, because currentWindow is destroyed as soon as launchXWindow or launchYWindow terminate. This is basic C++ (read about variable scopes).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • Juan de DironneJ Juan de Dironne

            @JonB My method to separate my different windows in H and CPP file is not the right one...?
            And your remark on the destruction of instances explains the fact that the connect() does not work...?

            The connection established between the signal and the slot cannot work because...?
            The connect() function is called on the "currentWindow" Widget of type "MyXWindow" but it no longer exists when I click on it...?
            Basically that's it...?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Juan-de-Dironne
            Think about scope in C++.

            void MyMainWindow::launchYWindow()
            {
              MyYWindow currentWindow;
              currentWindow.displayWindow(this);
            }
            

            MyYWindow currentWindow is a local variable (on the stack) to this method. That means once the function exits any local variables are destroyed. displayWindow(MyMainWindow* windowFonc) seems to create some widgets which it places onto the passed-in MyMainWindow. It is odd to have a class/instance to do this, and to keep replacing the setCentralWidget(), but that probably "works". However it also goes

            // Creation Connection - DOES NOT WORK :(
            connect(buttonPage,SIGNAL(clicked(bool)),this,SLOT(doSpecificTreatment()));
            

            The slot, with doSpecificTreatment(), is placed on object this, which is the currentWindow in launchYWindow(). But as soon as displayWindow() exits so does launchYWindow(), taking MyYWindow currentWindow with it. When a slot (or signal) object goes out of scope/is destroyed Qt disconnects any slots or signals on that object. That looks like why your connect() shows no effect.

            In a word, you probably want to look at using a QStackedWidget for the central widget on your MyMainWindow if you want to change its content rather than altering its central widget. Your X/Y windows would be pages on the stacked widget, which shows just any one at a time. You will allocate them on the heap (i.e. new), just once, so they will persist even when not visible.

            1 Reply Last reply
            3
            • Juan de DironneJ Offline
              Juan de DironneJ Offline
              Juan de Dironne
              wrote on last edited by
              #6

              @JonB and @jsulm Thank you for your answers. It's really a C++ variable scoping issue actually.

              My method to separate windows or features into different files is not workable I feel. Are there any tutorials on this?

              Or the solution proposed by @JonB (which I thank myself again) is the solution for a complex application with different functionalities requiring different interfaces...?

              The use of "QStackedWidget" is necessary because we do not usually change the central widget to display different windows..?

              jsulmJ 1 Reply Last reply
              0
              • Juan de DironneJ Juan de Dironne

                @JonB and @jsulm Thank you for your answers. It's really a C++ variable scoping issue actually.

                My method to separate windows or features into different files is not workable I feel. Are there any tutorials on this?

                Or the solution proposed by @JonB (which I thank myself again) is the solution for a complex application with different functionalities requiring different interfaces...?

                The use of "QStackedWidget" is necessary because we do not usually change the central widget to display different windows..?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Juan-de-Dironne said in Connection Signal/Slot not working (with window "separation"):

                My method to separate windows or features into different files is not workable I feel

                Separating functionality and also windows/widgets in separate files is common approach and works. You just need to make sure your objects live as long as they are needed. To achieve this either allocate them on the heap or make them member variables.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                Juan de DironneJ 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @Juan-de-Dironne said in Connection Signal/Slot not working (with window "separation"):

                  My method to separate windows or features into different files is not workable I feel

                  Separating functionality and also windows/widgets in separate files is common approach and works. You just need to make sure your objects live as long as they are needed. To achieve this either allocate them on the heap or make them member variables.

                  Juan de DironneJ Offline
                  Juan de DironneJ Offline
                  Juan de Dironne
                  wrote on last edited by
                  #8

                  @jsulm "Separating functionality and also windows/widgets in separate files is common approach and works"
                  But not using the principle that I implemented with an "Annex Window" which only changes the central Widget of the Main Window.
                  This solution requires interactions between classes that seem difficult to achieve.
                  Are there any tutorials on this?

                  jsulmJ 1 Reply Last reply
                  0
                  • Juan de DironneJ Juan de Dironne

                    @jsulm "Separating functionality and also windows/widgets in separate files is common approach and works"
                    But not using the principle that I implemented with an "Annex Window" which only changes the central Widget of the Main Window.
                    This solution requires interactions between classes that seem difficult to achieve.
                    Are there any tutorials on this?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Juan-de-Dironne said in Connection Signal/Slot not working (with window "separation"):

                    This solution requires interactions between classes that seem difficult to achieve

                    Why difficult? Wha texactly is difficult?
                    In Qt you can also use signals/slots to communicate between different classes.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    Juan de DironneJ 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @Juan-de-Dironne said in Connection Signal/Slot not working (with window "separation"):

                      This solution requires interactions between classes that seem difficult to achieve

                      Why difficult? Wha texactly is difficult?
                      In Qt you can also use signals/slots to communicate between different classes.

                      Juan de DironneJ Offline
                      Juan de DironneJ Offline
                      Juan de Dironne
                      wrote on last edited by
                      #10

                      @jsulm I can keep my method but I have to change the code of my "launchYWindow" function if I want it to work. With the following code :

                      The Code Before

                      void MyMainWindow::launchYWindow()
                      {
                        MyYWindow currentWindow;
                        currentWindow.displayWindow(this);
                      }
                      

                      The Code After

                      void MyMainWindow::launchXWindow()
                      {
                        MyXWindow *currentWindow; currentWindow = new MyXWindow();
                        currentWindow->displayWindow(this);
                      }
                      

                      This code works. I allocate my Widget on the heap as you said above.
                      But is it better to do that or use the "stackedWidget"...?

                      JonBJ 1 Reply Last reply
                      0
                      • Juan de DironneJ Juan de Dironne

                        @jsulm I can keep my method but I have to change the code of my "launchYWindow" function if I want it to work. With the following code :

                        The Code Before

                        void MyMainWindow::launchYWindow()
                        {
                          MyYWindow currentWindow;
                          currentWindow.displayWindow(this);
                        }
                        

                        The Code After

                        void MyMainWindow::launchXWindow()
                        {
                          MyXWindow *currentWindow; currentWindow = new MyXWindow();
                          currentWindow->displayWindow(this);
                        }
                        

                        This code works. I allocate my Widget on the heap as you said above.
                        But is it better to do that or use the "stackedWidget"...?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @Juan-de-Dironne
                        Stacked widget, for several reasons. Including that you are leaking a widget every time you do it your way.

                        1 Reply Last reply
                        3
                        • Juan de DironneJ Offline
                          Juan de DironneJ Offline
                          Juan de Dironne
                          wrote on last edited by
                          #12

                          Thank you very much for your answers and your time spent.

                          1 Reply Last reply
                          0
                          • Juan de DironneJ Juan de Dironne referenced this topic on

                          • Login

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