twiBranch = new QTreeWidgetItem("branch");twiBranch->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsTristate);twiRoot->addChild(twiBranch);twiChild = new QTreeWidgetItem("branch");twiChild->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);twiBranch->addChild(twiChild);
#include <QtGui>#include <QtWidgets>class TestWindow: public QWidget { QTreeWidget *tree;public: TestWindow(QWidget *parent = 0); ~TestWindow() {delete tree;}};TestWindow::TestWindow(QWidget *parent) : QWidget(parent) { tree = new QTreeWidget(); tree->setHeaderHidden(true); tree->setRootIsDecorated(true); QTreeWidgetItem *twiAll; twiAll = new QTreeWidgetItem(); twiAll->setText(0,"all"); twiAll->setCheckState(0, Qt::Unchecked); twiAll->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsTristate); tree->addTopLevelItem(twiAll); twiAll->setExpanded(true); QTreeWidgetItem *twiParent = new QTreeWidgetItem(); twiParent->setText(0,"Parent 1"); twiParent->setCheckState(0, Qt::Unchecked); twiParent->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsTristate); twiAll->addChild(twiParent); twiParent->setExpanded(true); QTreeWidgetItem *twiChild = new QTreeWidgetItem(); twiChild->setText(0, "child a"); twiChild->setCheckState(0, Qt::Unchecked); twiChild->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled ); twiParent->addChild(twiChild); twiChild = new QTreeWidgetItem(); twiChild->setText(0, "child b"); twiChild->setCheckState(0, Qt::Unchecked); twiChild->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled ); twiParent->addChild(twiChild); twiParent = new QTreeWidgetItem(); twiParent->setText(0,"Parent 2"); twiParent->setCheckState(0, Qt::Unchecked); twiParent->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsTristate); twiAll->addChild(twiParent); twiParent->setExpanded(true); twiChild = new QTreeWidgetItem(); twiChild->setText(0, "child c"); twiChild->setCheckState(0, Qt::Unchecked); twiChild->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled ); twiParent->addChild(twiChild); twiChild = new QTreeWidgetItem(); twiChild->setText(0, "child d"); twiChild->setCheckState(0, Qt::Unchecked); twiChild->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled ); twiParent->addChild(twiChild); QVBoxLayout *vlayout = new QVBoxLayout(); vlayout->addWidget(tree); this->setLayout(vlayout); tree->setCurrentItem(tree->topLevelItem(0));}int main(int argc, char **argv) { QApplication a(argc, argv); TestWindow *window = new TestWindow(); window->show(); return a.exec();}