How to implement Qt QTreeWidget tree structure

To realize the tree structure in Qt, you can use the QTreeWidget class or the QTreeView class. QTreeWidget inherits from the QTreeView class. The tree effect is shown in the figure below:

 

How is this possible? There is also a corresponding event response when a node is clicked.

1. Tree structure realization

There is a treeWidget component in QT GUI, and the control is laid out in Gui, assuming that its object name is treeWidget.

Official documentation of the QTreeWidget class: http://qt-project.org/doc/qt-4.8/qtreewidget.html

The tree structure is realized by the QTreeWidget class and the QTreeWidgetItem class, and the QTreeWidgetItem class implements the addition of nodes. The above code is implemented as follows:

ui->treeWidget->setColumnCount(1); //设置列数
ui->treeWidget->setHeaderLabel(tr("图像选择")); //设置头的标题

QTreeWidgetItem *imageItem1 = new QTreeWidgetItem(ui->treeWidget,QStringList(QString("图像1")));
imageItem1->setIcon(0,QIcon("xxx.png"));
QTreeWidgetItem *imageItem1_1 = new QTreeWidgetItem(imageItem1,QStringList(QString("Band1"))); //子节点1
imageItem1->addChild(imageItem1_1); //添加子节点

QTreeWidgetItem *imageItem2 = new QTreeWidgetItem(ui->treeWidget,QStringList(QString("图像2")));
QTreeWidgetItem *imageItem2_1 = new QTreeWidgetItem(imageItem2,QStringList(QString("Band1"))); //子节点1
QTreeWidgetItem *imageItem2_2 = new QTreeWidgetItem(imageItem2,QStringList(QString("Band2"))); //子节点2
imageItem2->addChild(imageItem2_1);  //添加子节点
imageItem2->addChild(imageItem2_2);

ui->treeWidget->expandAll(); //结点全部展开

Of course, there are other methods for setting, please refer to the help documentation for specific needs.

In addition to using the above method, you can also use QList<QTreeWidgetItem *> & items to add nodes. QT encapsulates the use of containers in the STL library in C++, and using the encapsulated classes can easily solve many problems similar to very complex data structures. The implementation is as follows:

//只写结点的实现
QList<QTreeWidgetItem *> rootList;

QTreeWidgetItem *imageItem1 = new QTreeWidgetItem;   //添加第一个父节点
imageItem1->setText(0,tr("图像1"));
rootList.append(imageItem1);

QTreeWidgetItem *imageItem1_1 = new QTreeWidgetItem(imageItem1,QStringList(QString("Band1"))); //添加子节点
imageItem1->addChild(imageItem1_1);

QTreeWidgetItem *imageItem2 = new QTreeWidgetItem;   //添加第二个父节点
imageItem2->setText(0,tr("图像2"));
rootList.append(imageItem2);

QTreeWidgetItem *imageItem2_1 = new QTreeWidgetItem(imageItem2,QStringList(QString("Band1")));  //添加子节点
QTreeWidgetItem *imageItem2_2 = new QTreeWidgetItem(imageItem2,QStringList(QString("Band2")));
imageItem2->addChild(imageItem2_1);
imageItem2->addChild(imageItem2_2);

ui->treeWidget->insertTopLevelItems(0,rootList);  //将结点插入部件中

ui->treeWidget->expandAll(); //全部展开

2. Click the event response of the node

First of all, think of whether there is a signal to click on a certain node. Check the document. There is a void itemClicked ( QTreeWidgetItem * item , int column ) signal, which is a signal to double-click a certain node. Connect the signal to a custom slot. When double-clicking a node The slot function is triggered when.

Look at this signal, the first parameter is the clicked QTreeWidgetItem class object, and the second parameter is the column number where the node is located.

Idea: According to the clicked QTreeWidgetItem class object, the parent node can be obtained through the parent() function. If the QTreeWidgetItem class object is the topmost node, the parent() function returns NULL. The index value of the node in the parent node can be obtained through the insertChildren ( int index , const QList<QTreeWidgetItem *> & children ) function.

At present, it can only solve the event response when there is only one topmost parent node. When there are multiple topmost parent nodes (for example, there are 2 at the beginning of this article), when clicking on a child node, it is impossible to determine which parent node the child node is (I'm stupid!), so I can't perform corresponding operations for its slot function.

Here is an example of a branch.

private slots:
      void showSelectedImage(QTreeWidgetItem * item, int column); //点击树节点事件
  
  connect(ui->treeWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(showSelectedImage(QTreeWidgetItem*,int)));
  
  void MainWindow::showSelectedImage(QTreeWidgetItem *item, int column)
  {
      QTreeWidgetItem *parent = item->parent();
      if(NULL==parent) //注意:最顶端项是没有父节点的,双击这些项时注意(陷阱)
         return;
     int col = parent->indexOfChild(item); //item在父项中的节点行号(从0开始)
 
     if(0==col) //Band1
     {
         //执行对应操作
     }
     if(1==col) //Band2
     {
         //执行对应操作
     }
 }

If the judgment of whether the parent node is empty is not added, when the node has a parent node, there will be no error. When the node has no parent node, the program will make an error (operation error). After the judgment, double-click the node without the parent node and it will be a tree shrinking operation.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_60259116/article/details/130394067