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. How to refer to the button inside of her signal function?

How to refer to the button inside of her signal function?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 810 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.
  • N Offline
    N Offline
    n34rt
    wrote on last edited by n34rt
    #1
    Main::Main(QWidget *parent) : QMainWindow(parent)
    {
        ui.setupUi(this);
    
        return;
    }
    
    void Main::on_toolButton_released()
    {
        // this->
        return;
    }
    

    In this example this-> refers to the class Main, how do I refer to the button with belongs to the function _released?

    I mean, there's another keyword than ui.<buttonanme> ?
    in this case ui.toolButton

    1 Reply Last reply
    0
    • N n34rt

      @JonB said in How to refer to the button 'obj' inside of her signal function?:

      QPushButton *btn = qobject_cast<QPushButton *>( sender() );
      QLabel *lbl = qobject_cast<QLabel *>( sender() );
      if (btn) ...
      else if (lbl) ...
      else if ....

      Interesting, suppose the sender() is a QLabel* and I write
      QPushButton *btn = qobject_cast<QPushButton *>( sender() );
      its an invalid cast? won't cause the program to crash?
      Isn't it a 'bad practice' to do such a thing?

      If not, inside of a if else, possible to check the button type in a switch?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #9

      @n34rt

      Hi
      qobject_cast will return nullptr when the cast is not possible so its safe to do so IF you check the variable before use.

      so its not like a c type cast that can crash the program if wrong.

      but one must check the variable before use :)

      N 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #2

        Hi

        ui.<buttonanme> is they way to address widgets you made via Designer.
        For widgets you dynamically create, then only the fucntion findchildren or you can store the pointers (like UI does) and
        use them in that way.

        Sorry totally got something else from question.

        You are asking how to know the button that was clicked / released.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          n34rt
          wrote on last edited by
          #3

          @mrjj said in How to refer to the button 'obj' inside of her signal function?:

          For widgets you dynamically create, then only the fucntion findchildren or you can store the pointers (like UI does) and
          use them in that way.

          Could you give an example, please?

          mrjjM 1 Reply Last reply
          0
          • N n34rt

            @mrjj said in How to refer to the button 'obj' inside of her signal function?:

            For widgets you dynamically create, then only the fucntion findchildren or you can store the pointers (like UI does) and
            use them in that way.

            Could you give an example, please?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #4

            @n34rt
            Sorry . just realized i misread you question.

            you can use sender() to know which button

            void Main::on_toolButton_released()  {
            
             QPushButton *btn = qobject_cast<QPushButton *>( sender() );
             if (bnt) {
             // use it
             }
            }
            
            

            Also just for information. you can also use lambdas as slot and capture the button (also for widgets in UI)

             QPushButton* loadTextFileButton = new QPushButton("load");
             connect(loadTextFileButton, &QPushButton::clicked, [loadTextFileButton]()
             {
                 qDebug()<<"clicked";
                loadTextFileButton->xxxxxx 
             });
            
            N 1 Reply Last reply
            0
            • mrjjM mrjj

              @n34rt
              Sorry . just realized i misread you question.

              you can use sender() to know which button

              void Main::on_toolButton_released()  {
              
               QPushButton *btn = qobject_cast<QPushButton *>( sender() );
               if (bnt) {
               // use it
               }
              }
              
              

              Also just for information. you can also use lambdas as slot and capture the button (also for widgets in UI)

               QPushButton* loadTextFileButton = new QPushButton("load");
               connect(loadTextFileButton, &QPushButton::clicked, [loadTextFileButton]()
               {
                   qDebug()<<"clicked";
                  loadTextFileButton->xxxxxx 
               });
              
              N Offline
              N Offline
              n34rt
              wrote on last edited by
              #5

              @mrjj thank you, this is what I was looking for.

              Is it possible to get the *btn object without declaring the button 'type' QPushButton?

              I mean something like: auto *btn = qobject_cast<??>( sender() );
              where ?? is something containing the button type.

              mrjjM 1 Reply Last reply
              0
              • N n34rt

                @mrjj thank you, this is what I was looking for.

                Is it possible to get the *btn object without declaring the button 'type' QPushButton?

                I mean something like: auto *btn = qobject_cast<??>( sender() );
                where ?? is something containing the button type.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @n34rt said in How to refer to the button inside of her signal function?:

                @mrjj thank you, this is what I was looking for.

                Is it possible to get the *btn object without declaring the button 'type' QPushButton?

                I mean something like: auto *btn = qobject_cast<??>( sender() );
                where ?? is something containing the button type.

                well the ?? could have to be the real type or else compiler would be mad but
                what do you need it for ?
                If you have mixed Toolbuttons and pushbuttons we could make a helper func to get it but
                I need to understand the use case. :)

                JonBJ 1 Reply Last reply
                0
                • mrjjM mrjj

                  @n34rt said in How to refer to the button inside of her signal function?:

                  @mrjj thank you, this is what I was looking for.

                  Is it possible to get the *btn object without declaring the button 'type' QPushButton?

                  I mean something like: auto *btn = qobject_cast<??>( sender() );
                  where ?? is something containing the button type.

                  well the ?? could have to be the real type or else compiler would be mad but
                  what do you need it for ?
                  If you have mixed Toolbuttons and pushbuttons we could make a helper func to get it but
                  I need to understand the use case. :)

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @mrjj said in How to refer to the button inside of her signal function?:

                  or else compiler would be mad

                  I love the way you phrase things :)

                  @n34rt said in How to refer to the button inside of her signal function?:

                  I mean something like: auto *btn = qobject_cast<??>( sender() );
                  where ?? is something containing the button type.

                  No, you can't achieve what you are aiming at like this. You need an actual type name for the ?? which you are asking C++ to actually cast to. And once you put that there you may as well equally replace the auto with that, that's all auto does for you, it's not "magic" in any way.

                  If you are asking: what can I do if the sender() might be either a QPushButton or (say) a QLabel and I need to know which for my following code, just put in multiple separate casts:

                  QPushButton *btn = qobject_cast<QPushButton *>( sender() );
                  QLabel *lbl = qobject_cast<QLabel *>( sender() );
                  if (btn) ...
                  else if (lbl) ...
                  else if ....
                  
                  N 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @mrjj said in How to refer to the button inside of her signal function?:

                    or else compiler would be mad

                    I love the way you phrase things :)

                    @n34rt said in How to refer to the button inside of her signal function?:

                    I mean something like: auto *btn = qobject_cast<??>( sender() );
                    where ?? is something containing the button type.

                    No, you can't achieve what you are aiming at like this. You need an actual type name for the ?? which you are asking C++ to actually cast to. And once you put that there you may as well equally replace the auto with that, that's all auto does for you, it's not "magic" in any way.

                    If you are asking: what can I do if the sender() might be either a QPushButton or (say) a QLabel and I need to know which for my following code, just put in multiple separate casts:

                    QPushButton *btn = qobject_cast<QPushButton *>( sender() );
                    QLabel *lbl = qobject_cast<QLabel *>( sender() );
                    if (btn) ...
                    else if (lbl) ...
                    else if ....
                    
                    N Offline
                    N Offline
                    n34rt
                    wrote on last edited by
                    #8

                    @JonB said in How to refer to the button 'obj' inside of her signal function?:

                    QPushButton *btn = qobject_cast<QPushButton *>( sender() );
                    QLabel *lbl = qobject_cast<QLabel *>( sender() );
                    if (btn) ...
                    else if (lbl) ...
                    else if ....

                    Interesting, suppose the sender() is a QLabel* and I write
                    QPushButton *btn = qobject_cast<QPushButton *>( sender() );
                    its an invalid cast? won't cause the program to crash?
                    Isn't it a 'bad practice' to do such a thing?

                    If not, inside of a if else, possible to check the button type in a switch?

                    mrjjM 1 Reply Last reply
                    0
                    • N n34rt

                      @JonB said in How to refer to the button 'obj' inside of her signal function?:

                      QPushButton *btn = qobject_cast<QPushButton *>( sender() );
                      QLabel *lbl = qobject_cast<QLabel *>( sender() );
                      if (btn) ...
                      else if (lbl) ...
                      else if ....

                      Interesting, suppose the sender() is a QLabel* and I write
                      QPushButton *btn = qobject_cast<QPushButton *>( sender() );
                      its an invalid cast? won't cause the program to crash?
                      Isn't it a 'bad practice' to do such a thing?

                      If not, inside of a if else, possible to check the button type in a switch?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @n34rt

                      Hi
                      qobject_cast will return nullptr when the cast is not possible so its safe to do so IF you check the variable before use.

                      so its not like a c type cast that can crash the program if wrong.

                      but one must check the variable before use :)

                      N 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @n34rt

                        Hi
                        qobject_cast will return nullptr when the cast is not possible so its safe to do so IF you check the variable before use.

                        so its not like a c type cast that can crash the program if wrong.

                        but one must check the variable before use :)

                        N Offline
                        N Offline
                        n34rt
                        wrote on last edited by
                        #10

                        @mrjj

                        QPushButton *btn = qobject_cast<QPushButton *>( sender() );
                        

                        Is It possible to get the signal type from the *btn? I mean in if its Button_released Button_pressed etc?

                        JonBJ Paul ColbyP 3 Replies Last reply
                        0
                        • N n34rt

                          @mrjj

                          QPushButton *btn = qobject_cast<QPushButton *>( sender() );
                          

                          Is It possible to get the signal type from the *btn? I mean in if its Button_released Button_pressed etc?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #11

                          @n34rt
                          No. You will put this code in a slot for pressed or for released, so you will know.

                          1 Reply Last reply
                          1
                          • N n34rt

                            @mrjj

                            QPushButton *btn = qobject_cast<QPushButton *>( sender() );
                            

                            Is It possible to get the signal type from the *btn? I mean in if its Button_released Button_pressed etc?

                            Paul ColbyP Offline
                            Paul ColbyP Offline
                            Paul Colby
                            wrote on last edited by
                            #12

                            You need an actual type name for the ?? which you are asking C++ to actually cast to.

                            While yes, that is true, it's worth noting that QObject::sender() returns QObject *, which means that you can introspect the sender, without needing to first cast to an derived type. For example,

                            // we don't know what type `sender` is, but it *must* be QObject-derived (if not nullptr).
                            QObject * const sender = sender();
                            if (sender) {
                                qDebug() << sender->metaObject()->className(); // eg "QPushButton"
                                // just for fun:
                                if (sender->metaObject()->inherits("QAbstractButton")) {
                                    // it was a button.
                                }
                            }
                            

                            Is It possible to get the signal type from the *btn? I mean in if its Button_released Button_pressed etc?

                            Yes, sort of. Have a look at QObject::senderSignalIndex(). eg:

                            QObject * const sender = sender();
                            if ((sender) && (senderSignalIndex() >= 0)) {
                                QMetaMethod method = sender->metaObject()->method(senderSignalIndex);
                                if (method.isValid()) {
                                    // inspect / do stuff with `method`.
                                }
                            }
                            

                            Be sure to read the warnings documented for QObject::senderSignalIndex() though. eg:

                            Warning: This function violates the object-oriented principle of modularity. However, getting access to the signal index might be useful when many signals are connected to a single slot.

                            I would generally avoid using QObject::senderSignalIndex(), and if I did use it, would probably only ever use it when sender() == this. Any other time would smell like a bad design.

                            Cheers.

                            1 Reply Last reply
                            1
                            • N n34rt

                              @mrjj

                              QPushButton *btn = qobject_cast<QPushButton *>( sender() );
                              

                              Is It possible to get the signal type from the *btn? I mean in if its Button_released Button_pressed etc?

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #13

                              @n34rt
                              I would avoid using sender() at all. I have never needed to. Since Qt5 accepted lambdas --- to which you can pass your own parameters --- for slots, it is preferable to pass the sender as an explicit parameter rather than trying to use sender() to refer to it. Example:

                              connect(btn, &QPushButton::clicked, this, [btn, this]() { qDebug() << btn->objectName(); this->someFunction(btn);  });
                              
                              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