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