In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people do not know what to do about how to operate and use the nested nodes of Cumberbatch + Qt TreeWidget. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Use the TreeWidget component to create a single-layer tree structure, and add the right-click menu function to the tree component. Next, we will continue to extend the use of the tree component and implement various operations on the tree frame multi-node.
Simple node traversal
First of all, we still use TreeView components to implement a simple multi-layer nested tree structure. After the code runs, we first set three outer nodes in a loop, then loop the inner nodes, and append the QStandardItem in the inner layer to the outer layer.
# include "mainwindow.h" # include "ui_mainwindow.h" # include # include / / By: LyShark// https://www.cnblogs.com/lysharkMainWindow::MainWindow(QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); QStandardItemModel * tree = new QStandardItemModel; ui- > treeView- > setColumnWidth (0J 50); / / set the length of the first column ui- > treeView- > setColumnWidth (1200) / / set the second column length ui- > treeView- > setColumnWidth (2200); / / set the third column length tree- > setHeaderData (0, Qt::Horizontal, tr ("serial number"); tree- > setHeaderData (1, Qt::Horizontal, tr ("name")); tree- > setHeaderData (2, Qt::Horizontal, tr ("age")); ui- > treeView- > setModel (tree); for (int I = 0; I
< 4; ++i) { // 设置3个外层节点 QList items; for (int i = 0; i < 3; ++i) { QStandardItem *item = new QStandardItem(QString("%0").arg(i)); if (0 == i) item->SetCheckable (true); items.push_back (item);} tree- > appendRow (items); / / set inner layer for (int I = 0; I
< 2; ++i) { QList childItems; for (int i = 0; i < 3; ++i) { QStandardItem *item = new QStandardItem(QString("lyshark")); if (0 == i) item->SetCheckable (true); childItems.push_back (item);} items.at (0)-> appendRow (childItems);} MainWindow::~MainWindow () {delete ui;}
The code works as follows:
Initialize the tree node
First of all, before starting to manipulate elements, we can simply initialize the tree nodes in MainWindow::MainWindow and insert several test nodes.
# include "mainwindow.h" # include "ui_mainwindow.h" # include # include # include / / By: LySharkMainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); ui- > treeWidget- > clear (); / / set the number of QTreeWidget columns ui- > treeWidget- > setColumnCount (1); / / set QTreeWidget title to hide ui- > treeWidget- > setHeaderHidden (true) / / create a friend node of QTreeWidget. The parent node is tree QTreeWidgetItem * Friend = new QTreeWidgetItem (ui- > treeWidget,QStringList (QString ("friend")); Friend- > setIcon (0Coje QIcon (": / image/4.ico")); / / add an icon Friend- > setFlags (Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsAutoTristate); Friend- > setCheckState (0Magic QtGV checked) / / add a child node to Friend frd QTreeWidgetItem * frd = new QTreeWidgetItem (Friend); frd- > setText (0, "www.lyshark.com"); frd- > setIcon (0LQ Qicon (tr (": / image/1.ico")); frd- > setCheckState (0Q QT image/1.ico checked); / / default selected state QTreeWidgetItem * frs = new QTreeWidgetItem (Friend); frs- > setText (0, "cdn.lyshark.com") Frs- > setIcon (0Query QIcon (tr (": / image/1.ico")); frs- > setCheckState (0memeQtGanghuo Unmanned) / / default is not selected / /-/ / create a node called classmate, and the parent node is also tree QTreeWidgetItem * ClassMate = new QTreeWidgetItem (ui- > treeWidget,QStringList (QString (classmate) ClassMate- > setIcon (0QString QIcon (": / image/5.ico")); / / add an icon ClassMate- > setCheckState (0Magic QT image/5.ico checked); / / default selected / / Fly is the child node of ClassMate * Fly = new QTreeWidgetItem (QStringList ("nas.lyshark.com")); Fly- > setIcon (0Magnum QIcon (tr (": / image/2.ico") / / another way to create child nodes: ClassMate- > addChild (Fly); Fly- > setCheckState (0MagneQTGV checked); / / set to select QTreeWidgetItem * Fls = new QTreeWidgetItem (QStringList ("lyshark.cnblogs.com")); Fls- > setIcon (0LQ Qicon (tr (": / image/2.ico"); ClassMate- > addChild (Fls); Fls- > setCheckState (0Query QTGV checked) / / set to select / /-/ / strangers alone QTreeWidgetItem * Strange = new QTreeWidgetItem (true); Strange- > setText (0, "strangers") Strange- > setIcon (0ui- QIcon (": / image/6.ico"); / / add an icon ui- > treeWidget- > addTopLevelItem (ClassMate); ui- > treeWidget- > addTopLevelItem (Strange); / / add text to the edit box ui- > plainTextEdit- > appendPlainText ("hello lyshark"); / / expand all nodes of QTreeWidget / ui- > treeWidget- > expandAll (); / / ui- > treeWidget- > resize (271401);} MainWindow::~MainWindow () {delete ui;}
The code works as follows:
Click double-click node feedback
When we dock the mouse in a specified node and click, we need to trigger the treeWidget_itemDoubleClicked attribute to give feedback to basic attributes such as the line title.
/ / get the name of the specified member void MainWindow::on_treeWidget_itemDoubleClicked (QTreeWidgetItem * item, int column) {QString str = item- > text (column); std::cout appendPlainText (str.toStdString (). Data ()) when we double-click the specified member;} / / get the data void MainWindow::on_treeWidget_itemClicked (QTreeWidgetItem * item, int column) {QString str = item- > text (column) when we click the specified member Std::cout appendPlainText (str.toStdString (). Data ();}
The code works as follows:
Add parent node / child node
Through the way of code, when you click on_pushButton_clicked, you can add a parent node and a child node respectively.
/ / Click the button to add a new parent void MainWindow::on_pushButton_clicked () {QString NodeText = "New parent"; QTreeWidgetItem * item = new QTreeWidgetItem (true); item- > setText (0Magic NodeText); item- > setIcon (0qicon (": / image/7.ico")); ui- > treeWidget- > addTopLevelItem (item) } / / Click the button to add a child node void MainWindow::on_pushButton_4_clicked () {QTreeWidgetItem * item= ui- > treeWidget- > currentItem (); if (itemized null) AddTreeNode (item, "new child node", "new child node"); else AddTreeRoot ("new child node", "new child node");}
The code works as follows:
Delete selected node
First of all, select the specified node to be deleted, and then you can delete the node. Delete the child node directly, and delete the parent node together with the internal child node.
/ / delete the selected node void MainWindow::on_pushButton_3_clicked () {QTreeWidgetItem * currentItem = ui- > treeWidget- > currentItem (); if (currentItem = = NULL) return; / / delete if directly if there is no parent node (currentItem- > parent () = = NULL) {delete ui- > treeWidget- > takeTopLevelItem (ui- > treeWidget- > currentIndex (). Row ()) Std::cout treeWidget- > currentIndex () .row () parent ()-> takeChild (ui- > treeWidget- > currentIndex () .row ());}}
The code works as follows:
Modify the specified node name
Click to change the specified node to Modify and set the icon to the new
/ / modify the node / / By: LySharkvoid MainWindow::on_pushButton_2_clicked () {/ / get the current node QTreeWidgetItem * currentItem = ui- > treeWidget- > currentItem (); if (currentItem = = NULL) return; / / modify the selected item for (int xscene x columnCount (); x scene +) {currentItem- > setText (xMagnetr ("Modify") + QString::number (x)) CurrentItem- > setIcon (xmam QIcon (": / image/1.ico");}}
The code works as follows:
Enumerate all node elements
Enumerates all node elements in the current Tree and outputs the results to the right edit box.
/ / enumerate all nodes / / By: LyShark// enumerates all nodes void MainWindow::on_pushButton_5_clicked () {/ / get the total number of root nodes int size = ui- > treeWidget- > topLevelItemCount (); QTreeWidgetItem * child; for (int xtreeWidget-> topLevelItem (x); std::cout appendPlainText (child- > text (0). ToStdString (). Data ()) / / get the count of all child nodes int childCount = child- > childCount (); / / std::cout treeWidget- > topLevelItemCount (); QTreeWidgetItem * child; for (int xtreeWidget-> topLevelItem (x); / / get the count of all child nodes int childCount = child- > childCount (); / / output the child node for under the root node (int ynode) / / determine whether it is selected, if the output parent node and child node if (Qt::Checked = = grandson- > checkState (0)) {std::cout appendPlainText (grandson- > text (0). ToStdString (). Data ());}
The code works as follows:
Get the parent node of the selected child node
Get the parent ID of the child node, and then get the child node name according to the ID.
Void MainWindow::on_pushButton_6_clicked () {/ / take all parent nodes QTreeWidgetItem * currentItem = ui- > treeWidget- > currentItem ()-> parent (); int root_count = ui- > treeWidget- > indexOfTopLevelItem (currentItem); std::cout text (0). ToStdString (). Data ();}}
The code works as follows:
To supplement the definition of the node insertion function, the definitions of the two functions of AddTreeRoot/AddTreeNode are as follows.
/ / add header declaration QTreeWidgetItem * AddTreeRoot (QString name,QString desc); QTreeWidgetItem * AddTreeNode (QTreeWidgetItem * parent,QString name,QString desc) in mainwindow.h; / / add implementation part QTreeWidgetItem * MainWindow::AddTreeRoot (QString name,QString desc) {QTreeWidgetItem * item=new QTreeWidgetItem (QStringList () in mainwindow.cpp
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.