我想在Qt中创建一个窗口,其中包含由{4}列组成的QTableWidget
,其中一列是文本,最后三列是QRadioButtons
。
我能够创造这个:
但是,我无法按行对QRadioButton
进行分组。实际上,使用当前的UI,我只能从显示的30个中选择一个无线电,而不是每行一个
这是我的代码:
// 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);
}
我该怎么做?
答案 0 :(得分:4)
QRadioButton的默认行为是与同一父项下的所有其他按钮互斥。在这种情况下,一旦你将它们放在单元格中,它们都将成为tableWidget的父级。
你应该做的是在每个循环结束时,创建一个新的QButtonGroup,设置一个要检查的按钮,然后将所有3添加到按钮组。现在,每个行都只在您为每行创建的QButtonGroup中是独占的。