How to add one parameter to the clicked SIGNAL?



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


  • Moderators

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


  • @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!


  • Moderators

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



  • @ambershark Thanks a lot, let me try it!



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


  • Moderators

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



  • @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?


  • Moderators

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


  • Moderators

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

  • Moderators

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



  • @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.


  • Qt Champions 2018

    @ambershark @ChristianMontero

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

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


  • Moderators

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



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



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


  • Qt Champions 2018

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