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

same signal-slot connection in two windows

Scheduled Pinned Locked Moved Solved General and Desktop
60 Posts 9 Posters 9.6k Views 3 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
    #32

    @jazzco2

    Could you give me an example to show how do I pass a pointer to the constructor of the classes? for ex. if I need to use it in in Settings and Patients classes ?

    jsulmJ 1 Reply Last reply
    0
    • R russjohn834

      @jazzco2

      Could you give me an example to show how do I pass a pointer to the constructor of the classes? for ex. if I need to use it in in Settings and Patients classes ?

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

      @russjohn834 said in same signal-slot connection in two windows:

      Could you give me an example to show how do I pass a pointer to the constructor of the classes?

      Well

      tetra_grip_api api;
      Settings settings(&api);
      

      Of cource you have to change the constructor, but this is C++ basic stuff.

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

      R 1 Reply Last reply
      2
      • JonBJ JonB

        @jazzco2 said in same signal-slot connection in two windows:

        @JonB Yes, that's interesting. You can't include main.cpp in Settings or in Patients.

        No, but you might be including main.h, and maybe you have extern tetra_grip_api api; there, that's the sort of thing I was trying to understand. Which finally you have shown is actually in tetra_grip_api.h, which I guess you are including into each of them?

        For the line there

        extern tetra_grip_api api;
        

        does the docs for tetra_grip_api say you are responsible for defining tetra_grip_api api; yourself like you have done in main.cpp, or does it say they (the library) are defining that global instance and you are supposed to use their instance?

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

        @JonB

        it did not mention anything like that. My primary requirement is to use tetra_grip_api(which is a class containing all serial communication objects) instance in every form class. That's why I made it global, but now I realize it's not a good way of programming!

        JonBJ 1 Reply Last reply
        0
        • R russjohn834

          @JonB

          it did not mention anything like that. My primary requirement is to use tetra_grip_api(which is a class containing all serial communication objects) instance in every form class. That's why I made it global, but now I realize it's not a good way of programming!

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

          @russjohn834 said in same signal-slot connection in two windows:
          Yes and no, it gets complicated. You may not need to pass it around that way.

          It looks like the tetra_grip_api is something in-house to you, it does not exist on the web. You have shown that tetra_grip_api.h file contains the line:

          extern tetra_grip_api api;  //----------> it leads me here
          

          You should ask the author of that file whether he intends:

          1. It is up to you to go tetra_grip_api api once in one of your own files, in order to define it, as you have done in your main.cpp; OR

          2. The tetra_grip_api library already contains this definition for you, and you are supposed to use that one.

          For now, humour me:

          Go into your main.cpp. Locate the line which reads:

          tetra_grip_api api; //-------------> api instance
          

          Now comment out that line.

          Try to rebuild & relink. What happens? Do you end up, after the linking, with an error message like: "Unresolved symbol: ''tetra_grip_api", or not?

          • If there is no error, see whether your program behaves correctly now.

          • If there is an error, unfortunately it's time we saw all your relevant code which should work but does not, because there is way too much incomplete information in what you have posted. I hesitate to say this, because I have a bad feeling that's going to be (at least) 3 .cpp files & 3 .h. files. Which is an awful lot for to paste and for us to look through :( If you could cut it down to a relevant, minimal subset which reproduces the bad behaviour you say you see, that would be really good....

          1 Reply Last reply
          2
          • jsulmJ jsulm

            @russjohn834 said in same signal-slot connection in two windows:

            Could you give me an example to show how do I pass a pointer to the constructor of the classes?

            Well

            tetra_grip_api api;
            Settings settings(&api);
            

            Of cource you have to change the constructor, but this is C++ basic stuff.

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

            @JonB Thanks a lot for your feedback.

            Commenting out of tetra_grip_api api in the main.cpp did end up in multiple errors with "Unresolved external symbol class tetra_grip_api api".

            From the suggestions from you guys, I feel like there is something wrong in making use of global variables. I should try also the other way of passing pointer to the class.

            But I was trying to understand a reason program behaves this way.

            As I mentioned earlier , If I call Patients behaves badly if I call it from Settings. But instead If I call both Patieints and Settings in the main (as shown below), both behave as expected. Do you immedatly think of any reason for this!?

            #include <QApplication>
            
            tetra_grip_api api;
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
            
                api.openSerialPort();
                QObject::connect(api.serial, SIGNAL(readyRead()), &api, SLOT(readData()));
                QObject::connect(api.serial, SIGNAL(error(QSerialPort::SerialPortError)),&api, SLOT(ErrorHandler(QSerialPort::SerialPortError))); // error handling
            
            
                Settings  w(nullptr); //---->this behaves right
                Patients  v(nullptr); //---- >this behaves right
                v.show();
                w.show();
            
                return a.exec();
            }
            
            JonBJ 1 Reply Last reply
            0
            • R russjohn834

              @JonB Thanks a lot for your feedback.

              Commenting out of tetra_grip_api api in the main.cpp did end up in multiple errors with "Unresolved external symbol class tetra_grip_api api".

              From the suggestions from you guys, I feel like there is something wrong in making use of global variables. I should try also the other way of passing pointer to the class.

              But I was trying to understand a reason program behaves this way.

              As I mentioned earlier , If I call Patients behaves badly if I call it from Settings. But instead If I call both Patieints and Settings in the main (as shown below), both behave as expected. Do you immedatly think of any reason for this!?

              #include <QApplication>
              
              tetra_grip_api api;
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  api.openSerialPort();
                  QObject::connect(api.serial, SIGNAL(readyRead()), &api, SLOT(readData()));
                  QObject::connect(api.serial, SIGNAL(error(QSerialPort::SerialPortError)),&api, SLOT(ErrorHandler(QSerialPort::SerialPortError))); // error handling
              
              
                  Settings  w(nullptr); //---->this behaves right
                  Patients  v(nullptr); //---- >this behaves right
                  v.show();
                  w.show();
              
                  return a.exec();
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #37

              @russjohn834
              No, because you show the case which works OK. We need to see exactly how Settings calls Patients, plus the signal connections, with whatever is relevant to that case, to judge where the problem is.

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

                @JonB

                This is how I call Patients from Settings

                void Settings::on_pushButton_patients_clicked()
                {
                    this->close();
                    stagetwo = new Patients(this);
                    stagetwo -> show();
                }
                

                where stagetwo is public

                public:
                     Settings(QString,QWidget *parent = nullptr);
                    ~Settings();
                     Patients *stagetwo;
                

                In the Patients signal connection is as below:

                Patients::Patients( QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::Patients)
                {
                    ui->setupUi(this);
                
                connect(&api, &tetra_grip_api::tetraGripEvent,this, &StageTwoPatients::eventHandlerTwo);
                
                }
                
                JonBJ R 2 Replies Last reply
                0
                • R russjohn834

                  @JonB

                  This is how I call Patients from Settings

                  void Settings::on_pushButton_patients_clicked()
                  {
                      this->close();
                      stagetwo = new Patients(this);
                      stagetwo -> show();
                  }
                  

                  where stagetwo is public

                  public:
                       Settings(QString,QWidget *parent = nullptr);
                      ~Settings();
                       Patients *stagetwo;
                  

                  In the Patients signal connection is as below:

                  Patients::Patients( QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::Patients)
                  {
                      ui->setupUi(this);
                  
                  connect(&api, &tetra_grip_api::tetraGripEvent,this, &StageTwoPatients::eventHandlerTwo);
                  
                  }
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #39

                  @russjohn834 said in same signal-slot connection in two windows:

                  void Settings::on_pushButton_patients_clicked()
                  {
                  this->close();
                  stagetwo = new Patients(this);
                  stagetwo -> show();
                  }

                  Just to humour me, replace by

                  void Settings::on_pushButton_patients_clicked()
                  {
                      // this->close();
                      // stagetwo = new Patients(this);
                      stagetwo = new Patients(nullptr);
                      stagetwo -> show();
                  }
                  

                  Any better behaviour?

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

                    @JonB

                    Can't see any change.

                    JonBJ 1 Reply Last reply
                    0
                    • R russjohn834

                      @JonB

                      Can't see any change.

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

                      @russjohn834
                      Very last thing: how do you know slot is not being called? Please place a breakpoint on it.

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

                        @JonB
                        I did try with a breakpoint, and convinced that the slot is not being called when the Patients window is constructed.
                        So I guess that's a serious issue using global class instance. Though I was trying to understand why this is happening ..

                        JonBJ 1 Reply Last reply
                        0
                        • R russjohn834

                          @JonB
                          I did try with a breakpoint, and convinced that the slot is not being called when the Patients window is constructed.
                          So I guess that's a serious issue using global class instance. Though I was trying to understand why this is happening ..

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

                          @russjohn834
                          There should be no problem. I don't know what the difference is in your working vs non-working situations, I'm afraid.

                          1 Reply Last reply
                          1
                          • R russjohn834

                            @JonB

                            This is how I call Patients from Settings

                            void Settings::on_pushButton_patients_clicked()
                            {
                                this->close();
                                stagetwo = new Patients(this);
                                stagetwo -> show();
                            }
                            

                            where stagetwo is public

                            public:
                                 Settings(QString,QWidget *parent = nullptr);
                                ~Settings();
                                 Patients *stagetwo;
                            

                            In the Patients signal connection is as below:

                            Patients::Patients( QWidget *parent) :
                                QMainWindow(parent),
                                ui(new Ui::Patients)
                            {
                                ui->setupUi(this);
                            
                            connect(&api, &tetra_grip_api::tetraGripEvent,this, &StageTwoPatients::eventHandlerTwo);
                            
                            }
                            
                            R Offline
                            R Offline
                            russjohn834
                            wrote on last edited by russjohn834
                            #44

                            @mrjj
                            can you think of a reason why this second scenario not working?

                            I have signal connection in two windows , Settings and Patients

                            In Settings:

                            Settings::Settings( QWidget *parent) :
                                QMainWindow(parent),
                                ui(new Ui::Settings)
                            {
                                ui->setupUi(this);
                            
                            connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler);
                            ...
                            }
                            
                            

                            api is global instance of a class.

                            And in Patients:

                            Patients::Patients( QWidget *parent) :
                                QMainWindow(parent),
                                ui(new Ui::Patients)
                            {
                                ui->setupUi(this);
                            
                            connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);
                            
                            }
                            

                            Situation1 - Working

                            I construct both Settings and Patients window from Main

                            #include <QApplication>
                            
                            tetra_grip_api api;
                            
                            int main(int argc, char *argv[])
                            {
                                QApplication a(argc, argv);
                            
                                api.openSerialPort();
                                QObject::connect(api.serial, SIGNAL(readyRead()), &api, SLOT(readData()));
                                
                            
                                Settings  w(nullptr); //---->this behaves right
                                Patients  v(nullptr); //---- >this behaves right
                            
                                v.show();
                                w.show();
                            
                                return a.exec();
                            }
                            

                            By working I mean , both the slots are being called , and QLabel set text accordingly

                            Situation 2 - Not working

                            I call Patients from Settings:

                            void Settings::on_pushButton_patients_clicked()
                            {
                                this->close();
                                stagetwo = new Patients(this);
                                stagetwo -> show();
                            }
                            

                            where stagetwo is public

                            public:
                                 Settings(QString,QWidget *parent = nullptr);
                                ~Settings();
                                 Patients *stagetwo;
                            

                            Here Settings works just fine (slot being called) but slot in Patients, eventhandlerTwo is not being called at all.

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              jazzco2
                              wrote on last edited by
                              #45

                              @russjohn834 Can you test what happens if you hide() the Settings instead of close() them?

                              void Settings::on_pushButton_patients_clicked()
                              {
                                  this->hide();
                                  stagetwo = new Patients(this);
                                  stagetwo -> show();
                              }
                              
                              1 Reply Last reply
                              1
                              • R Offline
                                R Offline
                                russjohn834
                                wrote on last edited by
                                #46

                                @jazzco2 Did try that, in both case (hide or close) slot in the Patients not being called

                                JonBJ 1 Reply Last reply
                                0
                                • R russjohn834

                                  @jazzco2 Did try that, in both case (hide or close) slot in the Patients not being called

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

                                  @russjohn834
                                  Try commenting out in Settings::Settings

                                  connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler);
                                  

                                  Let's not have Settings connected to the signal while we sort out Patients.

                                  1 Reply Last reply
                                  1
                                  • J Offline
                                    J Offline
                                    jazzco2
                                    wrote on last edited by
                                    #48

                                    Ok, I just wondered what happens, because close() may delete your Settings afterwards and you have no access to neither Settings nor Patients. What about the other tests:

                                    • Did it help to pass the pointer to Settings and to Patients in the constructor?

                                    • In what order are the debug messages when you add them in Settings::eventHandler() and after connecting and disconnecting (destructor) in Settings and Patients? (I wonder if the connection is cut off before it is called)

                                    JonBJ 1 Reply Last reply
                                    1
                                    • J jazzco2

                                      Ok, I just wondered what happens, because close() may delete your Settings afterwards and you have no access to neither Settings nor Patients. What about the other tests:

                                      • Did it help to pass the pointer to Settings and to Patients in the constructor?

                                      • In what order are the debug messages when you add them in Settings::eventHandler() and after connecting and disconnecting (destructor) in Settings and Patients? (I wonder if the connection is cut off before it is called)

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

                                      @jazzco2
                                      I suggested earlier that OP run for now with

                                      void Settings::on_pushButton_patients_clicked()
                                      {
                                          // this->close();
                                          // stagetwo = new Patients(this);
                                          stagetwo = new Patients(nullptr);
                                          stagetwo -> show();
                                      }
                                      

                                      Although it apparently did not solve, I suggest he revert to that code while sorting this out. It will remove some irrelevant paths of investigation.

                                      1 Reply Last reply
                                      2
                                      • Christian EhrlicherC Offline
                                        Christian EhrlicherC Offline
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #50

                                        Maybe it would be easier if the OP could provide a minimal, compilable example of his problem so we can try it out by ourself. I see a good chance here that the current code is that confusing that no suggestion from here will ever help.

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        R 1 Reply Last reply
                                        3
                                        • J Offline
                                          J Offline
                                          jazzco2
                                          wrote on last edited by
                                          #51

                                          @JonB Yes that's fine. It also should be ensured that

                                          • both &api references point to the same instance.
                                            -> add debug messages at each connect(..) to display the pointer

                                          • the connection is alive
                                            -> add several debug messages as I pointed out earlier

                                          • the signal tetraGripEvent() is emitted in both cases
                                            -> keep Settings alive (using hide() instead of close()) and add a debug message in Settings::eventHandler

                                          1 Reply Last reply
                                          2

                                          • Login

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