QTreeWidget 的用法
Posted lovebay
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QTreeWidget 的用法相关的知识,希望对你有一定的参考价值。
Qt QTreeWidget
新建一个Qt Widgets Application,拖拽一个Tree Widget 到 ui 界面上,最后实现的效果如下:
1 添加代码 2 3 4 //test.h 5 6 //在头文件里添加两个公用函数还有一个槽函数的声明 7 8 #include <QTreeWidgetItem> 9 10 public: 11 12 void initTreeWidget(); 13 14 void updateParentItem(QTreeWidgetItem *item); 15 16 public: 17 18 void treeItemChanged(QTreeWidgetItem *item,int column); 19 20 21 //test.cpp 22 23 //在构造函数里添加 connect() 24 25 initTreeWidget(); 26 27 connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem *,int)),this,SLOT(treeItemChanged(QTreeWidgetItem*,int))); 28 29 //initTreeWidget() 的实现 30 31 void test::initTreeWidget() 32 33 { 34 35 ui->treeWidget->clear(); 36 37 //第一组 38 39 QTreeWidgetItem *group1=new QTreeWidgetItem(ui->treeWidget); 40 41 group1->setText(0,"group1"); 42 43 group1->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 44 45 group1->setCheckState(0,Qt::Unchecked); 46 47 QTreeWidgetItem *item11=new QTreeWidgetItem(group1); 48 49 item11->setText(0,"item11"); 50 51 item11->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 52 53 item11->setCheckState(0,Qt::Unchecked); 54 55 QTreeWidgetItem *item12=new QTreeWidgetItem(group1); 56 57 item12->setText(0,"item12"); 58 59 item12->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 60 61 item12->setCheckState(0,Qt::Unchecked); 62 63 QTreeWidgetItem *item13=new QTreeWidgetItem(group1); 64 65 item13->setText(0,"item13"); 66 67 item13->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 68 69 item13->setCheckState(0,Qt::Unchecked); 70 71 //第二组 72 73 QTreeWidgetItem *group2=new QTreeWidgetItem(ui->treeWidget); 74 75 group2->setText(0,"group2"); 76 77 group2->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 78 79 group2->setCheckState(0,Qt::Unchecked); 80 81 QTreeWidgetItem *item21=new QTreeWidgetItem(group2); 82 83 item21->setText(0,"item21"); 84 85 item21->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 86 87 item21->setCheckState(0,Qt::Unchecked); 88 89 QTreeWidgetItem *item22=new QTreeWidgetItem(group2); 90 91 item22->setText(0,"item22"); 92 93 item22->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 94 95 item22->setCheckState(0,Qt::Unchecked); 96 97 QTreeWidgetItem *item23=new QTreeWidgetItem(group2); 98 99 item23->setText(0,"item23"); 100 101 item23->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); 102 103 item23->setCheckState(0,Qt::Unchecked); 104 105 } 106 107 //treeItemChanged(QTreeWidgetItem *item, int column)的实现 108 109 void test::treeItemChanged(QTreeWidgetItem *item, int column) 110 111 { 112 113 QString itemText=item->text(0); 114 115 //选中时 116 117 if(Qt::Checked==item->checkState(0)) 118 119 { 120 121 QTreeWidgetItem* parent=item->parent(); 122 123 int count=item->childCount(); 124 125 if(count>0) 126 127 { 128 129 for(int i=0;i<count;i++) 130 131 { 132 133 //子节点也被选中 134 135 item->child(i)->setCheckState(0,Qt::Checked); 136 137 } 138 139 } 140 141 else 142 143 { 144 145 //是子节点 146 147 updateParentItem(item); 148 149 } 150 151 } 152 153 else if(Qt::Unchecked==item->checkState(0)) 154 155 { 156 157 int count=item->childCount(); 158 159 if(count>0) 160 161 { 162 163 for(int i=0;i<count;i++) 164 165 { 166 167 item->child(i)->setCheckState(0,Qt::Unchecked); 168 169 } 170 171 } 172 173 else 174 175 { 176 177 //是子节点 178 179 updateParentItem(item); 180 181 } 182 183 } 184 185 } 186 187 //updateParentItem(QTreeWidgetItem *item) 的实现 188 189 void test::updateParentItem(QTreeWidgetItem *item) 190 191 { 192 193 QTreeWidgetItem* parent=item->parent(); 194 195 if(parent==NULL) 196 197 { 198 199 return; 200 201 } 202 203 //选中的子节点个数 204 205 int selectCount=0; 206 207 int childCount=parent->childCount(); 208 209 for(int i=0;i<childCount;i++) 210 211 { 212 213 QTreeWidgetItem *childItem=parent->child(i); 214 215 if(childItem->checkState(0)==Qt::Checked) 216 217 { 218 219 selectCount++; 220 221 } 222 223 } 224 225 if(selectCount<=0) 226 227 { 228 229 //选中状态 230 231 parent->setCheckState(0,Qt::Unchecked); 232 233 } 234 235 else if(selectCount>0 && selectCount<childCount) 236 237 { 238 239 //部分选中状态 240 241 parent->setCheckState(0,Qt::PartiallyChecked); 242 243 } 244 245 else if(selectCount==childCount) 246 247 { 248 249 //未选中状态 250 251 parent->setCheckState(0,Qt::Checked); 252 253 } 254 255 }
以上是关于QTreeWidget 的用法的主要内容,如果未能解决你的问题,请参考以下文章