Search code examples
c++qtsignals-slotsqt6qbuttongroup

How to get a clicked button id from QButtonGroup in qt 6.4 through signal and slot connection


i am new to qt and what to know how to get the id of the button that is clicked in qt through signal and slot.

connect(group, SIGNAL(buttonClicked(int)), this, SLOT(buttonWasClicked(int)));

This was the earlier syntax to get the id, but qt has declared buttonClicked(int) as obsolete and it no longer allows us use it. is there any new replacement for this code.

Plz forgive if this question was silly, but i don't know much about qt yet.


Solution

  • The QButtonGroup::buttonClicked(int) signal is obsoleted but you can still use QButtonGroup:: buttonClicked(QAbstractButton *). Perhaps use it in conjunction with a lambda and your existing buttonWasClicked slot...

    connect(group, &QButtonGroup::buttonClicked,
            [this, group](QAbstractButton *button)
              {
                buttonWasClicked(group->id(button));
              });
    

    Alternatively, use the idClicked signal as suggested by @chehrlic...

    connect(group, &QButtonGroup::idClicked, this, &MainWindow::buttonWasClicked);
    

    (Assuming MainWindow is the type pointed to by this.)