How can I add a checkbox/radio button to QTableWidget

 ̄綄美尐妖づ 提交于 2019-11-29 00:05:06

问题


How can I add a checkbox/radiobutton/combobox to a QTableWidget or a QListWidget?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5369300/how-can-i-add-a-checkbox-radio-button-to-qtablewidget

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