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. Signals and Slots

Signals and Slots

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 2.7k Views 2 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 Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I have a Qt signal and slot connection:

    QObject::connect(pobjPushButton
                    ,&clsQtPushbtn::clicked
                    ,pobjSubscriber
                    ,&clsXMLnode::pbtnClicked);
    

    I want to replace this code with something like this:

    pSignalClicked = static_cast<some type>(&clsQtPushbtn::clicked);
    pSlotClicked = static_cast<some type>(&clsXMLnode::pbtnClicked);
    QObject::connect(pobjPushButton
                    ,pSignalClicked
                    ,pobjSubscriber
                    ,pSlotClicked);
    

    So far my attempts at replacing the original code with the later has failed.

    I have several signals and slots with the same signal provider and subscriber, I am trying to clean up the code with a single connect statement and then set the pSignalClicked and pSlotClick pointers before the connect.

    Kind Regards,
    Sy

    J.HilkJ 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      void (clsQtPushbtn::* pSignalClicked)(void) = &clsQtPushbtn::clicked;

      As you can see, on the left hand side of the =, the class the signal/slot comes from, its return type and argument types have to be fixed in advance

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        Thank you, however, what I'm looking for is a generic cast where I can assign any signal and slot and then use a single connection call.

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          I'm afraid it's not possible, anything more generic than the above causes undefined behaviour on dereferencing the pointer. It might be doable with templates but it really depends on your specific situation

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2
          • SPlattenS SPlatten

            I have a Qt signal and slot connection:

            QObject::connect(pobjPushButton
                            ,&clsQtPushbtn::clicked
                            ,pobjSubscriber
                            ,&clsXMLnode::pbtnClicked);
            

            I want to replace this code with something like this:

            pSignalClicked = static_cast<some type>(&clsQtPushbtn::clicked);
            pSlotClicked = static_cast<some type>(&clsXMLnode::pbtnClicked);
            QObject::connect(pobjPushButton
                            ,pSignalClicked
                            ,pobjSubscriber
                            ,pSlotClicked);
            

            So far my attempts at replacing the original code with the later has failed.

            I have several signals and slots with the same signal provider and subscriber, I am trying to clean up the code with a single connect statement and then set the pSignalClicked and pSlotClick pointers before the connect.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @SPlatten
            You could try it with the old qt4 syntax, as long as your Signal and slot names are the same, for all classes, it should work.

            But it's slower - in executing - than the qt5 one and you don't get compile time checks.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            2
            • SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #6

              @J-Hilk , do you mean using the SIGNAL and SLOT macros?

              Kind Regards,
              Sy

              J.HilkJ 1 Reply Last reply
              0
              • SPlattenS SPlatten

                @J-Hilk , do you mean using the SIGNAL and SLOT macros?

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @SPlatten yes


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by SPlatten
                  #8

                  @J-Hilk , thank you, works a treat:

                  const char* cpSignal = nullptr, * cpSlot = nullptr;
                  bool blnRc = false;
                  
                  if ( eSignal == ST_PUSHBUTTON_CLICKED ) {
                      cpSignal = SIGNAL(clsQtPushbtn::clicked);
                      cpSlot = SLOT(clsXMLnode::pbtnClicked);
                      blnRc = true;
                  }
                  

                  The only problem is now a variant of the connect that I'm also using:

                          QObject::connect(pobjPushButton
                                          ,&clsQtPushbtn::clicked
                                          ,[pobjScriptEng, strCall]() {
                                              QString strScript = clsMainWnd::strGetScript();
                  
                                              if ( strCall.isEmpty() != true ) {
                                                  strScript += strCall + "();";
                                                  pobjScriptEng->evaluate(strScript);
                                              }
                                          });
                  

                  I cannot use the SIGNAL() macro in this connect method.

                  Kind Regards,
                  Sy

                  VRoninV 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    @J-Hilk , thank you, works a treat:

                    const char* cpSignal = nullptr, * cpSlot = nullptr;
                    bool blnRc = false;
                    
                    if ( eSignal == ST_PUSHBUTTON_CLICKED ) {
                        cpSignal = SIGNAL(clsQtPushbtn::clicked);
                        cpSlot = SLOT(clsXMLnode::pbtnClicked);
                        blnRc = true;
                    }
                    

                    The only problem is now a variant of the connect that I'm also using:

                            QObject::connect(pobjPushButton
                                            ,&clsQtPushbtn::clicked
                                            ,[pobjScriptEng, strCall]() {
                                                QString strScript = clsMainWnd::strGetScript();
                    
                                                if ( strCall.isEmpty() != true ) {
                                                    strScript += strCall + "();";
                                                    pobjScriptEng->evaluate(strScript);
                                                }
                                            });
                    

                    I cannot use the SIGNAL() macro in this connect method.

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9

                    @SPlatten said in Signals and Slots:

                    cpSignal = SIGNAL(clsQtPushbtn::clicked);
                    cpSlot = SLOT(clsXMLnode::pbtnClicked);

                    This should not work. it should be

                    cpSignal = SIGNAL(clicked());
                    cpSlot = SLOT(pbtnClicked());
                    

                    @SPlatten said in Signals and Slots:

                    The only problem is now a variant of the connect that I'm also using

                    You can use a QSignalMapper as a bridge between the two

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    0
                    • SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by SPlatten
                      #10

                      @VRonin , it does work, I've tried it.

                      Kind Regards,
                      Sy

                      VRoninV 1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        @VRonin , it does work, I've tried it.

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        @SPlatten said in Signals and Slots:

                        it does work, I've tried it.

                        Are you 100% sure? The below is a minimal example that shows it doesn't work

                        #include <QApplication>
                        #include <QSpinBox>
                        #include <QSlider>
                        #include <QHBoxLayout>
                        int main(int argc, char ** argv)
                        {
                            QApplication app(argc, argv);
                            QWidget mainWid;
                            QHBoxLayout* minLay = new QHBoxLayout(&mainWid);
                            QSpinBox* testSpinn=new QSpinBox(&mainWid);
                            testSpinn->setRange(0,100);
                            testSpinn->setValue(50);
                            QSlider* testSlide=new QSlider(Qt::Horizontal,&mainWid);
                            testSlide->setRange(0,100);
                            testSlide->setValue(50);
                            minLay->addWidget(testSpinn);
                            minLay->addWidget(testSlide);
                        
                            // Works
                            // QObject::connect(testSpinn,SIGNAL(valueChanged(int)),testSlide,SLOT(setValue(int)));
                            // QObject::connect(testSlide,SIGNAL(valueChanged(int)),testSpinn,SLOT(setValue(int)));
                        
                            // Doesn't Work
                            QObject::connect(testSpinn,SIGNAL(QSpinBox::valueChanged),testSlide,SLOT(QSlider::setValue));
                            QObject::connect(testSlide,SIGNAL(QSlider::valueChanged),testSpinn,SLOT(QSpinBox::setValue));
                        
                            mainWid.show();
                            return app.exec();
                        }
                        

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        2
                        • SPlattenS Offline
                          SPlattenS Offline
                          SPlatten
                          wrote on last edited by
                          #12

                          @VRonin , I'm absolutely positive, I have a QPushButton, which I connected the "clicked" signal using the macro's this was connected to the slot also using the macro's exactly as I posted, it worked...I'm using Qt 5.12

                          Kind Regards,
                          Sy

                          1 Reply Last reply
                          0
                          • VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #13

                            I checked the current sources. It can't work. But I feel I'm being pedantic and not really helpful.
                            Did you manage to use QSignalMapper as a bridge to connect a lambda?

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            1 Reply Last reply
                            0
                            • SPlattenS Offline
                              SPlattenS Offline
                              SPlatten
                              wrote on last edited by
                              #14

                              No, I've worked around the problem.

                              Kind Regards,
                              Sy

                              Pablo J. RoginaP 1 Reply Last reply
                              0
                              • SPlattenS SPlatten

                                No, I've worked around the problem.

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

                                @SPlatten so is your issue solved? Please don't forget to mark your post as such. Thanks

                                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
                                0

                                • Login

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