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.
  • J Offline
    J Offline
    jazzco2
    wrote on last edited by
    #7

    If this is of your class Settings the following should work.

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

    Otherwise replace the second class by the correct one.

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

      @jazzco2 Thanks a lot.

      The new connect syntax seems ok. It did not complain about anything. But I'm not getting the expected output. Just to summarize.

      I'm calling QMainWindow Patients from Settings. In the Patients constructor, I did connect as follows:

      connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);
      

      When I put a breakpoint in the connect statement, it did not went into the eventHandlerTwo slot. which means something wrong in the way I did the connection.

      where api is global variable.

      eventHandlerTwo is a public slot:

      void Patients::eventHandlerTwo( STIM_GUI_TOPIC_T topic, uint8_t reg, uint32_t value)
      {
          if (topic==TOPIC_STIMULATOR)
          {
              switch(reg)
              {
              case STIM_REG_BATTERY_CAPACITY_REMAINING:
      
                  if(value<86)
                      ui->qLed_p2->setOnColor(QLed::Red);
                  else
                      ui->qLed_p2->setOnColor(QLed::Green);
                      ui->qLed_p2->setValue(true);
                  break;
              }
          }
          ui->label_batteryVal->setText(QString::number(value));
       }
      
      

      Patients being called from Settings as follows:

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

      Any thoughts?

      JonBJ 1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #9

        So where do you call your connect() statement?

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

        1 Reply Last reply
        0
        • R russjohn834

          @jazzco2 Thanks a lot.

          The new connect syntax seems ok. It did not complain about anything. But I'm not getting the expected output. Just to summarize.

          I'm calling QMainWindow Patients from Settings. In the Patients constructor, I did connect as follows:

          connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);
          

          When I put a breakpoint in the connect statement, it did not went into the eventHandlerTwo slot. which means something wrong in the way I did the connection.

          where api is global variable.

          eventHandlerTwo is a public slot:

          void Patients::eventHandlerTwo( STIM_GUI_TOPIC_T topic, uint8_t reg, uint32_t value)
          {
              if (topic==TOPIC_STIMULATOR)
              {
                  switch(reg)
                  {
                  case STIM_REG_BATTERY_CAPACITY_REMAINING:
          
                      if(value<86)
                          ui->qLed_p2->setOnColor(QLed::Red);
                      else
                          ui->qLed_p2->setOnColor(QLed::Green);
                          ui->qLed_p2->setValue(true);
                      break;
                  }
              }
              ui->label_batteryVal->setText(QString::number(value));
           }
          
          

          Patients being called from Settings as follows:

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

          Any thoughts?

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

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

          connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);

          When I put a breakpoint in the connect statement, it did not went into the eventHandlerTwo slot. which means something wrong in the way I did connection.

          What do you mean? When the connect statement is executed it will not cause any entry into Patients::eventHandlerTwo, that only comes when the signal is later emitted.

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

            @Christian-Ehrlicher

            Connect is called as in the following:

            
            Patients::Patients(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::Patients)
            {
                ui->setupUi(this);
            
            
                this->setStyleSheet("background-color: white;");
                this->setFixedSize(this->width(),this->height());
            
            
            
                connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo); //--->connect is called here
                
            // Setup table
                ui->tableWidget->setColumnCount(5);
                ui->tableWidget->setHorizontalHeaderLabels(QStringList{"Patient ID","Name", "Surname", "LastSession", "Note"});
                ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
                ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
            
            
            
                 QString path = QCoreApplication::applicationDirPath()+"/data/";
            
            
            
                QStringList qdiFilter("*.xml");
            
            
                QDirIterator qdi( path, qdiFilter, QDir::Files);
                while (qdi.hasNext())
                {
                    parseDataEntry(qdi.next());
                }
            
            }
            
            1 Reply Last reply
            0
            • JonBJ JonB

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

              connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);

              When I put a breakpoint in the connect statement, it did not went into the eventHandlerTwo slot. which means something wrong in the way I did connection.

              What do you mean? When the connect statement is executed it will not cause any entry into Patients::eventHandlerTwo, that only comes when the signal is later emitted.

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

              @JonB

              I'm connecting with the same signal in the previous window , so do I need to disconnect that and connection to use the same signal in with a new slot?

              Basically in Settings window this works:

               connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler); //--> slot is eventHandler
              

              But in Patients this is not working:

                 connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);//--->Slot is eventHandlerTwo
              
              JonBJ 1 Reply Last reply
              0
              • R russjohn834

                @JonB

                I'm connecting with the same signal in the previous window , so do I need to disconnect that and connection to use the same signal in with a new slot?

                Basically in Settings window this works:

                 connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler); //--> slot is eventHandler
                

                But in Patients this is not working:

                   connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);//--->Slot is eventHandlerTwo
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #13

                @russjohn834
                So I now take it you mean you find the slot does not get entered, not the the connect fails?

                No, you do not have to disconnect, a signal may have any number of slots attached. Though if a previously-attached slot does not return, or misbehaves like if the slot instance is no longer around, it is possible the second slot might not get called.

                So if debugging what is going you might be advised to temporarily not connect the Settings slot, just the Patients slot and concentrate on sorting that out. It is not possible to say from what you have posted why one slot works and the other does not.

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

                  @JonB Thank you. I tried to connect only the Patients slot but cant see where I'm wrong..

                  Patients window is opened from Settings as given below:

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

                  Where stagetwo is public

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

                  Anything wrong I'm doing here!?

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

                    Hi all,

                    I just noticed this thing if I call both my Settings and Patients from my main the connection is successful (see below)

                    #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);
                        w.show();
                    
                        Patients v(nullptr); //---------> this makes connection successfull
                        v.show();
                    
                        return a.exec();
                    }
                    

                    but instead, if I call Patients from Settings even though the connection is successful im not getting the expected output.

                    My signal source api is a global variable which is initialized in the main as shown above.
                    So constructing both classes in the main makes connection ok and I'm gettting expected output. But if I call one window from other makes connection true but my slot is not being called at all.

                    What is the reason for this?

                    JonBJ 1 Reply Last reply
                    0
                    • R russjohn834

                      Hi all,

                      I just noticed this thing if I call both my Settings and Patients from my main the connection is successful (see below)

                      #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);
                          w.show();
                      
                          Patients v(nullptr); //---------> this makes connection successfull
                          v.show();
                      
                          return a.exec();
                      }
                      

                      but instead, if I call Patients from Settings even though the connection is successful im not getting the expected output.

                      My signal source api is a global variable which is initialized in the main as shown above.
                      So constructing both classes in the main makes connection ok and I'm gettting expected output. But if I call one window from other makes connection true but my slot is not being called at all.

                      What is the reason for this?

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

                      @russjohn834
                      If you're asking me to guess, show how you gain access to tetra_grip_api api; from Settings/Patients? In these cases where do you do the connect()s from? And, how do you know your connect()s are successful?

                      R 1 Reply Last reply
                      2
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #17

                        Hi,

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

                        QObject::connect(api.serial, SIGNAL(readyRead()), &api, SLOT(readData()));
                        QObject::connect(api.serial, SIGNAL(error(QSerialPort::SerialPortError)),&api, SLOT(ErrorHandler(QSerialPort::SerialPortError))); // error handling

                        Since serial is internal to api why don't you connect it in the constructor of your tetra_grip_api class ?

                        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
                        1
                        • JonBJ JonB

                          @russjohn834
                          If you're asking me to guess, show how you gain access to tetra_grip_api api; from Settings/Patients? In these cases where do you do the connect()s from? And, how do you know your connect()s are successful?

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

                          @JonB

                          This is how I make the connection in Settings.

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

                          Connection is successfull (qDebug()<<connect(....) gives TRUE) also the a QLabel and an LED in the eventHandler slot works perfectly.

                          In the same way I try to connect in the Patients:

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

                          here connection is successfull (qDebug()<<connect(....) gives TRUE) but the a QLabel and an LED in the eventHandlerTwo are not working.

                          JonBJ 1 Reply Last reply
                          0
                          • R russjohn834

                            @JonB

                            This is how I make the connection in Settings.

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

                            Connection is successfull (qDebug()<<connect(....) gives TRUE) also the a QLabel and an LED in the eventHandler slot works perfectly.

                            In the same way I try to connect in the Patients:

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

                            here connection is successfull (qDebug()<<connect(....) gives TRUE) but the a QLabel and an LED in the eventHandlerTwo are not working.

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

                            @russjohn834
                            How do I know &api refers to the same, global, initialized api instance in both cases?
                            And I see nothing in code about any QLabel in either case, not mentioned by you before...
                            You have to supply the necessary/relevant code if you want people to help.
                            Use a debugger to make sure whether slots are being hit.

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

                              @JonB

                              I thought api should be known and accessible to all classes if intialized as global in the main.cpp

                              #include <QApplication>
                              
                              tetra_grip_api api; //-------------> api instance
                              
                              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);
                                  w.show();
                              
                                  return a.exec();
                              }
                              

                              Correct me if I'm wrong.

                              JonBJ 1 Reply Last reply
                              0
                              • R russjohn834

                                @JonB

                                I thought api should be known and accessible to all classes if intialized as global in the main.cpp

                                #include <QApplication>
                                
                                tetra_grip_api api; //-------------> api instance
                                
                                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);
                                    w.show();
                                
                                    return a.exec();
                                }
                                

                                Correct me if I'm wrong.

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

                                @russjohn834
                                Your Settings & Patients classes are defined in a different source module from main.cpp, right? So how do they gain access to reference the api symbol in order to compile?

                                 connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);
                                

                                I am having to tease out each bit of relevant information, one at a time....

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

                                  @JonB
                                  Correct. I actually did not do anything other that making apiinstance as global in main.cpp. That might be the issue. But dont know how to proceed. How do I make Patients classes know about global api instance?

                                  jsulmJ JonBJ 2 Replies Last reply
                                  0
                                  • R russjohn834

                                    @JonB
                                    Correct. I actually did not do anything other that making apiinstance as global in main.cpp. That might be the issue. But dont know how to proceed. How do I make Patients classes know about global api instance?

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

                                    @russjohn834 You should avoid global variables as much as possible.
                                    Just pass the pointer to api to Settings & Patients constructors...

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

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

                                      Did you ensure that the connection in Patients is done before the signal tetra_grip_api::tetraGripEvent is emitted? Some qDebugs around connection, in the very first line of Patients::eventHandlerTwo and in the deconstructor of Patients should help to prove that.

                                      1 Reply Last reply
                                      1
                                      • R russjohn834

                                        @JonB
                                        Correct. I actually did not do anything other that making apiinstance as global in main.cpp. That might be the issue. But dont know how to proceed. How do I make Patients classes know about global api instance?

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

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

                                        @JonB
                                        Correct. I actually did not do anything other that making apiinstance as global in main.cpp. That might be the issue. But dont know how to proceed. How do I make Patients classes know about global api instance?

                                        I truly, truly do not understand. You said your program runs but does/does not behave properly in the cases you mentioned. I asked how you can even compile your Settings/Patients modules/.cpp source files which contain the line connect(&api, ..., when api is defined in main.cpp? From what you have said/shown they should error at compile-time with a "No such variable: api" message. So I will leave others to help, who obviously understand your source code better than I.

                                        J 1 Reply Last reply
                                        3
                                        • JonBJ JonB

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

                                          @JonB
                                          Correct. I actually did not do anything other that making apiinstance as global in main.cpp. That might be the issue. But dont know how to proceed. How do I make Patients classes know about global api instance?

                                          I truly, truly do not understand. You said your program runs but does/does not behave properly in the cases you mentioned. I asked how you can even compile your Settings/Patients modules/.cpp source files which contain the line connect(&api, ..., when api is defined in main.cpp? From what you have said/shown they should error at compile-time with a "No such variable: api" message. So I will leave others to help, who obviously understand your source code better than I.

                                          J Offline
                                          J Offline
                                          jazzco2
                                          wrote on last edited by
                                          #26

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

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

                                          isn't visible anywhere!

                                          JonBJ 1 Reply Last reply
                                          1

                                          • Login

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