标签:style blog http color io os 使用 ar for
void Widget::init() { QStandardItemModel* model = new QStandardItemModel(ui->treeView); model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("项目")); //加入项目目录 QStandardItem* root = new QStandardItem(QIcon(":/icon/icon/p.png"),QStringLiteral("项目")); root->setData(MARK_PROJECT,ROLE_MARK);//首先它是项目中目录 root->setData(MARK_FOLDER,ROLE_MARK_FOLDER);//其次它属于目录 model->appendRow(root); QStandardItem* folder = new QStandardItem(QIcon(":/icon/icon/h-f.png"),QStringLiteral("头文件")); folder->setData(MARK_FOLDER,ROLE_MARK);//首先它是目录 folder->setData(MARK_FOLDER_H,ROLE_MARK_FOLDER);//其次它属于头文件目录 root->appendRow(folder); folder = new QStandardItem(QIcon(":/icon/icon/c-f.png"),QStringLiteral("源文件")); folder->setData(MARK_FOLDER,ROLE_MARK);//首先它是目录 folder->setData(MARK_FOLDER_CPP,ROLE_MARK_FOLDER);//其次它属于源文件目录 root->appendRow(folder); ui->treeView->setModel(model); }
QStandardItemModel* Widget::getTreeModel() { return qobject_cast<QStandardItemModel*>(ui->treeView->model()); } QList<QStandardItem*> Widget::getRoots() { QList<QStandardItem*> roots; QStandardItemModel* model = getTreeModel(); for(int i=0;i < model->rowCount();++i) { roots.append(model->item(i)); } return roots; } QStandardItem* Widget::getProjectFolder() { QList<QStandardItem*> roots = getRoots(); for(auto i=roots.begin();i!=roots.end();++i){ if((*i)->data(ROLE_MARK) == MARK_PROJECT){ return (*i); } } return nullptr; } QStandardItem* Widget::getHeaderFolder() { QStandardItem* project = getProjectFolder(); if(nullptr == project) return nullptr; for(int i=0;i < project->rowCount();++i) { QStandardItem* child = project->child(i); QVariant var = child->data(ROLE_MARK_FOLDER); if(!var.isValid()) continue;//说明不是ROLE_MARK_FOLDER,有可能是一些项目,相应项目结构树那个xxx.pro就是一个非目录条目 if(MARK_FOLDER_H == var.value<int>()) return child; } return nullptr; } QStandardItem* Widget::getSrcFolder() { QStandardItem* project = getProjectFolder(); if(nullptr == project) return nullptr; for(int i=0;i < project->rowCount();++i) { QStandardItem* child = project->child(i); QVariant var = child->data(ROLE_MARK_FOLDER); if(!var.isValid()) continue;//说明不是ROLE_MARK_FOLDER,有可能是一些项目,相应项目结构树那个xxx.pro就是一个非目录条目 if(MARK_FOLDER_CPP == var.value<int>()) return child; } return nullptr; }
void Widget::on_pushButton_clicked() { static int s_header_count = 1; //找到头文件目录 QStandardItem* headerFolder = getHeaderFolder(); if(headerFolder) { QStandardItem* item = new QStandardItem(QIcon(":/icon/icon/i.png") ,QStringLiteral("%1.h").arg(s_header_count)); item->setData(MARK_ITEM,ROLE_MARK);//首先标定条目的类型 - 目录、项目、条目… item->setData(MARK_ITEM_H,ROLE_MARK_ITEM);//再次标定项目的类型 headerFolder->appendRow(item); ++s_header_count; } }
void Widget::on_treeView_customContextMenuRequested(const QPoint &pos) { }
m_projectMenu = new QMenu(this); m_itemMenu = new QMenu(this); QAction* ac = nullptr; ac = new QAction(QStringLiteral("构建"),this); m_projectMenu->addAction(ac); ac = new QAction(QStringLiteral("执行qmake"),this); m_projectMenu->addAction(ac); ac = new QAction(QStringLiteral("部署"),this); ac->setEnabled(false); m_projectMenu->addAction(ac); ac = new QAction(QStringLiteral("执行"),this); m_projectMenu->addAction(ac); m_projectMenu->addSeparator(); ac = new QAction(QStringLiteral("又一次构建"),this); m_projectMenu->addAction(ac); ac = new QAction(QStringLiteral("清除"),this); m_projectMenu->addAction(ac); m_projectMenu->addSeparator(); ac = new QAction(QStringLiteral("加入新文件……"),this); m_projectMenu->addAction(ac); ac = new QAction(QStringLiteral("余下的省略……"),this); m_projectMenu->addAction(ac); // ac = new QAction(QStringLiteral("打开文件"),this); m_itemMenu->addAction(ac); ac = new QAction(QStringLiteral("在explorer中显示"),this); m_itemMenu->addAction(ac); ac = new QAction(QStringLiteral("在此弹出命令提示"),this); m_itemMenu->addAction(ac); QMenu* itemChildMenu = new QMenu(m_itemMenu); itemChildMenu->setTitle(QStringLiteral("用…打开")); ac = new QAction(QStringLiteral("C++编辑器"),this); itemChildMenu->addAction(ac); ac = new QAction(QStringLiteral("普通文本编辑器"),this); itemChildMenu->addAction(ac); ac = new QAction(QStringLiteral("二进制编辑器"),this); itemChildMenu->addAction(ac); ac = new QAction(QStringLiteral("System Editor"),this); itemChildMenu->addAction(ac); m_itemMenu->addAction(itemChildMenu->menuAction()); ac = new QAction(QStringLiteral("余下省略n条"),this); m_itemMenu->addAction(ac);
void Widget::on_treeView_customContextMenuRequested(const QPoint &pos) { QModelIndex index = ui->treeView->indexAt(pos); QVariant var = index.data(ROLE_MARK); if(var.isValid()) { if(MARK_PROJECT == var.toInt()) m_projectMenu->exec(QCursor::pos());//弹出右键菜单,菜单位置为光标位置 else if(MARK_ITEM == var.toInt()) m_itemMenu->exec(QCursor::pos()); } }
void Widget::on_pushButton_3_clicked() { QModelIndex index = ui->treeView->currentIndex(); if(!index.isValid()) return; getTreeModel()->itemFromIndex(index)->setData(QColor(232,209,57,200),Qt::BackgroundRole); }
void Widget::init() { QStandardItemModel* model = new QStandardItemModel(ui->treeView); model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("项目")); //加入项目目录 QStandardItem* root = new QStandardItem(QIcon(":/icon/icon/p.png"),QStringLiteral("项目")); root->setData(MARK_PROJECT,ROLE_MARK);//首先它是项目中目录 root->setData(MARK_FOLDER,ROLE_MARK_FOLDER);//其次它属于目录 root->setData( QStringLiteral("这是关于QStandardItemModel设定角色的教程\n具体介绍见:http://blog.csdn.net/czyt1988/article/details/26018513") ,Qt::ToolTipRole ); …… }
void Widget::on_pushButton_4_clicked() { //涉及到遍历,因此使用回调函数,把遍历须要运行的函数传给封装好的遍历 StandardItemModelEx::ergodicAllItem(getTreeModel() ,std::bind(&Widget::callback_clearColor,this,std::placeholders::_1)); } void Widget::callback_clearColor(QStandardItem* item) { item->setData(QVariant(),Qt::BackgroundRole); }
QStandardItemModel角色控制及QTreeView加入不同的右键菜单
标签:style blog http color io os 使用 ar for
原文地址:http://www.cnblogs.com/blfshiye/p/4012091.html