当前位置:网站首页>QT notes - qtablewidget table spanning tree, qtreewidget tree node generates table content
QT notes - qtablewidget table spanning tree, qtreewidget tree node generates table content
2022-07-24 12:04:00 【Cool breeze in the old street °】
Purpose : We generate a tree from the data in the table , Then click the branch of the tree to get the data saved by the node on the tree and display it on another table .
A spanning tree table and data 
The contents of the spanning tree are :
By clicking on the node of the tree , Display the contents of the tree node :
1. We need to generate a tree according to the contents of the table
1.1 Find the target string in a column
QList<QTableWidgetItem*> qtreewidgetTest::findItem(const QString& target, int column)
{
// Store the string that finds the column
QList<QTableWidgetItem *> list;
QString str;
int row = ui.tableWidget2->rowCount();
int col = ui.tableWidget2->columnCount();
// Traverse the list Find the target string
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col;j++)
{
// If it is not this column
if(j != column)
continue;
// The string of this column is compared with the target character
str = ui.tableWidget2->item(i, j)->text();
if (target == str)
{
list.push_back(ui.tableWidget2->item(i, j));
}
}
}
return list;
}
1.2 Recurse all children of this parent node
void qtreewidgetTest::departTree(int fatherRow, QTreeWidgetItem* item)
{
// What came in was his father's node
// Find his child node through the parent node
QList <QTableWidgetItem*> list;
// Of the parent node ID Number
QString id = ui.tableWidget2->item(fatherRow, colItem)->text();
// Find all the children under the father node
list = findItem(id, colItem2);
int sonRow;
QString name;
if (!list.isEmpty())
{
for (int i = 0 ; i < list.size(); i++)
{
// The number of rows of the child and get the name of the child
sonRow = list.at(i)->row();
name = ui.tableWidget2->item(sonRow, colItem1)->text();
QTreeWidgetItem* newItem = new QTreeWidgetItem(item, QStringList(name));
// Add it to the incoming parent node
item->addChild(item);
// Recurse whether each child of the parent node has children
departTree(sonRow, newItem);
}
}
}
1.3 Make trees
void qtreewidgetTest::initTree()
{
QList<QTableWidgetItem*> list;
// If the parent node is 1 This node is the root node
QString str = "1";
list = findItem(str, colItem2);
int row;
QString name;
// The root node
if (!list.isEmpty())
{
for (int i = 0; i < list.size(); ++i)
{
row = list.at(i)->row();
name = ui.tableWidget2->item(row, colItem1)->text();
QTreeWidgetItem* item = new QTreeWidgetItem(ui.treeWidget, QStringList(name));
// Set the top-level node ( Root node )
ui.treeWidget->addTopLevelItem(item);
// Recurse all child nodes of the parent node
departTree(row, item);
}
}
ui.treeWidget->expandAll();
}
2. We need to give the tree node content , Then click the tree node to display the content in the table
// Traverse the tree list
QTreeWidgetItemIterator it(ui.treeWidget);
while (*it)
{
if ((*it)->text(0) == QStringLiteral(" sales 1 Ministry "))
{
QVariant varData;
varData.setValue(createMap(*it, list));
(*it)->setData(0, Qt::UserRole, QVariant::fromValue(varData));
}
else if ((*it)->text(0) == QStringLiteral(" The personnel department "))
{
QVariant varData1;
varData1.setValue(createMap(*it, list1));
(*it)->setData(0, Qt::UserRole, QVariant::fromValue(varData1));
}
//(*it)->text(0);
++it;
}
Click the tree node :
// Definition SLOTS
public slots:
void si_itemClicked(QTreeWidgetItem* item, int column);
// Connect click tree node signal
connect(ui.treeWidget, SIGNAL(itemClicked(QTreeWidgetItem* ,int)), this, SLOT(si_itemClicked(QTreeWidgetItem*, int)));
// stay SLOT Add data to the table
Complete code :
#pragma once
#include <QtWidgets/QWidget>
#include "ui_qtreewidgetTest.h"
#include <QTreeWidgetItem>
#include <QTableWidget>
#include <QStringList>
#include <QDebug>
#include <QStandardItem>
struct Person
{
QString name;
int age;
int score;
QString className;
};
Q_DECLARE_METATYPE(Person)
class qtreewidgetTest : public QWidget
{
Q_OBJECT
enum tableColumn
{
colItem = 0,
colItem1 ,
colItem2,
};
public:
qtreewidgetTest(QWidget *parent = Q_NULLPTR);
~qtreewidgetTest();
QMap <QString, QList<Person>> createMap(QTreeWidgetItem* item, QList<Person> & list); // The name of the node of the current tree Number data
void init_TableWidget2(); // Initialization list
QList<QTableWidgetItem*> findItem(const QString& target, int column); // Find the child of the current goal item
void departTree(int row, QTreeWidgetItem* item); // Update tree
void initTree();
public slots:
void si_itemClicked(QTreeWidgetItem* item, int column);
private:
Ui::qtreewidgetTestClass ui;
};
#include "qtreewidgetTest.h"
qtreewidgetTest::qtreewidgetTest(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
init_TableWidget2();
ui.treeWidget->setHeaderHidden(true);
initTree();
// Set data source
Person p = {
QStringLiteral(" Xiao Song "),20,60,QStringLiteral("2 class ") };
Person p1 = {
QStringLiteral(" Wang Wu "),26,90,QStringLiteral("2 class ") };
Person p2 = {
QStringLiteral(" Xiao Ming "),24,70,QStringLiteral("1 class ") };
Person p3 = {
QStringLiteral(" petty thief "),30,50,QStringLiteral("4 class ") };
// insert data
QList<Person> list;
list.append(p);
list.append(p1);
list.append(p2);
list.append(p3);
Person p5 = {
QStringLiteral(" Xiao Hu "),20,60,QStringLiteral("3 class ") };
Person p6 = {
QStringLiteral(" Xiao Chen "),26,90,QStringLiteral("2 class ") };
Person p4 = {
QStringLiteral(" Small Oh "),24,70,QStringLiteral("6 class ") };
QList<Person> list1;
list1.append(p4);
list1.append(p5);
list1.append(p6);
// Traverse the tree list
QTreeWidgetItemIterator it(ui.treeWidget);
while (*it)
{
if ((*it)->text(0) == QStringLiteral(" sales 1 Ministry "))
{
QVariant varData;
varData.setValue(createMap(*it, list));
(*it)->setData(0, Qt::UserRole, QVariant::fromValue(varData));
}
else if ((*it)->text(0) == QStringLiteral(" The personnel department "))
{
QVariant varData1;
varData1.setValue(createMap(*it, list1));
(*it)->setData(0, Qt::UserRole, QVariant::fromValue(varData1));
}
//(*it)->text(0);
++it;
}
// Set table properties
ui.tableWidget1->setColumnCount(4);
ui.tableWidget1->verticalHeader()->hide();
ui.tableWidget1->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui.tableWidget1->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// Set the header
QStringList head;
head << QStringLiteral(" full name ") << QStringLiteral(" Age ") << QStringLiteral(" fraction ") << QStringLiteral(" class ");
ui.tableWidget1->setHorizontalHeaderLabels(head);
connect(ui.treeWidget, SIGNAL(itemClicked(QTreeWidgetItem* ,int)), this, SLOT(si_itemClicked(QTreeWidgetItem*, int)));
}
qtreewidgetTest::~qtreewidgetTest()
{
}
QMap<QString, QList<Person>> qtreewidgetTest::createMap(QTreeWidgetItem * item, QList<Person>& p)
{
QMap <QString, QList<Person>> list;
list.insert(item->text(0), p);
return list;
}
void qtreewidgetTest::init_TableWidget2()
{
ui.tableWidget2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); // Horizontal table adaptation
ui.tableWidget2->setEditTriggers(QAbstractItemView::NoEditTriggers);
QStringList tab2List;
tab2List << "ID" << QStringLiteral(" name ") << "parent_ID";
ui.tableWidget2->setHorizontalHeaderLabels(tab2List);
}
QList<QTableWidgetItem*> qtreewidgetTest::findItem(const QString& target, int column)
{
// Store the string that finds the column
QList<QTableWidgetItem *> list;
QString str;
int row = ui.tableWidget2->rowCount();
int col = ui.tableWidget2->columnCount();
// Traverse the list Find the target string
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col;j++)
{
// If it is not this column
if(j != column)
continue;
// The string of this column is compared with the target character
str = ui.tableWidget2->item(i, j)->text();
if (target == str)
{
list.push_back(ui.tableWidget2->item(i, j));
}
}
}
return list;
}
void qtreewidgetTest::departTree(int fatherRow, QTreeWidgetItem* item)
{
// What came in was his father's node
// Find his child node through the parent node
QList <QTableWidgetItem*> list;
// Of the parent node ID Number
QString id = ui.tableWidget2->item(fatherRow, colItem)->text();
// Find all the children under the father node
list = findItem(id, colItem2);
int sonRow;
QString name;
if (!list.isEmpty())
{
for (int i = 0 ; i < list.size(); i++)
{
// The number of rows of the child Get the child's name
sonRow = list.at(i)->row();
name = ui.tableWidget2->item(sonRow, colItem1)->text();
QTreeWidgetItem* newItem = new QTreeWidgetItem(item, QStringList(name));
item->addChild(item);
departTree(sonRow, newItem);
}
}
}
void qtreewidgetTest::initTree()
{
QList<QTableWidgetItem*> list;
// If the parent node is 1 This node is the root node
QString str = "1";
list = findItem(str, colItem2);
int row;
QString name;
// The root node
if (!list.isEmpty())
{
for (int i = 0; i < list.size(); ++i)
{
row = list.at(i)->row();
name = ui.tableWidget2->item(row, colItem1)->text();
QTreeWidgetItem* item = new QTreeWidgetItem(ui.treeWidget, QStringList(name));
// Set the grading node
ui.treeWidget->addTopLevelItem(item);
departTree(row, item);
}
}
ui.treeWidget->expandAll();
}
void qtreewidgetTest::si_itemClicked(QTreeWidgetItem* item, int column)
{
// clear list
ui.tableWidget1->clearContents();
int rowNum = ui.tableWidget1->rowCount();
qDebug() << rowNum;
// Number of rows removed
for (int i = rowNum - 1; i >= 0; i--)
{
ui.tableWidget1->removeRow(i);
}
if (item == NULL)
return;
QMap <QString, QList<Person>> list = item->data(0, Qt::UserRole).value<QMap <QString, QList<Person>> >();
for (auto iter = list.begin(); iter != list.end(); ++iter)
{
const QList<Person>& plist = iter.value();
const QString &text = iter.key();
int row = plist.size();
// Insert rows
ui.tableWidget1->setRowCount(row);
// Insert table data
for (int i = 0; i < row; i++)
{
ui.tableWidget1->setItem(i, 0, new QTableWidgetItem(plist.at(i).name));
ui.tableWidget1->setItem(i, 1, new QTableWidgetItem(QString::number(plist.at(i).age)));
ui.tableWidget1->setItem(i, 2, new QTableWidgetItem(QString::number(plist.at(i).score)));
ui.tableWidget1->setItem(i, 3, new QTableWidgetItem(plist.at(i).className));
}
}
qDebug() << item->text(0);
}
ui chart :
design sketch :
Refer to the connection :
https://blog.csdn.net/weixin_45465612/article/details/104530127
https://ask.csdn.net/questions/1063721
边栏推荐
- CCF 1-2 question answering record (2)
- A* and JPS
- L1-049 天梯赛座位分配
- Agile? DevOps ?
- [C and pointer Chapter 14] preprocessor
- 理解数据的存与取
- 2 万字详解,吃透 ES!
- Miss waiting for a year! Baidu super chain digital publishing service is limited to 50% discount
- 源码分析Sentry用户行为记录实现过程
- Use prometheus+grafana to monitor server performance in real time
猜你喜欢

Linked list - Sword finger offer interview question 02.07. linked list intersection

Use of multithreading in QT

【网络空间安全数学基础第9章】有限域

链表——剑指offer面试题 02.07. 链表相交

Install JMeter
![Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]](/img/a9/f080940ec7bf94ab83c922990efa62.png)
Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]

1184. Distance between bus stops: simple simulation problem

Shell script "< < EOF" my purpose and problems

What is prescaler in STM32
![[mathematical basis of Cyberspace Security Chapter 3] congruence](/img/00/42a5f7f6f0e8a50884f4639767949f.jpg)
[mathematical basis of Cyberspace Security Chapter 3] congruence
随机推荐
Three small knowledge points about data product managers
Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
Top and bottom of stack
SQL multi condition query cannot be implemented
[TA frost wolf umay - "hundred people plan] Figure 3.3 surface subdivision and geometric shader large-scale grass rendering
Jmeter-While控制器
[rust] what software should I use to develop rust? Recommended editors commonly used to support rust
What is prescaler in STM32
[Commons beanautils topic] 005- convertutils topic
LogBack & MDC & a simple use
Recommended SSH cross platform terminal tool tabby
Delphi gexperts expert instructions for improving development efficiency
Shell script
Using huggingface model to translate English
Easy to use example
JMeter while controller
L1-064 AI core code valued at 100 million
Skillfully using command line parameters in Delphi to realize the trigger function of dragging files onto program icons
Quick check list of various XSS payloads