C++ 如何向 QTableWidget 添加复选框/单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5369300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add a checkbox/radio button to QTableWidget
提问by Pie_Jesu
How can I add a checkbox/radiobutton/combobox to a QTableWidget or a QListWidget?
如何将复选框/单选按钮/组合框添加到 QTableWidget 或 QListWidget?
采纳答案by serge_gubenko
For a checkbox using the item's setCheckStatemethod should do what you need both for list and table widgets. See if code below would work for you:
对于使用项目的setCheckState方法的复选框应该可以满足列表和表格小部件的需求。看看下面的代码是否适合你:
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.
您可以将委托(QItemDelegate)用于其他类型的编辑器小部件,示例在这里:Spin Box Delegate Example。
I hope this helps.
我希望这有帮助。
回答by 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
.
它们允许插入任何继承 QWidget 的小部件和其他控件。复选框/单选按钮/组合框确实从QWidget
.
回答by user889030
you can add checkbox like this too
你也可以添加这样的复选框
#include <QCheckBox>
void addCheckBoxAt(int row_number, int column_number,int state)
{
// Create a widget that will contain a checkbox
QWidget *checkBoxWidget = new QWidget();
QCheckBox *checkBox = new QCheckBox(); // We declare and initialize the checkbox
QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget); // create a layer with reference to the widget
layoutCheckBox->addWidget(checkBox); // Set the checkbox in the layer
layoutCheckBox->setAlignment(Qt::AlignCenter); // Center the checkbox
layoutCheckBox->setContentsMargins(0,0,0,0); // Set the zero padding
/* Check on the status of odd if an odd device,
* exhibiting state of the checkbox in the Checked, Unchecked otherwise
* */
if(state == 1){
checkBox->setChecked(true);
} else {
checkBox->setChecked(false);
}
ui->job_table_view->setCellWidget(row_number,column_number, checkBoxWidget);
// Another way to add check box as item
/*
// QTableWidgetItem *checkBoxItem = new QTableWidgetItem("checkbox string ");
QTableWidgetItem *checkBoxItem = new QTableWidgetItem();
checkBoxItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
checkBoxItem->setCheckState(Qt::Checked);
ui->job_table_view->setItem(row_number,column_number,checkBoxItem);
*/
}
// call it like
// 像这样称呼它
addCheckBoxAt(0,0,1); // insert checkbox it 0,0 and check status true