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 add one parameter to the clicked SIGNAL?
Forum Updated to NodeBB v4.3 + New Features

How to add one parameter to the clicked SIGNAL?

Scheduled Pinned Locked Moved Solved General and Desktop
slotssignal & slotsignalevent-handlingarray
19 Posts 5 Posters 22.5k 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.
  • A ambershark

    @ChristianMontero You can't change the definition like that. So you won't be able to just add a variable, but what you can do is inside your clicked handler you can check the state of the button.

    Another option is to derive a class from QPushButton and emit a separate signal with the bool, int that you want. Inside the new class you would get the clicked signal and then emit a clicked(bool, int) which you could then capture at the Window level as normal.

    Edit: I realized you may need a bit of code to reference, here's how you can check the state of a button in a normal clicked() function:

    void Window::clicked()
    {
       auto button = qobject_cast<QPushButton *>(sender());
       Q_ASSERT(button);
       if (button->text() == "0")
          button->setText("1")
       else 
           button->setText("0");
    }
    
    ChristianMonteroC Offline
    ChristianMonteroC Offline
    ChristianMontero
    wrote on last edited by
    #3

    @ambershark Thank you for your time, just another thing please
    how can I check the state of the particular button of the array if I can't send the number of the element as a parameter?
    again, thanks a lot!

    A 1 Reply Last reply
    0
    • ChristianMonteroC ChristianMontero

      @ambershark Thank you for your time, just another thing please
      how can I check the state of the particular button of the array if I can't send the number of the element as a parameter?
      again, thanks a lot!

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #4

      @ChristianMontero Hey sorry, was in the middle of editing my previous post, check above for a code example. :)

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      ChristianMonteroC 2 Replies Last reply
      1
      • A ambershark

        @ChristianMontero Hey sorry, was in the middle of editing my previous post, check above for a code example. :)

        ChristianMonteroC Offline
        ChristianMonteroC Offline
        ChristianMontero
        wrote on last edited by
        #5

        @ambershark Thanks a lot, let me try it!

        1 Reply Last reply
        1
        • A ambershark

          @ChristianMontero Hey sorry, was in the middle of editing my previous post, check above for a code example. :)

          ChristianMonteroC Offline
          ChristianMonteroC Offline
          ChristianMontero
          wrote on last edited by
          #6

          @ambershark Thanks a lot! it works!
          have a great day/nigth I don't know where are you writing me!
          you are so helpful!

          A 1 Reply Last reply
          0
          • ChristianMonteroC ChristianMontero

            @ambershark Thanks a lot! it works!
            have a great day/nigth I don't know where are you writing me!
            you are so helpful!

            A Offline
            A Offline
            ambershark
            wrote on last edited by
            #7

            @ChristianMontero Night for me, and you're welcome, happy to help. :)

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            ChristianMonteroC 1 Reply Last reply
            0
            • A ambershark

              @ChristianMontero Night for me, and you're welcome, happy to help. :)

              ChristianMonteroC Offline
              ChristianMonteroC Offline
              ChristianMontero
              wrote on last edited by
              #8

              @ambershark hey one last thing :/ where could I read to understand the second option that you gave me, I mean to emit a different Clicked signal, because for the purpose of the application the next step it's to pass the text of each button to a string and I have to acces to every button as an element of an array, I hope not to bother you anymore, just need some reference, you think that the QT Documentation can help me on this, or would you recommend me somewhere else to read?

              A 2 Replies Last reply
              0
              • ChristianMonteroC ChristianMontero

                @ambershark hey one last thing :/ where could I read to understand the second option that you gave me, I mean to emit a different Clicked signal, because for the purpose of the application the next step it's to pass the text of each button to a string and I have to acces to every button as an element of an array, I hope not to bother you anymore, just need some reference, you think that the QT Documentation can help me on this, or would you recommend me somewhere else to read?

                A Offline
                A Offline
                ambershark
                wrote on last edited by
                #9

                @ChristianMontero It's no bother ... give me a sec and I'll type up an example for you. I'm not sure of a good place to point you for docs on it though.

                It's basically just deriving a class and setting your own signals.

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                A 1 Reply Last reply
                0
                • A ambershark

                  @ChristianMontero It's no bother ... give me a sec and I'll type up an example for you. I'm not sure of a good place to point you for docs on it though.

                  It's basically just deriving a class and setting your own signals.

                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by ambershark
                  #10

                  @ambershark

                  Here ya go... there may be errors I didn't compile or anything.

                  MyButton.h file:

                  #pragma once
                  
                  #include <QPushButton>
                  
                  class MyButton : public QPushButton
                  {
                      Q_OBJECT
                  
                  public:
                      MyButton(QWidget *parent = 0) : QWidget(parent), someBool_(false), someInt_(0)
                      {
                          connect(this, SIGNAL(clicked()), this, SLOT(handleClick()));
                      }
                  
                  signals:
                      void buttonClicked(bool, int);
                  
                  private slots:
                      void handleClick()
                      {
                          emit buttonClicked(someBool_, someInt_);
                      }
                  
                  private:
                      bool someBool_;
                      int someInt_;
                  };
                  

                  Some window.cpp somewhere:

                  // somewhere else ... i.e. Window
                  // ...
                  {
                      connect(instanceOfMyButton, SIGNAL(buttonClicked(bool, int)), this, SLOT(myHandler(b
                  }
                  
                  void Window::myHandler(bool b, int i)
                  {
                      // do whatever you want with b/i
                  }
                  
                  

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  ChristianMonteroC 1 Reply Last reply
                  1
                  • ChristianMonteroC ChristianMontero

                    @ambershark hey one last thing :/ where could I read to understand the second option that you gave me, I mean to emit a different Clicked signal, because for the purpose of the application the next step it's to pass the text of each button to a string and I have to acces to every button as an element of an array, I hope not to bother you anymore, just need some reference, you think that the QT Documentation can help me on this, or would you recommend me somewhere else to read?

                    A Offline
                    A Offline
                    ambershark
                    wrote on last edited by
                    #11

                    @ChristianMontero

                    @ChristianMontero said in How to add one parameter to the clicked SIGNAL?:

                    @ambershark hey one last thing :/ where could I read to understand the second option that you gave me, I mean to emit a different Clicked signal, because for the purpose of the application the next step it's to pass the text of each button to a string and I have to acces to every button as an element of an array, I hope not to bother you anymore, just need some reference, you think that the QT Documentation can help me on this, or would you recommend me somewhere else to read?

                    If you explain this a bit more I can help you design it better perhaps.. I don't quite understand what you're trying to do. I wrote the example above to show you what I was talking about making a custom derived button, but it may not be a good solution for what you want. Or at least there may be a more elegant or easier one.

                    My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                    1 Reply Last reply
                    0
                    • A ambershark

                      @ambershark

                      Here ya go... there may be errors I didn't compile or anything.

                      MyButton.h file:

                      #pragma once
                      
                      #include <QPushButton>
                      
                      class MyButton : public QPushButton
                      {
                          Q_OBJECT
                      
                      public:
                          MyButton(QWidget *parent = 0) : QWidget(parent), someBool_(false), someInt_(0)
                          {
                              connect(this, SIGNAL(clicked()), this, SLOT(handleClick()));
                          }
                      
                      signals:
                          void buttonClicked(bool, int);
                      
                      private slots:
                          void handleClick()
                          {
                              emit buttonClicked(someBool_, someInt_);
                          }
                      
                      private:
                          bool someBool_;
                          int someInt_;
                      };
                      

                      Some window.cpp somewhere:

                      // somewhere else ... i.e. Window
                      // ...
                      {
                          connect(instanceOfMyButton, SIGNAL(buttonClicked(bool, int)), this, SLOT(myHandler(b
                      }
                      
                      void Window::myHandler(bool b, int i)
                      {
                          // do whatever you want with b/i
                      }
                      
                      
                      ChristianMonteroC Offline
                      ChristianMonteroC Offline
                      ChristianMontero
                      wrote on last edited by ChristianMontero
                      #12

                      @ambershark Thank you very much, I just stared to use QT and I was having problems, but I hope i get use to it quickly and start helping people just like you, thanks you again! take care!

                      I just read your last comment, well I have to establish a connection between 2 devices, via UDP could be two computers, cellphones or microcontrollers, then in my GUI I need two sections, the first one is an array of labels, where i will show the data that i will recieve from de UDP connection, and the second section is an array of buttons, where the user will click to change the value of the bits, and then I'll have to pass the value of the buttons to the string which I will send via UDP as response to the information I recieved. And I have to use labels and buttons because they're requirements of the client.

                      I hope to be specific enough.

                      And thanks again.

                      1 Reply Last reply
                      1
                      • aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @ambershark @ChristianMontero

                        I think QSignalMapper [1] was written for exactly this problem :)

                        [1] http://doc.qt.io/qt-5/qsignalmapper.html

                        Qt has to stay free or it will die.

                        A ChristianMonteroC 2 Replies Last reply
                        1
                        • aha_1980A aha_1980

                          @ambershark @ChristianMontero

                          I think QSignalMapper [1] was written for exactly this problem :)

                          [1] http://doc.qt.io/qt-5/qsignalmapper.html

                          A Offline
                          A Offline
                          ambershark
                          wrote on last edited by
                          #14

                          @aha_1980 That sure does do what he needs, lol. I didn't know about QSignalMapper.. It's funny that even after 16+ years of Qt there's still classes I don't know about. :)

                          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                          ChristianMonteroC 1 Reply Last reply
                          1
                          • aha_1980A aha_1980

                            @ambershark @ChristianMontero

                            I think QSignalMapper [1] was written for exactly this problem :)

                            [1] http://doc.qt.io/qt-5/qsignalmapper.html

                            ChristianMonteroC Offline
                            ChristianMonteroC Offline
                            ChristianMontero
                            wrote on last edited by
                            #15

                            @aha_1980 Thanks you so much, I'll check the link!

                            1 Reply Last reply
                            0
                            • A ambershark

                              @aha_1980 That sure does do what he needs, lol. I didn't know about QSignalMapper.. It's funny that even after 16+ years of Qt there's still classes I don't know about. :)

                              ChristianMonteroC Offline
                              ChristianMonteroC Offline
                              ChristianMontero
                              wrote on last edited by
                              #16

                              @ambershark Thank you, you're so helpful :)

                              1 Reply Last reply
                              1
                              • aha_1980A Offline
                                aha_1980A Offline
                                aha_1980
                                Lifetime Qt Champion
                                wrote on last edited by
                                #17

                                @ambershark said in How to add one parameter to the clicked SIGNAL?:

                                @aha_1980 That sure does do what he needs, lol. I didn't know about QSignalMapper.. It's funny that even after 16+ years of Qt there's still classes I don't know about. :)

                                I only knew it because I had the same requirement as @ChristianMontero some time ago :)

                                Qt has to stay free or it will die.

                                1 Reply Last reply
                                2
                                • I Offline
                                  I Offline
                                  illud
                                  wrote on last edited by
                                  #18

                                  Hello @ChristianMontero hope you ok,

                                  Just implement a QPushButton::clicked() slot that is connected to multiple push buttons. Inside that cast sender() to QPushButton* to access the button and identify it. You can then emit a user defined signal with additional parameters.

                                  Another method would be using a C++11 lambda:

                                  int counter = 1;
                                  
                                  // Create button or access it using the ui member 
                                  QPushButton *button = new QPushButton; //In your case is m_button
                                  
                                  button->setProperty("myId", counter++); // in your case counter++ should be m_button[i]
                                  connect(button, &QPushButton::clicked, [this, button](){
                                     //here you call your function pasing parameters
                                    slotButtonClicked(true, button->property("myId").toInt());
                                  });
                                  

                                  I took this from here https://www.codeproject.com/Questions/1257831/Qt-custom-slot-sending-an-integer

                                  Pl45m4P 1 Reply Last reply
                                  0
                                  • I illud

                                    Hello @ChristianMontero hope you ok,

                                    Just implement a QPushButton::clicked() slot that is connected to multiple push buttons. Inside that cast sender() to QPushButton* to access the button and identify it. You can then emit a user defined signal with additional parameters.

                                    Another method would be using a C++11 lambda:

                                    int counter = 1;
                                    
                                    // Create button or access it using the ui member 
                                    QPushButton *button = new QPushButton; //In your case is m_button
                                    
                                    button->setProperty("myId", counter++); // in your case counter++ should be m_button[i]
                                    connect(button, &QPushButton::clicked, [this, button](){
                                       //here you call your function pasing parameters
                                      slotButtonClicked(true, button->property("myId").toInt());
                                    });
                                    

                                    I took this from here https://www.codeproject.com/Questions/1257831/Qt-custom-slot-sending-an-integer

                                    Pl45m4P Offline
                                    Pl45m4P Offline
                                    Pl45m4
                                    wrote on last edited by
                                    #19

                                    @illud

                                    Do you think after 6 years, he's still looking for a solution? ;-)
                                    The topic is marked "solved" anyway :-)


                                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                    ~E. W. Dijkstra

                                    1 Reply Last reply
                                    1

                                    • Login

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