加盟品牌网站建设,东莞最新招聘,百度点击软件找名风,在什么网站上可以做中学数学家教QT-QTreeView使用方法
QTreeView: 用于显示树状结构数据#xff0c;适用于树状结构数据的操作。
一、初始化
利用QStandardlternModel来初始化数据#xff0c;标准的基于项数据的数据模型类#xff0c; 每个项数据可以是任何数据类型。
// 初始化model
QStandardItem…QT-QTreeView使用方法
QTreeView: 用于显示树状结构数据适用于树状结构数据的操作。
一、初始化
利用QStandardlternModel来初始化数据标准的基于项数据的数据模型类 每个项数据可以是任何数据类型。
// 初始化model
QStandardItemModel* m_standardItemModel new QStandardItemModel();// 使用model设置QTreeView表头
m_standardItemModel-setHorizontalHeaderLabels(QStringList(QStringLiteral(资源))); // TreeView控件载入model
ui.m_treeView-setModel(m_standardItemModel);// 展开数据
ui.m_treeView-expandAll();二、节点添加数据
利用QStandardItem创建根节点与子节点的Item并向内填充数据。数据类型可以为QString等字符也可以是常规的变量类型。
// 创建根节点抽象Item并没有实际数据
QStandardItem* itemRoot m_standardItemModel-invisibleRootItem();// 创建并添加Item的第一个子节点
QStandardItem* itemCam new QStandardItem(QStringLiteral(相机));
itemRoot-appendRow(itemCam);// 向第一个子节点itemCam添加子节点数据
QListQStandardItem* camList;
camList.append(new QStandardItem(cam1));
camList.append(new QStandardItem(cam2));
camList.append(new QStandardItem(cam3));
itemCam-appendRows(camList);// 创建并添加Item的第二个子节点
QStandardItem* itemImg new QStandardItem(QStringLiteral(图片));
itemRoot-appendRow(itemImg);// 向第二个子节点itemImg添加子节点数据
QListQStandardItem* imgList;
imgList.append(new QStandardItem(img1));
imgList.append(new QStandardItem(img2));
imgList.append(new QStandardItem(img3));
itemImg-appendRows(imgList);显示如下 三、QTreeView节点鼠标点击事件
1.1 鼠标单击事件响应类型
1、当我们将鼠标停靠在指定节点内并点击时,我们需要触发鼠标选中的item发生变化此时会有QModelInex发生变化。
相关参数详解 参数 功能 QModelIndex 返回item的row(), colmun()以及父节点 m_treeViewz-selectionModel() 选中当前鼠标单击的TreeView的model item项 QitemModel::currentChanged 鼠标选择item事件响应 QItemSelectionModel::currentChanged 跟踪视图中的选定项
代码如下
// 鼠标左键单击槽函数
void _on_image_tree_currentChanged(const QModelIndex current, const QModelIndex previous); // 槽函数连接
connect(ui.m_treeView-selectionModel(), QitemModel::currentChanged, this, on_image_tree_currentChanged);// 槽函数
QtDemos::on_image_tree_currentChanged()
{// 你的操作// 从索引index里面获得item指针model是tree的数据model这里item数据类型为QStandradItemModel*QModelIndex index current.sibling(current.row(), 0); // 获取当前item的子节点index note:子节点只有一行的情况下QStandardItem* item mTreeViewModel-itemFromIndex(index); // 根据index获取当前itemif(item){// 处理代码// int indexCurrentItem index.row() // 获取item的行号// QString text item-text(); // 获取item的文本// m_standardItemModel-itemFromIndex(index)-text() // 通过index获取item文本}
}四、其他操作
1、节点设置checkBox
itemCam-setCheckable(true);
itemImg-setCheckable(true);2、节点添加图标
【note】只能添加png, jpg等格式文件无法添加.ico图标文件
itemCam-setIcon(QIcon(C:/Users/admin/Desktop/Alexnet_Structure.png));3、节点不可编辑
ui-m_treeView-setEditTriggers(QAbstractItemView::NoEditTriggers);完整代码
4、右键菜单
//打开右键菜单属性
ui-treeView-setContextMenuPolicy(Qt::CustomContextMenu);//右键菜单
menu new QMenu(ui-treeView);
menu-addAction(添加);
menu-addAction(删除);最终效果 五、完整代码
QtDemos.h
#pragma once#include QtWidgets/QMainWindow
#include ui_QtDemos.h
#include QStandardItemModel
#include QMenuclass QtDemos : public QMainWindow
{Q_OBJECTpublic:QtDemos(QWidget *parent Q_NULLPTR);private:Ui::QtDemosClass ui;private slots:// QTreeView鼠标左键选中子节点事件响应槽函数void _on_m_treeView_current_changed(const QModelIndex current, const QModelIndex previous);void _on_m_treeView_MenuPopup(const QPoint pos);
};QtDemos.cpp
#include QtDemos.h
QtDemos::QtDemos(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);// 1.初始化model
QStandardItemModel* m_standardItemModel new QStandardItemModel();// 2.使用model设置QTreeView表头
m_standardItemModel-setHorizontalHeaderLabels(QStringList(QStringLiteral(资源))); // 3.创建根节点抽象Item并没有实际数据
QStandardItem* itemRoot m_standardItemModel-invisibleRootItem();// 4.创建并添加Item的第一个子节点
QStandardItem* itemCam new QStandardItem(QStringLiteral(相机));
itemRoot-appendRow(itemCam);// 5.向第一个子节点itemCam添加子节点数据
QListQStandardItem* camList;
camList.append(new QStandardItem(cam1));
camList.append(new QStandardItem(cam2));
camList.append(new QStandardItem(cam3));
itemCam-appendRows(camList);// 6.创建并添加Item的第二个子节点
QStandardItem* itemImg new QStandardItem(QStringLiteral(图片));
itemRoot-appendRow(itemImg);// 7.向第二个子节点itemImg添加子节点数据
QListQStandardItem* imgList;
imgList.append(new QStandardItem(img1));
imgList.append(new QStandardItem(img2));
imgList.append(new QStandardItem(img3));
itemImg-appendRows(imgList);// 8.TreeView控件载入model
ui.m_treeView-setModel(m_standardItemModel);// 9.展开数据
ui.m_treeView-expandAll();// 槽函数
connect(ui.m_treeView-selectionModel(), QItemSelectionModel::currentChanged, this, QtDemos::_on_m_treeView_current_changed);
connect(ui.m_treeView, QTreeView::customContextMenuRequested, this, QtDemos::_on_m_treeView_MenuPopup);// 其他操作// 节点设置checkBox
itemCam-setCheckable(true);
itemImg-setCheckable(true);// 节点设置图标
itemCam-setIcon(QIcon(C:/Users/admin/Desktop/Alexnet_Structure.png));// 只能添加png, jpg等格式文件无法添加.ico图标文件// 节点不可编辑
ui.m_treeView-setEditTriggers(QAbstractItemView::NoEditTriggers);// 右键菜单
ui.m_treeView-setContextMenuPolicy(Qt::CustomContextMenu);
}void QtDemos::_on_m_treeView_current_changed(const QModelIndex current, const QModelIndex previous)
{// 你的操作 ...QModelIndex index current.sibling(current.row(), 0); //子节点只有一行的情况下
}void QtDemos::_on_m_treeView_MenuPopup(const QPoint pos)
{QMenu* menu new QMenu(ui.m_treeView);menu-addAction(QStringLiteral(添加));menu-addAction(QStringLiteral(删除));
menu-exec(ui.m_treeView-mapToGlobal(pos));
}
文章转载自: http://www.morning.zcqtr.cn.gov.cn.zcqtr.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.kwrzg.cn.gov.cn.kwrzg.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.sgmis.com.gov.cn.sgmis.com http://www.morning.wrlxt.cn.gov.cn.wrlxt.cn http://www.morning.smmby.cn.gov.cn.smmby.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.csnch.cn.gov.cn.csnch.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.kjgrg.cn.gov.cn.kjgrg.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.ppghc.cn.gov.cn.ppghc.cn http://www.morning.zyrcf.cn.gov.cn.zyrcf.cn http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.ndhxn.cn.gov.cn.ndhxn.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.ypdhl.cn.gov.cn.ypdhl.cn http://www.morning.rbsmm.cn.gov.cn.rbsmm.cn http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.jtybl.cn.gov.cn.jtybl.cn http://www.morning.jxdhc.cn.gov.cn.jxdhc.cn http://www.morning.phzrq.cn.gov.cn.phzrq.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.kfcz.cn.gov.cn.kfcz.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.wmfmj.cn.gov.cn.wmfmj.cn http://www.morning.nzxdz.cn.gov.cn.nzxdz.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.ktmbr.cn.gov.cn.ktmbr.cn http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn http://www.morning.dhxnr.cn.gov.cn.dhxnr.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.mm27.cn.gov.cn.mm27.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.kwz6232.cn.gov.cn.kwz6232.cn http://www.morning.lrgfd.cn.gov.cn.lrgfd.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.alwpc.cn.gov.cn.alwpc.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.gbsby.cn.gov.cn.gbsby.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.lgmty.cn.gov.cn.lgmty.cn http://www.morning.wgqtj.cn.gov.cn.wgqtj.cn http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn http://www.morning.gnkbf.cn.gov.cn.gnkbf.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.gwsfq.cn.gov.cn.gwsfq.cn http://www.morning.rhpgk.cn.gov.cn.rhpgk.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.splkk.cn.gov.cn.splkk.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.dmsxd.cn.gov.cn.dmsxd.cn http://www.morning.zxfr.cn.gov.cn.zxfr.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.hpkr.cn.gov.cn.hpkr.cn http://www.morning.ykxnp.cn.gov.cn.ykxnp.cn http://www.morning.bwkzn.cn.gov.cn.bwkzn.cn http://www.morning.tbcfj.cn.gov.cn.tbcfj.cn http://www.morning.jwbfj.cn.gov.cn.jwbfj.cn http://www.morning.wqfrd.cn.gov.cn.wqfrd.cn http://www.morning.qtqjx.cn.gov.cn.qtqjx.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn http://www.morning.llsrg.cn.gov.cn.llsrg.cn http://www.morning.sjqml.cn.gov.cn.sjqml.cn http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.lsnbx.cn.gov.cn.lsnbx.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn