2

I would like to create a window in Qt that contains a QTableWidget composed of 4 columns, one of text and the last 3 are QRadioButtons.
I was able to create this:

MainWindow interface

However, I am unable to group the QRadioButtons by row. Indeed, with this current UI, I can only select ONE radio from the 30 displayed, instead of one per row.
Here's my code:

// 1st col stretchable, other 3 fixed width
QHeaderView *header = ui->tableWidget->horizontalHeader();
header->setResizeMode(QHeaderView::Stretch);
header->setResizeMode(1, QHeaderView::Interactive);
header->setResizeMode(2, QHeaderView::Interactive);
header->setResizeMode(3, QHeaderView::Interactive);

// Can't select lines
ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection);

// Test: fill the list
ui->tableWidget->setRowCount(10);
QLabel *nom;
QRadioButton *radio1, *radio2, *radio3;
for (int i = 0; i < 10; i++) {
    nom = new QLabel();
    nom->setText(QString("test")+QString::number(i));
    ui->tableWidget->setCellWidget(i, 0, nom);

    radio1 = new QRadioButton();
    radio2 = new QRadioButton();
    radio3 = new QRadioButton();
    ui->tableWidget->setCellWidget(i, 1, radio1);
    ui->tableWidget->setCellWidget(i, 2, radio2);
    ui->tableWidget->setCellWidget(i, 3, radio3);
}

How can I do this?

Benoit Duffez
  • 11,839
  • 12
  • 77
  • 125

1 Answers1

4

The default behavior of the QRadioButton is to be exclusive with all the other buttons under that same parent. In this case, they are all being parented to the tableWidget once you set them in their cells.

What you should do is at the end of every loop, create a new QButtonGroup, set one of the buttons to checked, and then add all 3 to the button group. Now each of those rows will be exclusive only within the QButtonGroup you created for each row.

jdi
  • 90,542
  • 19
  • 167
  • 203