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. Qt 4.8, connecting signals with slots
Forum Updated to NodeBB v4.3 + New Features

Qt 4.8, connecting signals with slots

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 1.2k 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.
  • SPlattenS SPlatten

    I have a number of different controls, I am trying to implement a generic mechanism for connecting signals to slots , in the configuration file I have a section:

    [CONNECTIONS]
    "dial1","dialMoved(int)","lbl1","setText"
    

    dial1 and lbl1 are unique IDs used to store the controls in a QMap.
    dialMoved(int) is the signal and setText is the slot, however I'm using it at the moment as I would like to use a lamda slot to receive and handle the slot forwarding.

    Is there anyway to connect a signal and slot using the string name "dialMoved(int)", I have written a lambda class that I use elsewhere and this works fine, but so far my attempts to create a connecting are failing.

    KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by KroMignon
    #2

    @SPlatten said in Qt 4.8, connecting signals with slots:

    Is there anyway to connect a signal and slot using the string name "dialMoved(int)", I have written a lambda class that I use elsewhere and this works fine, but so far my attempts to create a connecting are failing.

    AFAIK, connect() accept a const char * for signal and slot.
    So this should work:

    // I suppose all is in a QStringList cnxInfo
    // 0: src object name
    // 1: src object signal
    // 2: dest object name
    // 3: dest object slot
    
    // I suppose all items are childs of the class instance
    QObject * src = this->findChild<QObject *>(cnxInfo[0]);
    QObject * dest = this->findChild<QObject *>(cnxInfo[2]);
    
    QObject::connect(src, cnxInfo[1].toLatin1().constData(), dest, cnxInfo[3].toLatin1().constData());
    

    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

    SPlattenS 1 Reply Last reply
    1
    • SPlattenS SPlatten

      I have a number of different controls, I am trying to implement a generic mechanism for connecting signals to slots , in the configuration file I have a section:

      [CONNECTIONS]
      "dial1","dialMoved(int)","lbl1","setText"
      

      dial1 and lbl1 are unique IDs used to store the controls in a QMap.
      dialMoved(int) is the signal and setText is the slot, however I'm using it at the moment as I would like to use a lamda slot to receive and handle the slot forwarding.

      Is there anyway to connect a signal and slot using the string name "dialMoved(int)", I have written a lambda class that I use elsewhere and this works fine, but so far my attempts to create a connecting are failing.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #3

      @SPlatten said in Qt 4.8, connecting signals with slots:

      dial1 and lbl1 are unique IDs used to store the controls in a QMap.

      You cant have dynamic/variable variable names in C++...

      So having a connection like

      connect(map.key(), SIGNAL(.....), map.value(), SLOT(....));
      

      wont work AFAIK, when you store strings in your map.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      SPlattenS 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @SPlatten said in Qt 4.8, connecting signals with slots:

        dial1 and lbl1 are unique IDs used to store the controls in a QMap.

        You cant have dynamic/variable variable names in C++...

        So having a connection like

        connect(map.key(), SIGNAL(.....), map.value(), SLOT(....));
        

        wont work AFAIK, when you store strings in your map.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #4

        @Pl45m4 , ah, but it does work, I have been doing exactly this with Qt 5 for several years.

        Kind Regards,
        Sy

        Pl45m4P 1 Reply Last reply
        0
        • KroMignonK KroMignon

          @SPlatten said in Qt 4.8, connecting signals with slots:

          Is there anyway to connect a signal and slot using the string name "dialMoved(int)", I have written a lambda class that I use elsewhere and this works fine, but so far my attempts to create a connecting are failing.

          AFAIK, connect() accept a const char * for signal and slot.
          So this should work:

          // I suppose all is in a QStringList cnxInfo
          // 0: src object name
          // 1: src object signal
          // 2: dest object name
          // 3: dest object slot
          
          // I suppose all items are childs of the class instance
          QObject * src = this->findChild<QObject *>(cnxInfo[0]);
          QObject * dest = this->findChild<QObject *>(cnxInfo[2]);
          
          QObject::connect(src, cnxInfo[1].toLatin1().constData(), dest, cnxInfo[3].toLatin1().constData());
          
          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #5

          @KroMignon , thank you, still not quite right.

          Kind Regards,
          Sy

          KroMignonK 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @KroMignon , thank you, still not quite right.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #6

            @SPlatten said in Qt 4.8, connecting signals with slots:

            thank you, still not quite right.

            Don't understand what you mean with this?

            Just to complete my previous reply: lambdas are not allowed/possible with Qt 4.x. It requires new connect() syntax which was introduced with Qt 5.x

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            SPlattenS 1 Reply Last reply
            0
            • KroMignonK KroMignon

              @SPlatten said in Qt 4.8, connecting signals with slots:

              thank you, still not quite right.

              Don't understand what you mean with this?

              Just to complete my previous reply: lambdas are not allowed/possible with Qt 4.x. It requires new connect() syntax which was introduced with Qt 5.x

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by SPlatten
              #7

              @KroMignon , sorry, can't you read minds ? ;)

              I have tried:

              QObject::connect(src, cnxInfo[1].toLatin1().constData(), dest, cnxInfo[3].toLatin1().constData());
              

              Still returns false, also tried:

              QObject::connect(pobjObject, SIGNAL(cstrSignalName), pobjSlotCtrl, SLOT(cstrSlotName));
              

              Where pobjObject points to the signal source, cstrSignalName contains dialMoved(int), pobjSlotCtrl points the slot object and cstrSlotName contains setText(int).

              Returns false too.

              If I try:

              QObject::connect(pobjObject, cstrSignalName.toLatin1().constData()
                              ,pobjSlotCtrl, cstrSlotName.toLatin1().constData());
              

              I see the output:

              Object::connect: Use the SIGNAL macro to bind QDial::dialMoved(int)
              

              Kind Regards,
              Sy

              Christian EhrlicherC KroMignonK 2 Replies Last reply
              0
              • SPlattenS SPlatten

                @KroMignon , sorry, can't you read minds ? ;)

                I have tried:

                QObject::connect(src, cnxInfo[1].toLatin1().constData(), dest, cnxInfo[3].toLatin1().constData());
                

                Still returns false, also tried:

                QObject::connect(pobjObject, SIGNAL(cstrSignalName), pobjSlotCtrl, SLOT(cstrSlotName));
                

                Where pobjObject points to the signal source, cstrSignalName contains dialMoved(int), pobjSlotCtrl points the slot object and cstrSlotName contains setText(int).

                Returns false too.

                If I try:

                QObject::connect(pobjObject, cstrSignalName.toLatin1().constData()
                                ,pobjSlotCtrl, cstrSlotName.toLatin1().constData());
                

                I see the output:

                Object::connect: Use the SIGNAL macro to bind QDial::dialMoved(int)
                
                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @SPlatten said in Qt 4.8, connecting signals with slots:

                Returns false too.

                Wonder why this should work...

                Hint: Take a look at the SIGNAL() and SLOT() macro - they're not no-ops.

                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
                1
                • SPlattenS SPlatten

                  @Pl45m4 , ah, but it does work, I have been doing exactly this with Qt 5 for several years.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #9

                  @SPlatten

                  Then I maybe misunderstood what you are doing exactly.
                  You can connect QObjects, not strings


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • Pl45m4P Pl45m4

                    @SPlatten

                    Then I maybe misunderstood what you are doing exactly.
                    You can connect QObjects, not strings

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @Pl45m4 said in Qt 4.8, connecting signals with slots:

                    You can connect QObjects, not strings

                    He's retrieving the QObjects via their objectName. This implies that they're properly set before.

                    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
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      Hi,

                      Beside the fact that you are trying to connect a signal with an int parameter to a slot with a QString parameter, if you really want to do that kind of generic connection, you should go with some meta programming. Use QMetaObject to retrieve the proper QMetaMethod objects for the signal and the slot and use them for the connection.

                      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
                      3
                      • SPlattenS SPlatten

                        @KroMignon , sorry, can't you read minds ? ;)

                        I have tried:

                        QObject::connect(src, cnxInfo[1].toLatin1().constData(), dest, cnxInfo[3].toLatin1().constData());
                        

                        Still returns false, also tried:

                        QObject::connect(pobjObject, SIGNAL(cstrSignalName), pobjSlotCtrl, SLOT(cstrSlotName));
                        

                        Where pobjObject points to the signal source, cstrSignalName contains dialMoved(int), pobjSlotCtrl points the slot object and cstrSlotName contains setText(int).

                        Returns false too.

                        If I try:

                        QObject::connect(pobjObject, cstrSignalName.toLatin1().constData()
                                        ,pobjSlotCtrl, cstrSlotName.toLatin1().constData());
                        

                        I see the output:

                        Object::connect: Use the SIGNAL macro to bind QDial::dialMoved(int)
                        
                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by
                        #12

                        @SPlatten said in Qt 4.8, connecting signals with slots:

                        I see the output:
                        Object::connect: Use the SIGNAL macro to bind QDial::dialMoved(int)

                        Ok my failure, I was a little too optimist.
                        As @Christian-Ehrlicher and @SGaist already says, you have to use QMetaObject / QMetaMethod to be able to do it.

                        Here a small example:

                        #include <QObject>
                        #include <QDebug>
                        #include <QCoreApplication>
                        #include <QTimer>
                        #include <QMetaObject>
                        #include <QMetaMethod>
                        
                        class ConnectTest : public QObject
                        {
                            Q_OBJECT
                        public:
                            explicit ConnectTest(QObject *parent = nullptr) : QObject{parent} { }
                        
                        public slots:
                            void mySlots() { qDebug() << "ConnectTest"; }
                        };
                        
                        int main(int argc, char *argv[])
                        {
                            QCoreApplication a(argc, argv);
                        
                            auto tmr = new QTimer(&a);
                            tmr->setObjectName("myTimer");
                            auto cnxTest = new ConnectTest(&a);
                            cnxTest->setObjectName("cnxTest");
                        
                            QStringList lst;
                            lst 
                               << "myTimer"
                               << "timeout()"
                               << "cnxTest"
                               << "mySlots()"; 
                        
                            auto src = a.findChild<QObject*>(lst[0]);
                            if(!src)
                                qDebug() << "src" << lst[0] <<"not found";
                            auto dest = a.findChild<QObject*>(lst[2]);
                            if(!src)
                                qDebug() << "dest" << lst[2] <<"not found";
                        
                            // find the signal in the source instance
                            int signal = src->metaObject()->indexOfSignal(lst[1].toLatin1().constData());
                            if(signal < 0)
                                qDebug() << "signal" << lst[1] << "not found in" << lst[0];
                        
                            // find the slot in the destination instance
                            int slot = dest->metaObject()->indexOfSlot(lst[3].toLatin1().constData());
                            if(slot < 0)
                                qDebug() << "slot" << lst[3] << "not found in" << lst[2];
                        
                            // connect source and destination
                            QObject::connect(src, src->metaObject()->method(signal),
                                    dest, dest->metaObject()->method(slot));
                        
                            // start the timer
                            tmr->start(5000);
                            return a.exec();
                        }
                        

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        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