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 690 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.
  • jeremy_kJ Offline
    jeremy_kJ Offline
    jeremy_k
    wrote on 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

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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

        JonBJ 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #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! ;-)

          Poor EnglishP 1 Reply Last reply
          2
          • JonBJ 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 EnglishP Offline
            Poor EnglishP Offline
            Poor English
            wrote on 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
            1
            • Poor EnglishP Poor English

              @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 last edited by mrjj
              #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.

              Poor EnglishP 1 Reply Last reply
              3
              • mrjjM 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 EnglishP Offline
                Poor EnglishP Offline
                Poor English
                wrote on 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
                2
                • Poor EnglishP Poor English

                  @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 last edited by mrjj
                  #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 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!

                    JonBJ 1 Reply Last reply
                    0
                    • U U7Development

                      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!

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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

                      • Login

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