In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "What are the methods of using Tree Widget in Qt", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "What are the methods of using Tree Widget in Qt"!
Widget Tree can be found in Item Widgets. This kind of control is actually useful sometimes. I mainly use tree controls with checkboxes. The results are shown in the figure;
When the top-level tree node is selected, all child nodes are selected; when the top-level tree node is deselected, all child nodes are deselected, and when the child node is selected, the parent node displays a partially selected state.
Several main functions:
setCheckState setting checkbox;
Is there a checkbox on the ItemIsUserCheckable item
ItemIsEnabled item is not disabled (Enabled/Disabled)
ItemIsSelectable item can be selected
Code:
Insert the following code in mainwindow.h:
void init(); void updateParentItem(QTreeWidgetItem *item); public slots: void treeItemChanged(QTreeWidgetItem* item,int column);
Add to the mainwindow constructor:
init(); connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(treeItemChanged(QTreeWidgetItem*,int)));
Implementation of init function;
QTreeWidgetItem* group1=new QTreeWidgetItem(ui->treeWidget); group1->setText(0,"group1"); group1->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); group1->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* subItem11=new QTreeWidgetItem(group1); subItem11->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem11->setText(0,"subItem11"); subItem11->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* subItem12=new QTreeWidgetItem(group1); subItem12->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem12->setText(0,"subItem12"); subItem12->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* subItem13=new QTreeWidgetItem(group1); subItem13->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem13->setText(0,"subItem11"); subItem13->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* subItem14=new QTreeWidgetItem(group1); subItem14->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem14->setText(0,"subItem12"); subItem14->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* group2=new QTreeWidgetItem(ui->treeWidget); group2->setText(0,"group2"); group2->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); group2->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* subItem1=new QTreeWidgetItem(group2); subItem1->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem1->setText(0,"subItem11"); subItem1->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* subItem2=new QTreeWidgetItem(group2); subItem2->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem2->setText(0,"subItem12"); subItem2->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* subItem3=new QTreeWidgetItem(group2); subItem3->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem3->setText(0,"subItem11"); subItem3->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* subItem4=new QTreeWidgetItem(group2); subItem4->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem4->setText(0,"subItem12"); subItem4->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* group3=new QTreeWidgetItem(ui->treeWidget); group3->setText(0,"group3"); group3->setFlags(Qt::ItemIsAutoTristate|Qt::ItemIsEnabled|Qt::ItemIsSelectable); group3->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* subItem111=new QTreeWidgetItem(group3); subItem111->setFlags(Qt::ItemIsAutoTristate|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem111->setText(0,"subItem111"); subItem111->setCheckState(0,Qt::Unchecked); QTreeWidgetItem* subItem112=new QTreeWidgetItem(group3); subItem112->setFlags(Qt::ItemIsAutoTristate|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem112->setText(0,"subItem112"); subItem112->setCheckState(0,Qt::Unchecked);
treeItemChanged function implementation;
QString itemText=item->text(0); //Select if(Qt::Checked ==item->checkState(0))//check All Check { QTreeWidgetItem* parent=item->parent();//Define a node int count=item->childCount();//Number of children if(count>0) { for(int i=0;ichild(i)->setCheckState(0,Qt::Checked); } } else { //is a child node updateParentItem(item); } } else if(Qt::Unchecked==item->checkState(0))//Unchecked exists { int count=item->childCount(); if(count>0) { for(int i=0;ichild(i)->setCheckState(0,Qt::Unchecked); } } else { updateParentItem(item); } }
Implementation of updateParentItem function;
QTreeWidgetItem* parent=item->parent(); if(parent==NULL) { return; } int selectedCount =0; int childCount =parent->childCount(); for(int i=0;ichild(i); if(childItem->checkState(0)==Qt::Checked) { selectedCount++; } } if(selectedCountsetCheckState(0,Qt::Unchecked); } else if(selectedCount>0&&selectedCountsetCheckState(0,Qt::PartiallyChecked); } else if(selectedCount==childCount) { parent->setCheckState(0,Qt::Checked); } At this point, I believe that everyone has a deeper understanding of "what are the methods of using the Tree Widget in Qt", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.