How can I add a checkbox/radio button to QTableWidget

亡梦爱人 提交于 2019-11-30 02:59:46
serge_gubenko

For a checkbox using the item's setCheckState method should do what you need both for list and table widgets. See if code below would work for you:

List widget:

QListWidgetItem *item0 = new QListWidgetItem(tr("First"), listWidget);
QListWidgetItem *item1 = new QListWidgetItem(tr("Second"), listWidget);

item0->setCheckState(Qt::Unchecked);
item1->setCheckState(Qt::Checked);

Table widget:

QTableWidgetItem *item2 = new QTableWidgetItem("Item2");
item2->setCheckState(Qt::Checked);
tableWidget->setItem(0, 0, item2);

You can use delegates (QItemDelegate) for other types of editor's widgets, example is here: Spin Box Delegate Example.

I hope this helps.

Pie_Jesu

There is two methods:

void QTableWidget::setCellWidget ( int row, int column, QWidget * widget )

and

void QListWidget::setItemWidget ( QListWidgetItem * item, QWidget * widget )

They allow to insert any widget and other controls that inherit QWidget. Checkbox/radio button/combobox do inherit from QWidget.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!