#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); initPtr(); inittreeView_Pro(); } void MainWindow::initPtr() { treeView_Pro_model=NULL; // itemxiangmu=NULL; // itemwenjianjia1=NULL; // itemwenjianjia2=NULL; // itemgroup=NULL; qlist_QStandardItem_ptr.clear(); } void MainWindow::destroyPtr() { //要注意析构的顺序,如果不对,会发生异常,因为析构先析构子,再析构子依赖的父 for(int i=qlist_QStandardItem_ptr.size()-1;i>=0;i--) { delete qlist_QStandardItem_ptr[i]; } qlist_QStandardItem_ptr.clear(); if(treeView_Pro_model!=NULL) {delete treeView_Pro_model;treeView_Pro_model=NULL;} } void MainWindow::initm_publicIconMap() { m_publicIconMap[QStringLiteral("treeItem_Project")] =QIcon(QStringLiteral(":/new/prefix1/res_treeItemIcon/Project.png")); m_publicIconMap[QStringLiteral("treeItem_folder")] =QIcon(QStringLiteral(":/new/prefix1/res_treeItemIcon/folder.png")); m_publicIconMap[QStringLiteral("treeItem_folder-ansys")] =QIcon(QStringLiteral(":/new/prefix1/res_treeItemIcon/folder-ansys.png")); m_publicIconMap[QStringLiteral("treeItem_group")] =QIcon(QStringLiteral(":/new/prefix1/res_treeItemIcon/group.png")); m_publicIconMap[QStringLiteral("treeItem_channel")] =QIcon(QStringLiteral(":/new/prefix1/res_treeItemIcon/channel.png")); } void MainWindow::inittreeView_Pro() { initm_publicIconMap(); //if(treeView_Pro_model==NULL) treeView_Pro_model = new QStandardItemModel(ui->treeView_Pro); treeView_Pro_model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("项目名")<<QStringLiteral("信息")); QStandardItem* itemxiangmu = new QStandardItem(m_publicIconMap[QStringLiteral("treeItem_Project")],QStringLiteral("项目")); qlist_QStandardItem_ptr.append(itemxiangmu); treeView_Pro_model->appendRow(itemxiangmu); QStandardItem* itemwenjianjia1 = new QStandardItem(m_publicIconMap[QStringLiteral("treeItem_folder")],QStringLiteral("文件夹1")); qlist_QStandardItem_ptr.append(itemwenjianjia1); itemxiangmu->setChild(0,0,itemwenjianjia1); treeView_Pro_model->setItem(0/*model->indexFromItem(itemProject).row()*/,1,new QStandardItem(QStringLiteral("项目信息说明"))); itemxiangmu->setChild(0/*itemChild->index().row()*/,1,new QStandardItem(QStringLiteral("信息说明"))); QStandardItem* itemwenjianjia2 = new QStandardItem(m_publicIconMap[QStringLiteral("treeItem_folder")],QStringLiteral("文件夹2")); qlist_QStandardItem_ptr.append(itemwenjianjia2); itemxiangmu->appendRow(itemwenjianjia2); for(int i=0;i<5;++i) { QStandardItem* itemgroup = new QStandardItem(m_publicIconMap[QStringLiteral("treeItem_group")],QStringLiteral("组%1").arg(i+1)); qlist_QStandardItem_ptr.append(itemgroup); itemwenjianjia2->appendRow(itemgroup); itemwenjianjia2->setChild(itemgroup->index().row(),1,new QStandardItem(QStringLiteral("组%1信息说明").arg(i+1))); for(int j=0;j<(i+1);++j){ QStandardItem* itemchannel = new QStandardItem(m_publicIconMap[QStringLiteral("treeItem_channel")],QStringLiteral("频道%1").arg(j+1)); qlist_QStandardItem_ptr.append(itemchannel); itemgroup->appendRow(itemchannel); itemgroup->setChild(itemchannel->index().row(),1,new QStandardItem(QStringLiteral("频道%1信息说明").arg(j+1))); } } itemxiangmu->setChild(itemwenjianjia2->index().row(),1,new QStandardItem(QStringLiteral("文件夹2信息说明"))); ui->treeView_Pro->setModel(treeView_Pro_model); } MainWindow::~MainWindow() { destroyPtr(); delete ui; } void MainWindow::on_treeView_Pro_clicked(const QModelIndex &index) { //注释起来的这段代码是获取本节点,以及获取整个MODEL QStandardItemModel* model = static_cast<QStandardItemModel*>(ui->treeView_Pro->model()); //QModelIndex currentIndex = ui->treeView_Pro->currentIndex(); //QStandardItem* currentItem = model->itemFromIndex(currentIndex); QStandardItem* currentItem = model->itemFromIndex(index); //QStandardItem* pParent=getTopParent(currentItem); //目前存在问题,此得不到结果,星期一来了仔细检查 QStandardItem* pParent=getTopParent(currentItem); QString str; QModelIndex pparentIndex=model->indexFromItem(pParent); //QString strr=pParent->data().toString(); //QModelIndex pparentIndex=getTopParentColn(index); str += QStringLiteral("当前pParent:%1\nrow:%2,column:%3\n").arg(pparentIndex.data().toString()) .arg(pparentIndex.row()).arg(pparentIndex.column()); // str += QStringLiteral("当前pParent:%1\nrow:%2,column:%3\n").arg(pParent->data().toString()) // .arg(pParent->row()).arg(pParent->column()); // QString str; // str += QStringLiteral("当前选中:%1\nrow:%2,column:%3\n").arg(index.data().toString()) // .arg(index.row()).arg(index.column()); // str += QStringLiteral("父级:%1\n").arg(index.parent().data().toString()); // ui->label_realTime->setText(str); //下面是关于获取本节点的兄弟节点的信息 // QString str; // str += QStringLiteral("当前选中:%1\nrow:%2,column:%3\n").arg(index.data().toString()) // .arg(index.row()).arg(index.column()); // str += QStringLiteral("父级:%1\n").arg(index.parent().data().toString()); // QString name,info; // if(index.column() == 0) // { // name = index.data().toString(); // info = index.sibling(index.row(),1).data().toString(); // } // else // { // name = index.sibling(index.row(),0).data().toString(); // info = index.data().toString(); // } // str += QStringLiteral("名称:%1\n信息:%2").arg(name).arg(info); ui->label_realTime->setText(str); } //QStandardItem * QStandardItemModel::invisibleRootItem()函数并不是得到我们想要的这个顶层节点,它得到的是所有节点的最终根节点,因此,得到顶层节点需要自己写操作,下面是根据任意一个节点获取其可见顶层节点的代码: QStandardItem* MainWindow::getTopParent(QStandardItem* item) { QStandardItem* currentItem = item; while(item->parent()!= 0) { currentItem = item->parent(); item = currentItem; } if(currentItem->index().column() != 0) { QStandardItemModel* model = static_cast<QStandardItemModel*>(ui->treeView_Pro->model()); currentItem = model->itemFromIndex(currentItem->index().sibling(currentItem->index().row(),0)); } return currentItem; } //QStandardItem * QStandardItemModel::invisibleRootItem()函数并不是得到我们想要的这个顶层节点,它得到的是所有节点的最终根节点,因此,得到顶层节点需要自己写操作,下面是根据任意一个节点获取其可见顶层节点的代码: QModelIndex MainWindow::getTopParentColn(QModelIndex itemIndex) { QModelIndex secondItem = itemIndex; while(itemIndex.parent().isValid()) { secondItem = itemIndex.parent(); itemIndex = secondItem; } if(secondItem.column() != 0) { secondItem = secondItem.sibling(secondItem.row(),0); } return secondItem; } void MainWindow::on_pushButton_clicked() { QStandardItem * rootitem=treeView_Pro_model->invisibleRootItem();//Returns the model's invisible root item. QString str; str += QStringLiteral("rootitem: %1\nrow:%2,column:%3\n").arg(rootitem->data().toString()) .arg(rootitem->row()).arg(rootitem->column()); ui->label_realTime->setText(str); }
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QStandardItemModel> #include <QStandardItem> #include <QIcon> #include <QMap> #include <QList> #include <QDebug> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; public: void inittreeView_Pro(); void initm_publicIconMap(); private slots: void on_treeView_Pro_clicked(const QModelIndex &index); void on_pushButton_clicked(); private: QMap<QString, QIcon> m_publicIconMap; QStandardItemModel* treeView_Pro_model; // QStandardItem* itemxiangmu; // QStandardItem* itemwenjianjia1; // QStandardItem* itemwenjianjia2; // QStandardItem* itemgroup; QList<QStandardItem*> qlist_QStandardItem_ptr; private: void initPtr(); void destroyPtr(); QStandardItem* getTopParent(QStandardItem* item); QModelIndex getTopParentColn(QModelIndex itemIndex); }; #endif // MAINWINDOW_H