Search code examples
c++qtradio-buttongroupbox

How to get value from radiobutton in qt?


I have lots of radiobutton in groupbox and I want to get radiobutton's text data which is checked. However I dont want to write lots of "else if", what I mean is;

if (ui->radiobutton->isChecked)
// method's 
else if (ui->radiobutton->isCheked)
// method's 
..
..

Is there any way that I can understand which radiobutton is checked inside specific groupbox ? If possible could you please give me an example?


Solution

  • You can use a QButtonGroup as the parent: http://doc.qt.io/qt-5/qbuttongroup.html#checkedButton

    QAbstractButton * QButtonGroup::checkedButton() const

    Returns the button group's checked button, or 0 if no buttons are checked.

    See also buttonClicked().