How to get the status of radio buttons from a radio button group which is inside a QTableWidget?
-
Hello all, I am very new to Qt and I just built this small little gui with radio buttons in QButtonGroup in each row of a QTableWidget. I wanted a dynamic GUI so I have a button to add rows dynamically to table (which wasn't a big deal). I also have a QComboBox at the 0th column in QTableWidget and I am able to know it's text using
for(int t_row = 0;t_row < ui->tableWidget->rowCount(); t_row++) { QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget->cellWidget(t_row,0)); qDebug() << myCB->currentText(); ui->textEdit->insertPlainText(myCB->currentText() + "\n"); }
However, I am not able to get the status of radio buttons and after spending sometime I do see that it is doable by using checkedId or checkedButton(), but I am not yet confident how to do so. Could anyone please let me know how would I do that here?
Also, I wanted to know if it is possible for me to use models and how much easier it is to learn as that is something that many of the threads mentioned but I haven't yet been able to understand that.
Thanks in advance!I am giving here the minimal code that I can to illustrate my problem and also I am attaching an image of the GUI.
.cpp code#include "dynamic_gui.h" #include "ui_dynamic_gui.h" dynamic_gui::dynamic_gui(QWidget *parent) : QMainWindow(parent) , ui(new Ui::dynamic_gui) { ui->setupUi(this); connect(ui->pushButton_cancel, SIGNAL(clicked()), this, SLOT(close())); } dynamic_gui::~dynamic_gui() { delete ui; } void dynamic_gui::on_pushButton_clicked() { ui->tableWidget->insertRow(ui->tableWidget->rowCount()); rButton_b1 = new QRadioButton(ui->tableWidget); rButton_b2 = new QRadioButton(ui->tableWidget); rButton_b3 = new QRadioButton(ui->tableWidget); ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,1,rButton_b1); ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,2,rButton_b2); ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,3,rButton_b3); combo = new QComboBox(); combo->addItems({"t1", "t2", "t3", "t4","Delete"}); connect(combo, SIGNAL(currentIndexChanged(QString)),this,SLOT(on_currentIndexChanged(QString))); ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,0,combo); g1 = new QButtonGroup; g1->addButton(rButton_b1, 1); g1->addButton(rButton_b2, 2); g1->addButton(rButton_b3, 3); } void dynamic_gui::on_currentIndexChanged(QString a) { if(a=="Delete") { ui->tableWidget->removeRow(ui->tableWidget->currentRow()); } } void dynamic_gui::on_pushButton_ok_clicked() { // read only the qcombox values in 0th column for(int t_row = 0;t_row < ui->tableWidget->rowCount(); t_row++) { QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget->cellWidget(t_row,0)); qDebug() << myCB->currentText(); ui->textEdit->insertPlainText(myCB->currentText() + "\n"); } // read which radio buttons are checked in 2nd row // for(int t_col = 0;t_col < ui->tableWidget->columnCount(); t_col++) // { // QButtonGroup *gk=qobject_cast<QButtonGroup*>(ui->tableWidget->cellWidget(2,t_col)); // qDebug() << gk->checkedButton(); // QRadioButton *rk=qobject_cast<QRadioButton*>(ui->tableWidget->cellWidget(2,t_col)); // qDebug() << rk->isChecked(); // } }
-
@qt_is_not_easy said in How to get the status of radio buttons from a radio button group which is inside a QTableWidget?:
I am not able to get the status of radio buttons
What did you try so far? What is exactly the problem?
You can iterate over rows/columns using https://doc.qt.io/qt-6/qtablewidget.html#item and then cast QTableWidgetItem to QRadiaButton to check its status. -
@qt_is_not_easy
As @jsulm says. YourQRadioButton *rk=qobject_cast<QRadioButton*>(ui->tableWidget->cellWidget(2,t_col));
already looks like the right principle, well done! Only thing is you are looping over every column assuming it will be a radio button, but not all columns are.Either only do that in the column numbers you know contain a
QRadioButton
(1, 2, 3 but not 0), or check the return result of the dynamic cast, which is a good idea anyway:for(int t_col = 0; t_col < ui->tableWidget->columnCount(); t_col++) { QComboBox *ck=qobject_cast<QComboBox*>(ui->tableWidget->cellWidget(2,t_col)); if (ck) qDebug() << t_col << "It's a combobox"; QRadioButton *rk=qobject_cast<QRadioButton*>(ui->tableWidget->cellWidget(2,t_col)); if (rk) qDebug() << t_col << "It's a radiobutton" << rk->isChecked(); }