In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to realize the Qt QTreeWidget tree structure". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The tree structure in Qt can be implemented using either the QTreeWidget class or the QTreeView class, and QTreeWidget inherits from the QTreeView class. The tree effect is shown in the following figure:
How did this happen? And there will be a corresponding event response when you click on the node.
1. Implementation of tree structure
There are treeWidget parts in QT GUI, so lay out the control in Gui, assuming that its object name is treeWidget.
QTreeWidget class official documentation: http://qt-project.org/doc/qt-4.8/qtreewidget.html
The tree structure is realized by QTreeWidget class and QTreeWidgetItem class, and the node is added by QTreeWidgetItem class. The code in the above figure is implemented as follows:
Ui- > treeWidget- > setColumnCount (1); / / set the number of columns ui- > treeWidget- > setHeaderLabel ("Image selection"); / / set the title of the header QTreeWidgetItem * imageItem1 = new QTreeWidgetItem (ui- > treeWidget,QStringList ("Image 1")); imageItem1- > setIcon ("xxx.png"); QTreeWidgetItem * imageItem1_1 = new QTreeWidgetItem (imageItem1,QStringList (QString ("Band1")); / / Child node 1imageItem1-> addChild (imageItem1_1) / / add child node QTreeWidgetItem * imageItem2 = new QTreeWidgetItem (ui- > treeWidget,QStringList ("image 2")); QTreeWidgetItem * imageItem2_1 = new QTreeWidgetItem (imageItem2,QStringList (QString ("Band1"); / / child node 1QTreeWidgetItem * imageItem2_2 = new QTreeWidgetItem (imageItem2,QStringList (QString ("Band2")); / / child node 2imageItem2-> addChild (imageItem2_1); / / add child node imageItem2- > addChild (imageItem2_2); ui- > treeWidget- > expandAll (); / / expand all nodes
Of course, there are other ways to set up, check the help documentation when needed.
In addition to using the above method, you can also use QList & items to add nodes. QT encapsulates the use of containers in the STL library in C++, and its encapsulated classes can easily solve many problems similar to complex data structures. The implementation is as follows:
/ / implementation of write-only node QList rootList;QTreeWidgetItem * imageItem1 = new QTreeWidgetItem; / / add the first parent node imageItem1- > setText (0quotient tr ("image 1")); rootList.append (imageItem1); QTreeWidgetItem * imageItem1_1 = new QTreeWidgetItem (imageItem1,QStringList (QString ("Band1"); / / add child node imageItem1- > addChild (imageItem1_1); QTreeWidgetItem * imageItem2 = new QTreeWidgetItem; / / add the second parent node imageItem2- > setText (0parenting tr ("image 2")) RootList.append (imageItem2); QTreeWidgetItem * imageItem2_1 = new QTreeWidgetItem (imageItem2,QStringList (QString ("Band1")); / / add child nodes QTreeWidgetItem * imageItem2_2 = new QTreeWidgetItem (imageItem2,QStringList ("Band2")); imageItem2- > addChild (imageItem2_1); imageItem2- > addChild (imageItem2_2); ui- > treeWidget- > insertTopLevelItems (0memrootList); / / insert nodes into the component ui- > treeWidget- > expandAll (); / all expand 2. Click on the event response of the node
First of all, think of whether there is a click on a node signal, view the document, there is a void itemClicked (QTreeWidgetItem * item, int column) signal, double-click the signal of a node, connect the signal to a custom slot, and trigger the slot function when you double-click the node.
Take a look at this signal. The first parameter is the QTreeWidgetItem class object you clicked on, and the second parameter is the column number of the node.
Idea: according to the QTreeWidgetItem class object you click, you can get the parent node 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 by the insertChildren (int index, const QList & children) function.
At present, we can only solve the event response when there is only one top parent node. When there are multiple top parent nodes (for example, there are 2 at the beginning of this article), when clicking on the child node, it is impossible to tell which parent node of the child node is (I am stupid!) So it is not possible to perform the corresponding operation for its slot function
Here, take a branch as an example.
Private slots: void showSelectedImage (QTreeWidgetItem* item, int column); / / Click the tree node event 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) / / Note: the top items have no parent node. Note (trap) return; int col = parent- > indexOfChild (item) when you double-click these items. / / the node line number of the item in the parent (starting from 0) if (0==col) / / Band1 {/ / perform the corresponding operation} if (1==col) / / Band2 {/ / perform the corresponding operation}}
If we do not add the judgment of whether the parent node is empty, when the node has a parent node, there will be no error, when the node does not have a parent node, the program will make an error (running error). Double-clicking a node without a parent node will be a shrinking operation of the tree.
This is the end of the content of "how to implement the Qt QTreeWidget tree structure". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.