Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved setup my own abstract class functions.. (about connect and slots)

    General and Desktop
    7
    13
    291
    Loading More Posts
    • 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.
    • U
      U7Development last edited by U7Development

      Hi!

      I have created a function that manages and returns a QPushButton pointer.

      QPushButton* setup_button(QString _txt, QString _style, QRect _geo, UnknownParam _param){
           QPushButton* o = new QPushButton(_txt);
           o->setStyleSheet(_style);
           o->setGeometry(_geo);
           connect(o, SIGNAL(clicked()), this, _param);
           vQWidgets.push_back(o);
           return o;
      }
      
      

      I want that "UnknownParam" parameter to receive a function for a slot so i can setup my button as following example:

      QPushButton* myButton = setup_button("Click me", btnStyle, QRect(10, 10, 20, 20), SLOT(action4myButton()));
      

      what is the parameter type i need to do this?.

      Thanks in advance.
      Mike

      B JonB 2 Replies Last reply Reply Quote 1
      • B
        Bonnie @U7Development last edited by Bonnie

        const char *

        1 Reply Last reply Reply Quote 5
        • Poor English
          Poor English last edited by

          I do not quite understand what you mean,does it mean that a certain button can be generated when the program is running,and then the slot function corresponding to the clicked button is dynamically determined when the program is running,right?In other words, in connect,how to pass a function pointer and use it as the content in SLOT,is that right?

          I am sorry about my poor English!

          1 Reply Last reply Reply Quote 1
          • jeremy_k
            jeremy_k last edited by

            https://doc.qt.io/qt-5/qobject.html#connect:

            QMetaObject::Connection QObject::connect(
                const QObject *sender, 
                const char *signal,
                const QObject *receiver,
                const char *method,
                Qt::ConnectionType type = Qt::AutoConnection
            );
            

            Asking a question about code? http://eel.is/iso-c++/testcase/

            1 Reply Last reply Reply Quote 3
            • JonB
              JonB @U7Development last edited by

              @U7Development
              Maybe now is the time to move over from your SIGNAL/SLOT() macros to New Signal Slot Syntax ?

              1 Reply Last reply Reply Quote 3
              • Christian Ehrlicher
                Christian Ehrlicher Lifetime Qt Champion last edited by

                @JonB said in setup my own abstract class functions.. (about connect and slots):

                Maybe now is the time to move over from your SIGNAL/SLOT() macros to New Signal Slot Syntax ?

                But this makes it even harder for him because then it's a PMF which has to be passed to the function. And as you can see there are currently problems passing a simple const char * ... :)

                Qt has to stay free or it will die.

                JonB 1 Reply Last reply Reply Quote 1
                • JonB
                  JonB @Christian Ehrlicher last edited by JonB

                  @Christian-Ehrlicher
                  I don't care! It's a function pointer. If you & he want to keep working on managing your slot connections via an untyped, unparsed arbitrary string that's up to you :) In this forum I have seen people pass an utterly rubbish string for the SLOT() (or SIGNAL()) parameter, and then wonder why it doesn't work... I'm surprised at you! ;-)

                  Poor English 1 Reply Last reply Reply Quote 2
                  • Poor English
                    Poor English @JonB last edited by

                    @JonB
                    After reading your advice on the problem,I checked the new usage of the signal slot you mentioned on the Internet and tried to learn it,then I used 【connect(btn,&QPushButton::XXX, xx, &XXX::XXX)】this from replaced the previous【SIGNAL】usage,and the program is still robust as before。Although I don't know the working principle of the signal slot and the specific differences between the two,the new usage is more convenient from the writing point of view,so I will follow the guidance you provide and try to use 【&】not【SIGNAL】,thank you!

                    I am sorry about my poor English!

                    mrjj 1 Reply Last reply Reply Quote 1
                    • mrjj
                      mrjj Lifetime Qt Champion @Poor English last edited by mrjj

                      @Poor-English

                      Hi
                      The main difference between the old and new syntax is runtime lookup of the given slot/signal name versus
                      compile-time verification of the given class method pointer.

                      the SIGNAL and SLOTS macros will accept any string at compile-time and first fail at runtime

                      The "new" & syntax is evaluated at compile-time and is type-safe, meaning
                      it will complain about missing signals or slot or even parameter mismatch.

                      Poster could do like

                      (in .h)
                      class MainWindow;
                      using CALL = void (MainWindow::*)(void); // function pointer to (named)  class memeber
                      (in .cpp) 
                      
                      QPushButton *MainWindow::setup_button(QString _txt, QString _style, QRect _geo, CALL _param)
                      {
                          QPushButton *o = new QPushButton(_txt);
                          o->setStyleSheet(_style);
                          o->setGeometry(_geo);
                          connect(o, &QPushButton::clicked, this, _param);
                          return o;
                      }
                      
                      using:
                        auto b = setup_button( "Test", "", QRect(0, 10, 100, 100), &MainWindow::DoSomething );
                      

                      To use new syntax, BUT this case is easy as he wants to use just clicked()
                      With another type of widgets and with different parameters, it becomes quite the template show.

                      Poor English 1 Reply Last reply Reply Quote 3
                      • Poor English
                        Poor English @mrjj last edited by

                        @mrjj
                        Oh yeah。。。thank you!!
                        you have deepened my understanding of the new usage of signal slot,finding errors at compile time is better than finding errors at runtime!it seens it is time to say goodbye with【SIGNAL】。。。I meet Qt from a book,in that book,it used all 【SIGNAL】,now it seems that the book should be quite old。

                        I am sorry about my poor English!

                        mrjj 1 Reply Last reply Reply Quote 2
                        • mrjj
                          mrjj Lifetime Qt Champion @Poor English last edited by mrjj

                          @Poor-English
                          Yes for bigger apps and more than one developer, catching type errors and at compile time has a huge benefit
                          as the app might have not so often used dialogs and some parameter change in other part of the app might silently fail
                          with the MACROS and first be discovered at end users.
                          With the new syntax, the first developer to recompile will see it.
                          That said, connect with MACROS does return true or false so its possible to catch connect failures, making
                          it less of a burden.

                          But the new syntax also offers new features, such as lambdas )in place, nameless function) and those can be very handy
                          for keep together compact code. So yes, you will not really look back once getting used to the new syntax.

                          1 Reply Last reply Reply Quote 3
                          • U
                            U7Development last edited by

                            I have tried const char pointer with no success but i guess i was in a mistake using connect argument

                            const chart _param* used as:

                            connect(btn, SIGNAL(clicked()), this, SLOT(_param));
                            

                            now i will change to :

                            connect(btn, SIGNAL(clicked()), this, _param);
                            

                            Regarding to new connect parameter format, its time to check it out.
                            Thanks!

                            JonB 1 Reply Last reply Reply Quote 0
                            • JonB
                              JonB @U7Development last edited by

                              @U7Development
                              Yes, to match your

                              connect(o, SIGNAL(clicked()), this, _param);
                              

                              you wanted to know what UnknownParam _param should be in setup_button() definition, and that should indeed be const char *_param to do things this way. The caller will pass SLOT(whatever), you won't use SLOT() in your code in your setup_button() code here.

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post