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. Connecting two windows
Qt 6.11 is out! See what's new in the release blog

Connecting two windows

Scheduled Pinned Locked Moved Solved General and Desktop
40 Posts 7 Posters 10.9k Views 4 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.
  • R Offline
    R Offline
    russjohn834
    wrote on last edited by
    #22

    @Pablo-J-Rogina Hi,

    There are two forms.
    MainWindow and StageOne (Both inherits QMainWindow).

    MainWindow has a signal patientIdChanged(QString), which is emitted upon clicking a button, I did this:

    void MainWindow::on_pushButton_Open_clicked()
    {
    
        QModelIndexList selection=ui->tableWidget->selectionModel()->selectedRows(0);
    
        this->hide();
        stageone = new StageOne(this);
        stageone->show();
        
      emit patientIdChanged(selection[0].data().toString());
        
        
    }
    

    And in StageOne I have a QLabel, its setText should get signal (Qstring) value from MainwWndow and show. That's the requirement.

    I tried to do this in StageOne:

     MainWindow *mainWindow= new MainWindow(this);
    connect(mainWindow, &MainWindow::patientIdChanged, ui->label, &QLabel::setText);
    
    

    But this is not working

    Pablo J. RoginaP 1 Reply Last reply
    0
    • R russjohn834

      @Pablo-J-Rogina Hi,

      There are two forms.
      MainWindow and StageOne (Both inherits QMainWindow).

      MainWindow has a signal patientIdChanged(QString), which is emitted upon clicking a button, I did this:

      void MainWindow::on_pushButton_Open_clicked()
      {
      
          QModelIndexList selection=ui->tableWidget->selectionModel()->selectedRows(0);
      
          this->hide();
          stageone = new StageOne(this);
          stageone->show();
          
        emit patientIdChanged(selection[0].data().toString());
          
          
      }
      

      And in StageOne I have a QLabel, its setText should get signal (Qstring) value from MainwWndow and show. That's the requirement.

      I tried to do this in StageOne:

       MainWindow *mainWindow= new MainWindow(this);
      connect(mainWindow, &MainWindow::patientIdChanged, ui->label, &QLabel::setText);
      
      

      But this is not working

      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by Pablo J. Rogina
      #23

      @russjohn834 said in Connecting two windows:

      Ok, it looks like you're overcomplicating things here.

      So you want a string (some selection in MainWindow widget) passed to StageOne window, right?
      What about having the StageOne constructor receiving such string? Pseudo-code:

      void MainWindow::on_pushButton_Open_clicked()
      {
           QModelIndexList selection=ui->tableWidget->selectionModel()->selectedRows(0);
       
           this->hide();
           stageone = new StageOne(selection[0].data().toString(), this);
           stageone->show();
      ...
      

      and obviusly StageOne class need a change in constructor:

      StageOne::StageOne(Qstring someString, QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::StageOne)
      {
      ...
      

      I don't see the need to deal with signals here

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • R Offline
        R Offline
        russjohn834
        wrote on last edited by
        #24

        @Pablo-J-Rogina Thanks a lot that's exactly I was trying for.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          russjohn834
          wrote on last edited by
          #25

          Thank you @JonB, @jsulm , @J-Hilk and @Pablo-J-Rogina for your feedback

          jsulmJ 1 Reply Last reply
          0
          • J Offline
            J Offline
            jhx76
            wrote on last edited by
            #26

            Hi everybody.

            Let's have a look to the code @russjohn834 gave us :

            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                MainWindow(QWidget *parent = nullptr);
                ~MainWindow();
                StageTwoNew *stagetwonew;
                StageOne *stageone; 
            
            signals:
            
                void patientID(QString);  ----------------> signal declaration
            
            
            public slots:
                void on_pushButton_New_clicked();
            
                void on_pushButton_Open_clicked();
            
                void parseDataEntry(const QString dataPath);
            
            private:
                Ui::MainWindow *ui;
            
            };
            #endif // MAINWINDOW_H
            

            Here we can see that the MainWindow owns an object of the class StageOne.
            So we can assume that the StageOne instance is built with specifying the parent as the MainWindow, certainly with code that looks like this :

            MainWindow::MainWindow() : QMainWindow() 
                : stageone(new StageOne(this))
            {
            }
            

            If i am right, the parent of the stageone object is the MainWindow from which the signal patientID is emitted.

            You can then write things like this :

            StageOne::StageOne(QWidget *parent) :
                QMainWindow(parent), // HERE the pointed parent is the MainWindow object
                ui(new Ui::StageOne)
            {
                ui->setupUi(this);
                
                // We connect the MainWindow::patientID signal emitted from the parent object
                // to the QLabel::setText slot
                connect(parent, &MainWindow::patientID, ui->label, &QLabel::setText);  
            }
            
            Pablo J. RoginaP 1 Reply Last reply
            2
            • J jhx76

              Hi everybody.

              Let's have a look to the code @russjohn834 gave us :

              class MainWindow : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                  MainWindow(QWidget *parent = nullptr);
                  ~MainWindow();
                  StageTwoNew *stagetwonew;
                  StageOne *stageone; 
              
              signals:
              
                  void patientID(QString);  ----------------> signal declaration
              
              
              public slots:
                  void on_pushButton_New_clicked();
              
                  void on_pushButton_Open_clicked();
              
                  void parseDataEntry(const QString dataPath);
              
              private:
                  Ui::MainWindow *ui;
              
              };
              #endif // MAINWINDOW_H
              

              Here we can see that the MainWindow owns an object of the class StageOne.
              So we can assume that the StageOne instance is built with specifying the parent as the MainWindow, certainly with code that looks like this :

              MainWindow::MainWindow() : QMainWindow() 
                  : stageone(new StageOne(this))
              {
              }
              

              If i am right, the parent of the stageone object is the MainWindow from which the signal patientID is emitted.

              You can then write things like this :

              StageOne::StageOne(QWidget *parent) :
                  QMainWindow(parent), // HERE the pointed parent is the MainWindow object
                  ui(new Ui::StageOne)
              {
                  ui->setupUi(this);
                  
                  // We connect the MainWindow::patientID signal emitted from the parent object
                  // to the QLabel::setText slot
                  connect(parent, &MainWindow::patientID, ui->label, &QLabel::setText);  
              }
              
              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #27

              @jhx76 said in Connecting two windows:

              If i am right, the parent of the stageone object is the MainWindow from which the signal patientID is emitted.

              it looks like there's no need to have signals at all.

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              4
              • J Offline
                J Offline
                jhx76
                wrote on last edited by jhx76
                #28

                That's right :)

                was just answering to the original question :

                @russjohn834

                What I'm doing wrong here?

                have a nice day

                1 Reply Last reply
                0
                • R russjohn834

                  Thank you @JonB, @jsulm , @J-Hilk and @Pablo-J-Rogina for your feedback

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

                  @russjohn834 Just a tip: to avoid circular dependencies use forward declarations:

                  // a.h
                  class B; // This is forward declaration for class B, no need to include b.h here
                  class A
                  {
                      private:
                          B *b;
                  };
                  
                  // b.h
                  #include "a.h"
                  ...
                  

                  Forward declarations work only for pointers - in the above example it would not work if b would not be a pointer.

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

                  1 Reply Last reply
                  2
                  • R Offline
                    R Offline
                    russjohn834
                    wrote on last edited by russjohn834
                    #30

                    @jsulm Thank you for your suggestion.
                    Just to further clarify myself the use of forward declaration:

                    Two form classes MainWindow and StageOne

                    StageOne needs to connect with a signal from MainWindow

                    I did this in StageOne

                    class MainWindow;  // ---------> forward declaration for Mainwindow
                    
                    namespace Ui {
                    class StageOne;
                    }
                    
                    class StageOne : public QMainWindow
                    {
                        Q_OBJECT
                    
                    public:
                        explicit StageOne(QString, QWidget *parent = nullptr);
                        ~StageOne();
                    
                    
                    private:
                        Ui::StageOne *ui;
                    
                        MainWindow *window;
                    
                    };
                    

                    I'm trying to connect as follows in StageOne:

                    connect(window,&MainWindow::patientIdChanged, ui->label, &QLabel::setText);
                    
                    

                    But this gives an error says:

                    stageone.cpp:12:21: error: incomplete type 'MainWindow' named in nested name specifier
                    stageone.h:10:7: note: forward declaration of 'MainWindow'
                    
                    

                    in mainwindow, i did include #include "stageone.h"

                    what mistake I'm doing here?

                    Thank you

                    jsulmJ 1 Reply Last reply
                    0
                    • R russjohn834

                      @jsulm Thank you for your suggestion.
                      Just to further clarify myself the use of forward declaration:

                      Two form classes MainWindow and StageOne

                      StageOne needs to connect with a signal from MainWindow

                      I did this in StageOne

                      class MainWindow;  // ---------> forward declaration for Mainwindow
                      
                      namespace Ui {
                      class StageOne;
                      }
                      
                      class StageOne : public QMainWindow
                      {
                          Q_OBJECT
                      
                      public:
                          explicit StageOne(QString, QWidget *parent = nullptr);
                          ~StageOne();
                      
                      
                      private:
                          Ui::StageOne *ui;
                      
                          MainWindow *window;
                      
                      };
                      

                      I'm trying to connect as follows in StageOne:

                      connect(window,&MainWindow::patientIdChanged, ui->label, &QLabel::setText);
                      
                      

                      But this gives an error says:

                      stageone.cpp:12:21: error: incomplete type 'MainWindow' named in nested name specifier
                      stageone.h:10:7: note: forward declaration of 'MainWindow'
                      
                      

                      in mainwindow, i did include #include "stageone.h"

                      what mistake I'm doing here?

                      Thank you

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

                      @russjohn834 You need to include mainwindow.h header in stageone.cpp

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

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        russjohn834
                        wrote on last edited by
                        #32

                        @jsulm if I include mainwindow.h header in stageone.cpp, I get follwoing error:

                        mainwindow.h:25: error: C2143: syntax error: missing ';' before '*'
                        mainwindow.h:25: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
                        mainwindow.h:25: error: C2238: unexpected token(s) preceding ';'
                        

                        Which points here:

                        #include <QMainWindow>
                        #include "stagetwonew.h"
                        #include "stageone.h"
                        
                        QT_BEGIN_NAMESPACE
                        namespace Ui { class MainWindow; }
                        QT_END_NAMESPACE
                        
                        class MainWindow : public QMainWindow
                        {
                            Q_OBJECT
                        
                        public:
                            MainWindow(QWidget *parent = nullptr);
                            ~MainWindow();
                            StageTwoNew *stagetwonew;
                            StageOne *stageone;  //--------------------> points here
                        
                        signals:
                        
                            void patientIdChanged(QString);
                        
                        
                        private slots:
                        
                            void on_pushButton_New_clicked();
                        
                            void on_pushButton_Open_clicked();
                        
                            void parseDataEntry(const QString dataPath);
                        
                        private:
                            Ui::MainWindow *ui;
                        
                        };
                        #endif // MAINWINDOW_H
                        
                        jsulmJ 1 Reply Last reply
                        0
                        • R russjohn834

                          @jsulm if I include mainwindow.h header in stageone.cpp, I get follwoing error:

                          mainwindow.h:25: error: C2143: syntax error: missing ';' before '*'
                          mainwindow.h:25: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
                          mainwindow.h:25: error: C2238: unexpected token(s) preceding ';'
                          

                          Which points here:

                          #include <QMainWindow>
                          #include "stagetwonew.h"
                          #include "stageone.h"
                          
                          QT_BEGIN_NAMESPACE
                          namespace Ui { class MainWindow; }
                          QT_END_NAMESPACE
                          
                          class MainWindow : public QMainWindow
                          {
                              Q_OBJECT
                          
                          public:
                              MainWindow(QWidget *parent = nullptr);
                              ~MainWindow();
                              StageTwoNew *stagetwonew;
                              StageOne *stageone;  //--------------------> points here
                          
                          signals:
                          
                              void patientIdChanged(QString);
                          
                          
                          private slots:
                          
                              void on_pushButton_New_clicked();
                          
                              void on_pushButton_Open_clicked();
                          
                              void parseDataEntry(const QString dataPath);
                          
                          private:
                              Ui::MainWindow *ui;
                          
                          };
                          #endif // MAINWINDOW_H
                          
                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #33

                          @russjohn834 Are you sure you don't include mainwindow.h in stageone.h?
                          Please show both header files.

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

                          R 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @russjohn834 Are you sure you don't include mainwindow.h in stageone.h?
                            Please show both header files.

                            R Offline
                            R Offline
                            russjohn834
                            wrote on last edited by
                            #34

                            @jsulm

                            I tried with without and with mainwindow.h in stageone.h. I'm a bit confused, you mentioned this earlier:
                            @jsulm said in Connecting two windows:

                            @russjohn834 You need to include mainwindow.h header in stageone.cpp

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              russjohn834
                              wrote on last edited by russjohn834
                              #35

                              here are two header files:

                              mainwindow.h

                              #ifndef MAINWINDOW_H
                              #define MAINWINDOW_H
                              
                              #include <QMainWindow>
                              #include <QDebug>
                              #include <QFile>
                              #include <QDirIterator>
                              #include <QXmlStreamReader>
                              #include <QMessageBox>
                              #include "stagetwonew.h"
                              #include "stageone.h"
                              
                              QT_BEGIN_NAMESPACE
                              namespace Ui { class MainWindow; }
                              QT_END_NAMESPACE
                              
                              class MainWindow : public QMainWindow
                              {
                                  Q_OBJECT
                              
                              public:
                                  MainWindow(QWidget *parent = nullptr);
                                  ~MainWindow();
                                  StageTwoNew *stagetwonew;
                                  StageOne *stageone;
                              
                              signals:
                              
                                  void patientIdChanged(QString);
                              
                              
                              private slots:
                              
                                  void on_pushButton_New_clicked();
                              
                                  void on_pushButton_Open_clicked();
                              
                                  void parseDataEntry(const QString dataPath);
                              
                              private:
                                  Ui::MainWindow *ui;
                              
                              };
                              #endif // MAINWINDOW_H
                              

                              and stageone.h:

                              #ifndef STAGEONE_H
                              #define STAGEONE_H
                              
                              #include <QMainWindow>
                              //#include "mainwindow.h"
                              #include "stagetwonew.h"
                              
                              
                              
                              class MainWindow;
                              namespace Ui {
                              class StageOne;
                              }
                              
                              class StageOne : public QMainWindow
                              {
                                  Q_OBJECT
                              
                              public:
                                  explicit StageOne(QString, QWidget *parent = nullptr);
                                  ~StageOne();
                              
                              
                              private:
                                  Ui::StageOne *ui;
                                  MainWindow *window;
                              };
                              
                              #endif // STAGEONE_H
                              

                              I do this at stageone:

                              StageOne::StageOne(QString someLabel, QWidget *parent) :
                                  QMainWindow(parent),
                                  ui(new Ui::StageOne)
                              {
                                  ui->setupUi(this);
                              
                              //    MainWindow *mainWindow= new MainWindow(this);
                                  connect(window,&MainWindow::patientIdChanged, ui->label, &QLabel::setText);  //
                              
                              }
                              

                              this gives an error says:

                              stageone.cpp:12:21: error: incomplete type 'MainWindow' named in nested name specifier
                              stageone.h:10:7: note: forward declaration of 'MainWindow'
                              
                              jsulmJ 1 Reply Last reply
                              0
                              • R russjohn834

                                here are two header files:

                                mainwindow.h

                                #ifndef MAINWINDOW_H
                                #define MAINWINDOW_H
                                
                                #include <QMainWindow>
                                #include <QDebug>
                                #include <QFile>
                                #include <QDirIterator>
                                #include <QXmlStreamReader>
                                #include <QMessageBox>
                                #include "stagetwonew.h"
                                #include "stageone.h"
                                
                                QT_BEGIN_NAMESPACE
                                namespace Ui { class MainWindow; }
                                QT_END_NAMESPACE
                                
                                class MainWindow : public QMainWindow
                                {
                                    Q_OBJECT
                                
                                public:
                                    MainWindow(QWidget *parent = nullptr);
                                    ~MainWindow();
                                    StageTwoNew *stagetwonew;
                                    StageOne *stageone;
                                
                                signals:
                                
                                    void patientIdChanged(QString);
                                
                                
                                private slots:
                                
                                    void on_pushButton_New_clicked();
                                
                                    void on_pushButton_Open_clicked();
                                
                                    void parseDataEntry(const QString dataPath);
                                
                                private:
                                    Ui::MainWindow *ui;
                                
                                };
                                #endif // MAINWINDOW_H
                                

                                and stageone.h:

                                #ifndef STAGEONE_H
                                #define STAGEONE_H
                                
                                #include <QMainWindow>
                                //#include "mainwindow.h"
                                #include "stagetwonew.h"
                                
                                
                                
                                class MainWindow;
                                namespace Ui {
                                class StageOne;
                                }
                                
                                class StageOne : public QMainWindow
                                {
                                    Q_OBJECT
                                
                                public:
                                    explicit StageOne(QString, QWidget *parent = nullptr);
                                    ~StageOne();
                                
                                
                                private:
                                    Ui::StageOne *ui;
                                    MainWindow *window;
                                };
                                
                                #endif // STAGEONE_H
                                

                                I do this at stageone:

                                StageOne::StageOne(QString someLabel, QWidget *parent) :
                                    QMainWindow(parent),
                                    ui(new Ui::StageOne)
                                {
                                    ui->setupUi(this);
                                
                                //    MainWindow *mainWindow= new MainWindow(this);
                                    connect(window,&MainWindow::patientIdChanged, ui->label, &QLabel::setText);  //
                                
                                }
                                

                                this gives an error says:

                                stageone.cpp:12:21: error: incomplete type 'MainWindow' named in nested name specifier
                                stageone.h:10:7: note: forward declaration of 'MainWindow'
                                
                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #36

                                @russjohn834 said in Connecting two windows:

                                #include "stagetwonew.h"

                                Does this one include mainwindow.h?

                                "stageone.cpp:12:21: error: incomplete type 'MainWindow' named in nested name specifier" - yes, because you did not include mainwindow.h in stageone.cpp
                                You really messed up your includes...

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

                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  russjohn834
                                  wrote on last edited by
                                  #37

                                  @jsulm I have a doubt

                                  @jsulm said in Connecting two windows:

                                  @russjohn834 Just a tip: to avoid circular dependencies use forward declarations:

                                  // a.h
                                  class B; // This is forward declaration for class B, no need to include b.h here
                                  class A
                                  {
                                      private:
                                          B *b;
                                  };
                                  
                                  // b.h
                                  #include "a.h"
                                  ...
                                  

                                  in the above example, even if I forward declare class B , I may not able to use any methods in class B unless I include b.h?

                                  So I was thinking how this is useful in a case , for ex:

                                  class B contains a signal which needs to be connected in class A?

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #38

                                    Hi,

                                    In that case, the real question is: do you really need these classes to know each other ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    2
                                    • R russjohn834

                                      @jsulm I have a doubt

                                      @jsulm said in Connecting two windows:

                                      @russjohn834 Just a tip: to avoid circular dependencies use forward declarations:

                                      // a.h
                                      class B; // This is forward declaration for class B, no need to include b.h here
                                      class A
                                      {
                                          private:
                                              B *b;
                                      };
                                      
                                      // b.h
                                      #include "a.h"
                                      ...
                                      

                                      in the above example, even if I forward declare class B , I may not able to use any methods in class B unless I include b.h?

                                      So I was thinking how this is useful in a case , for ex:

                                      class B contains a signal which needs to be connected in class A?

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

                                      @russjohn834 said in Connecting two windows:

                                      I may not able to use any methods in class B unless I include b.h?

                                      You can, in b.cpp if you include the header file there as I already said.
                                      Forward declaration only tells the compiler that B is a class, nothing more. This information is enough to declare a pointer to B (as pointer have always same size). But as soon as you want to access members/methods of B you need to include the header file, then compiler knows exactly what B is and which members/methods it has. See https://pvigier.github.io/2018/02/09/dependency-graph.html
                                      But as @SGaist said you should seriously rethink your design: are you sure both classes need to know each other? Usually it is enough that one class knows something about the other one. In Qt you can use signals/slots to implement loosely coupling.

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

                                      1 Reply Last reply
                                      2
                                      • R Offline
                                        R Offline
                                        russjohn834
                                        wrote on last edited by
                                        #40

                                        Thank you @jsulm , @SGaist for your feedback

                                        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