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. setup my own abstract class functions.. (about connect and slots)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 7 Posters 805 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.
  • U Offline
    U Offline
    U7Development
    wrote on 7 Feb 2021, 03:58 last edited by U7Development 2 Jul 2021, 04:00
    #1

    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 J 2 Replies Last reply 7 Feb 2021, 05:32
    1
    • U U7Development
      7 Feb 2021, 03:58

      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 Offline
      B Offline
      Bonnie
      wrote on 7 Feb 2021, 05:32 last edited by Bonnie 2 Jul 2021, 05:32
      #2

      const char *

      1 Reply Last reply
      5
      • P Offline
        P Offline
        Poor English
        wrote on 7 Feb 2021, 05:54 last edited by
        #3

        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
        1
        • J Offline
          J Offline
          jeremy_k
          wrote on 7 Feb 2021, 05:55 last edited by
          #4

          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
          3
          • U U7Development
            7 Feb 2021, 03:58

            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

            J Offline
            J Offline
            JonB
            wrote on 7 Feb 2021, 08:45 last edited by
            #5

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

            1 Reply Last reply
            3
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 7 Feb 2021, 08:47 last edited by
              #6

              @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              J 1 Reply Last reply 7 Feb 2021, 08:48
              1
              • C Christian Ehrlicher
                7 Feb 2021, 08:47

                @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 * ... :)

                J Offline
                J Offline
                JonB
                wrote on 7 Feb 2021, 08:48 last edited by JonB 2 Jul 2021, 09:03
                #7

                @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! ;-)

                P 1 Reply Last reply 7 Feb 2021, 09:25
                2
                • J JonB
                  7 Feb 2021, 08:48

                  @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! ;-)

                  P Offline
                  P Offline
                  Poor English
                  wrote on 7 Feb 2021, 09:25 last edited by
                  #8

                  @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!

                  mrjjM 1 Reply Last reply 7 Feb 2021, 09:43
                  1
                  • P Poor English
                    7 Feb 2021, 09:25

                    @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!

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 7 Feb 2021, 09:43 last edited by mrjj 2 Jul 2021, 09:59
                    #9

                    @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.

                    P 1 Reply Last reply 7 Feb 2021, 10:11
                    3
                    • mrjjM mrjj
                      7 Feb 2021, 09:43

                      @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.

                      P Offline
                      P Offline
                      Poor English
                      wrote on 7 Feb 2021, 10:11 last edited by
                      #10

                      @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!

                      mrjjM 1 Reply Last reply 7 Feb 2021, 10:23
                      2
                      • P Poor English
                        7 Feb 2021, 10:11

                        @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。

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 7 Feb 2021, 10:23 last edited by mrjj 2 Jul 2021, 10:24
                        #11

                        @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
                        3
                        • U Offline
                          U Offline
                          U7Development
                          wrote on 7 Feb 2021, 14:59 last edited by
                          #12

                          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!

                          J 1 Reply Last reply 7 Feb 2021, 15:05
                          0
                          • U U7Development
                            7 Feb 2021, 14:59

                            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!

                            J Offline
                            J Offline
                            JonB
                            wrote on 7 Feb 2021, 15:05 last edited by
                            #13

                            @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
                            0

                            1/13

                            7 Feb 2021, 03:58

                            • Login

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