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?
QtWS25 Last Chance

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.1k Views
  • 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.
  • C Offline
    C Offline
    ChristianMontero
    wrote on 1 Oct 2017, 05:11 last edited by
    #1

    Hi, Good Nigth!
    I'm newie in QT and I need to create a GUI with an array of buttons, for example 8 buttons which represents the bits of a byte. When the buttons are created they are initialized with the text "0" and I need to change the text of the n-th button to "1" when is clicked. I think that the problem is the signal, I need to add one parameter to the SIGNAL(clicke()) but I haven't understand how I can do this, this is my code, thanks for your time! Any help would be appreciated:

    In my header I have the following code:
    class QPushButton;
    class Window : public QWidget
    {
    Q_OBJECT
    public:
    explicit Window(QWidget *parent = nullptr);

    private:
    QPushButton *m_button[54];
    int i, j = 50;

    signals:

    public slots:
    void slotButtonClicked(bool checked, int i);
    };

    #endif // WINDOW_H

    and in the cpp file I have this:
    Window::Window(QWidget *parent) : QWidget(parent)
    {
    /Some variables/

    /*Set size of the window*/
    setFixedSize(1300,400);
    
    /*Create and position of the buttons*/
    for(i = 0; i<54; i++){
        m_button[i] = new QPushButton("0",this);
        m_button[i]->setGeometry(100+(20*i),j,20,20);
        m_button[i]->setCheckable(true);
        /*Do the connection*/
        connect(m_button[i], SIGNAL(clicked(bool)), this, SLOT(slotButtonClicked(bool, int i)));
    }
    

    }

    void Window::slotButtonClicked(bool checked, int idx){
    if(checked){
    m_button[idx]->setText("1");
    }else{
    m_button[idx]->setText("0");
    }
    }

    A 1 Reply Last reply 1 Oct 2017, 05:38
    0
    • C ChristianMontero
      1 Oct 2017, 05:11

      Hi, Good Nigth!
      I'm newie in QT and I need to create a GUI with an array of buttons, for example 8 buttons which represents the bits of a byte. When the buttons are created they are initialized with the text "0" and I need to change the text of the n-th button to "1" when is clicked. I think that the problem is the signal, I need to add one parameter to the SIGNAL(clicke()) but I haven't understand how I can do this, this is my code, thanks for your time! Any help would be appreciated:

      In my header I have the following code:
      class QPushButton;
      class Window : public QWidget
      {
      Q_OBJECT
      public:
      explicit Window(QWidget *parent = nullptr);

      private:
      QPushButton *m_button[54];
      int i, j = 50;

      signals:

      public slots:
      void slotButtonClicked(bool checked, int i);
      };

      #endif // WINDOW_H

      and in the cpp file I have this:
      Window::Window(QWidget *parent) : QWidget(parent)
      {
      /Some variables/

      /*Set size of the window*/
      setFixedSize(1300,400);
      
      /*Create and position of the buttons*/
      for(i = 0; i<54; i++){
          m_button[i] = new QPushButton("0",this);
          m_button[i]->setGeometry(100+(20*i),j,20,20);
          m_button[i]->setCheckable(true);
          /*Do the connection*/
          connect(m_button[i], SIGNAL(clicked(bool)), this, SLOT(slotButtonClicked(bool, int i)));
      }
      

      }

      void Window::slotButtonClicked(bool checked, int idx){
      if(checked){
      m_button[idx]->setText("1");
      }else{
      m_button[idx]->setText("0");
      }
      }

      A Offline
      A Offline
      ambershark
      wrote on 1 Oct 2017, 05:38 last edited by ambershark 10 Jan 2017, 05:44
      #2

      @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");
      }
      

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

      C 1 Reply Last reply 1 Oct 2017, 05:43
      1
      • A ambershark
        1 Oct 2017, 05:38

        @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");
        }
        
        C Offline
        C Offline
        ChristianMontero
        wrote on 1 Oct 2017, 05:43 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 1 Oct 2017, 05:44
        0
        • C ChristianMontero
          1 Oct 2017, 05:43

          @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 1 Oct 2017, 05:44 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

          C 2 Replies Last reply 1 Oct 2017, 05:50
          1
          • A ambershark
            1 Oct 2017, 05:44

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

            C Offline
            C Offline
            ChristianMontero
            wrote on 1 Oct 2017, 05:50 last edited by
            #5

            @ambershark Thanks a lot, let me try it!

            1 Reply Last reply
            1
            • A ambershark
              1 Oct 2017, 05:44

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

              C Offline
              C Offline
              ChristianMontero
              wrote on 1 Oct 2017, 06:09 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 1 Oct 2017, 06:10
              0
              • C ChristianMontero
                1 Oct 2017, 06:09

                @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 1 Oct 2017, 06:10 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

                C 1 Reply Last reply 1 Oct 2017, 06:21
                0
                • A ambershark
                  1 Oct 2017, 06:10

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

                  C Offline
                  C Offline
                  ChristianMontero
                  wrote on 1 Oct 2017, 06:21 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 1 Oct 2017, 06:25
                  0
                  • C ChristianMontero
                    1 Oct 2017, 06:21

                    @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 1 Oct 2017, 06:25 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 1 Oct 2017, 06:33
                    0
                    • A ambershark
                      1 Oct 2017, 06:25

                      @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 1 Oct 2017, 06:33 last edited by ambershark 10 Jan 2017, 06:34
                      #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

                      C 1 Reply Last reply 1 Oct 2017, 06:53
                      1
                      • C ChristianMontero
                        1 Oct 2017, 06:21

                        @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 1 Oct 2017, 06:43 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
                          1 Oct 2017, 06:33

                          @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
                          }
                          
                          
                          C Offline
                          C Offline
                          ChristianMontero
                          wrote on 1 Oct 2017, 06:53 last edited by ChristianMontero 10 Jan 2017, 06:55
                          #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 1 Oct 2017, 07:04 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 C 2 Replies Last reply 1 Oct 2017, 07:07
                            1
                            • aha_1980A aha_1980
                              1 Oct 2017, 07:04

                              @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 1 Oct 2017, 07:07 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

                              C 1 Reply Last reply 1 Oct 2017, 07:10
                              1
                              • aha_1980A aha_1980
                                1 Oct 2017, 07:04

                                @ambershark @ChristianMontero

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

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

                                C Offline
                                C Offline
                                ChristianMontero
                                wrote on 1 Oct 2017, 07:10 last edited by
                                #15

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

                                1 Reply Last reply
                                0
                                • A ambershark
                                  1 Oct 2017, 07:07

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

                                  C Offline
                                  C Offline
                                  ChristianMontero
                                  wrote on 1 Oct 2017, 07:10 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 1 Oct 2017, 07:11 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 9 Jun 2023, 20:54 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 10 Jun 2023, 18:44
                                      0
                                      • I illud
                                        9 Jun 2023, 20:54

                                        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 10 Jun 2023, 18:44 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