Personal tools
You are here: Home Developer Resources Knowledgebase How can I create checkable items in a QTreeWidget?
Document Actions

How can I create checkable items in a QTreeWidget?



Answer:

In order to create checkable items in a QTreeWidget, you need to pass in the Qt::ItemIsUserCheckable flag to QTreeWidgetItem::setFlags in addition to calling QTreeWidgetItem::setCheckState(). See the following example for a demonstration:

#include <QApplication>
#include <QTreeWidget>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QTreeWidget box(0);
box.setColumnCount(1);
QTreeWidgetItem *itemOne = new QTreeWidgetItem(&box);

itemOne->setFlags(itemOne->flags()|Qt::ItemIsUserCheckable);
itemOne->setText(0,"Item one");
itemOne->setCheckState(0, Qt::Checked);

box.show();
return app.exec();
}
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: