Qt中QTreeview目录树添加案例。实现右键菜单在Qt树形视图中添加子项功能的实例详解
本文介绍了如何在Qt中实现带有右键菜单的树形视图并根据右键点击事件在合适的层级下添加子项的功能。通过信号槽机制和对话框的交互使用户可以方便地管理树状数据结构。效果mainwindow.h代码//mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #includeQTreeView #include QMainWindow #includeQStandardItemModel QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent nullptr); ~MainWindow(); QStandardItemModel *model; // 声明 QStandardItemModel 成员变量 void initTree(); QTreeView* initTable; private: Ui::MainWindow *ui; int clickedTreeViewDepth(QModelIndex index);//判断目录层级的函数 }; #endif // MAINWINDOW_Hmainwindow.cpp代码// 包含必要的头文件 #include mainwindow.h #include ui_mainwindow.h #includeQTreeView #includeQLayout #includeQDebug #includeQMenu #includeQDialog #includeQLineEdit #includeQVBoxLayout #includeQPushButton #includeqstylefactory.h // MainWindow 类的构造函数 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui-setupUi(this); // 实例化 QStandardItemModel 作为数据模型 model new QStandardItemModel(this); // 初始化树形视图 initTree(); } // MainWindow 类的析构函数 MainWindow::~MainWindow() { delete ui; } // 初始化树形视图 void MainWindow::initTree() { // 新建一个树形视图 initTable new QTreeView(this); // 设置树形视图的样式为 Windows 风格 initTable-setStyle(QStyleFactory::create(windows)); // 将数据模型设置到树形视图中 initTable-setModel(model); // 隐藏树形视图的表头 initTable-header()-hide(); // 设置树形视图的位置和大小 initTable-setGeometry(10, 10, 300, 200); // 循环创建10个一级目录项并添加到数据模型中 for(int i 1; i 10; i){ QString categoryName 一级目录 QString::number(i); QStandardItem *cateA new QStandardItem(categoryName); cateA-setEditable(false); model-appendRow(cateA); } // 设置树形视图的上下文菜单策略为自定义 initTable-setContextMenuPolicy(Qt::CustomContextMenu); // 建立信号槽连接响应右键菜单请求事件 connect(initTable, QTreeView::customContextMenuRequested, this, [this](const QPoint pos){ // 获取当前选中的索引和目录层级 QModelIndex currentIndexSelect initTable-currentIndex(); int level clickedTreeViewDepth(currentIndexSelect); qDebug() 当前目录层级为 level; qDebug() 右键点击了 currentIndexSelect.data().toString().toUtf8().data(); // 创建右键菜单 QMenu *menu new QMenu(initTable); QAction *addAction menu-addAction(新建子项); // 弹出右键菜单并获取用户的选择项 QAction *item menu-exec(initTable-mapToGlobal(pos)); // 获取当前选中的项 QStandardItem *treeselectedItem model-itemFromIndex(currentIndexSelect); // 如果满足条件弹出对话框进行用户输入 if(level 4 item addAction){ QDialog *sonCateDialog new QDialog(menu); QLineEdit *LineEdit new QLineEdit(sonCateDialog); QVBoxLayout *layout1 new QVBoxLayout(menu); QPushButton *btn new QPushButton(ok,sonCateDialog); layout1-addWidget(LineEdit); layout1-addWidget(btn); sonCateDialog-setLayout(layout1); // 建立按钮点击信号槽连接添加子项并展开父项 connect(btn, QPushButton::clicked, sonCateDialog, [](){ QString userInput LineEdit-text(); QStandardItem *text new QStandardItem(userInput); treeselectedItem-appendRow(text); sonCateDialog-close(); initTable-expand(currentIndexSelect); }); // 弹出对话框等待用户输入 sonCateDialog-exec(); } }); } // 判断目录层级的函数 int MainWindow::clickedTreeViewDepth(QModelIndex index) { int level 1; QModelIndex current index; // 向上检查父节点计算层级 while(current.isValid()){ current current.parent(); if(current.isValid()){ level; } } return level; }