0

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.

  • Where did you read that [`QButtonGroup::buttonClicked`](https://doc.qt.io/qt-6/qbuttongroup.html#buttonClicked) was obsolete? – G.M. Oct 28 '22 at 14:32
  • https://doc.qt.io/qt-5/qbuttongroup-obsolete.html in this link – 2004satwik Oct 28 '22 at 14:47
  • Sorry, I misread your post. `buttonClicked(int)` is obsolete but `buttonClicked(QAbstractButton *)` is provided instead. Will that not suffice? – G.M. Oct 28 '22 at 14:56
  • But can we get clicked button id with buttonClicked(QAbstractButton *)? if yes, how? – 2004satwik Oct 28 '22 at 15:22
  • 1
    Then simply look around in the documentation: [QButtonGroup::idClicked(int)](https://doc.qt.io/qt-6/qbuttongroup.html#idClicked) – chehrlic Oct 28 '22 at 19:01
  • @chehrlic +1 I should probably heed your advice as well: I missed that one :-) – G.M. Oct 28 '22 at 19:15

1 Answers1

1

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

G.M.
  • 12,232
  • 2
  • 15
  • 18
  • but it gives me an error in line 2: [this,group] saying that ' mainwindow.cpp:342:22: 'group' in capture list does not name a variable '. – 2004satwik Oct 29 '22 at 06:59
  • can you do this without lambda function? – 2004satwik Oct 29 '22 at 07:11
  • @2004satwik I captured `group` in the lambda because that's the variable name you'd used in the question -- you should pass a pointer to the `QGroupBox` you're using. – G.M. Oct 29 '22 at 08:08
  • great, the idClicked signal worked ! – 2004satwik Oct 29 '22 at 08:39
  • However, removing 'group' from [this, group] also worked for me, though doing so doesn't make it a lambda function. Btw i don't know much about lambda function either ;) – 2004satwik Oct 29 '22 at 08:44