首页 > 学院 > 开发设计 > 正文

QTreeWidget中添加右键菜单

2019-11-11 05:26:41
字体:
来源:转载
供稿:网友

头文件

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow();PRivate slots: void onGrpMenuTriggered(QAction *action); void onMemberMenuTriggered(QAction *action); void onAddAreaMenuTriggered(QAction *action);private: //grp menu QMenu *m_grpMenu; //child menu QMenu *m_memberMenu; QMenu *m_addAreaMenu; //grp action QAction *m_grpAddOneMember; QAction *m_grpDeleteGrp; QAction *m_grpAddOneGrp; //child action QAction *m_memberDelete; QAction *m_memberMove; QAction *m_memberSyncTime; //action QAction *m_addAreaAction; QAction *m_grpFlush; QAction *m_addDev; void createItemMenu(); //构造函数中调用此函数 void createTreeWidget(); void contextMenuEvent(QContextMenuEvent *event);private: Ui::MainWindow *ui;};#endif // MAINWINDOW_H

.cpp文件

#include "mainwindow.h"#include "ui_mainwindow.h"#include <QContextMenuEvent>#include <QTreeWidgetItem>#include <qDebug>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); createItemMenu(); createTreeWidget();}MainWindow::~MainWindow(){ delete ui;}void MainWindow::createItemMenu(){ m_grpMenu = new QMenu(this); m_memberMenu = new QMenu(this); m_addAreaMenu = new QMenu(this); connect(m_grpMenu,SIGNAL(triggered(QAction *)), this,SLOT(onGrpMenuTriggered(QAction *))); connect(m_memberMenu,SIGNAL(triggered(QAction *)), this,SLOT(onMemberMenuTriggered(QAction *))); connect(m_addAreaMenu,SIGNAL(triggered(QAction *)), this,SLOT(onAddAreaMenuTriggered(QAction *))); m_grpAddOneMember = new QAction(tr("增加子区域"),this); m_grpAddOneMember->setIcon(QIcon(":/image/add.png")); m_grpAddOneGrp = new QAction(tr("增加设备"),this); m_grpAddOneGrp->setIcon(QIcon(":/image/add.png")); m_grpDeleteGrp = new QAction(tr("删除区域"),this); m_grpDeleteGrp->setIcon(QIcon(":/image/delete.png")); m_memberDelete = new QAction(tr("删除设备"),this); m_memberDelete->setIcon(QIcon(":/image/delete.png")); m_memberMove = new QAction(tr("移动设备"),this); m_memberMove->setIcon(QIcon(":/image/next.png")); m_memberSyncTime = new QAction(tr("同步时间"),this); m_memberSyncTime->setIcon(QIcon(":/image/move.png")); m_addAreaAction = new QAction(tr("增加区域"),this); m_addAreaAction->setIcon(QIcon(":/image/add.png")); m_addDev = new QAction(tr("增加设备"),this); m_addDev->setIcon(QIcon(":/image/add.png")); m_grpFlush = new QAction(tr("刷新列表"),this); m_grpFlush->setIcon(QIcon(":/image/update.png"));}//重构contextMenuEvent函数,记得#include <QContextMenuEvent>void MainWindow::contextMenuEvent(QContextMenuEvent *event){ QTreeWidgetItem *item = ui->treeWidget->currentItem(); if(item == NULL) { qDebug()<<"空白处"; if (m_addAreaMenu->isEmpty()) { //增加区域 m_addAreaMenu->addAction(m_addAreaAction); m_addAreaMenu->addAction(m_addDev); m_addAreaMenu->addAction(m_grpFlush); } //菜单出现的位置为当前鼠标的位置 m_addAreaMenu->exec(QCursor::pos()); } else if(ui->treeWidget->currentItem()->childCount() > 0) { if (m_grpMenu->isEmpty()) { //增加子区域 m_grpMenu->addAction(m_grpAddOneMember); //删除区域 m_grpMenu->addAction(m_grpDeleteGrp); m_grpMenu->addAction(m_grpAddOneGrp); } m_grpMenu->exec(QCursor::pos()); } else if(ui->treeWidget->currentItem()->childCount() == 0) { if (m_memberMenu->isEmpty()) { //删除设备 m_memberMenu->addAction(m_memberDelete); //移动设备 m_memberMenu->addAction(m_memberMove); //同步时间 m_memberMenu->addAction(m_memberSyncTime); } m_memberMenu->exec(QCursor::pos()); } event->accept();}void MainWindow::onGrpMenuTriggered(QAction *action){ if (action == m_grpAddOneMember) {//增加子区域 qDebug()<<"增加子区域"; } else if (action == m_grpDeleteGrp) {//删除区域 qDebug()<<"删除区域"; } else if(action == m_grpAddOneGrp) { qDebug()<<"增加子区域"; }}void MainWindow::onMemberMenuTriggered(QAction *action){ if (action == m_memberDelete) {//删除设备 } else if(action == m_memberMove) {//移动设备 } else if(action == m_memberSyncTime) {//同步时间 }}void MainWindow::onAddAreaMenuTriggered(QAction *action){ if (action == m_addAreaAction) {//增加区域 } if(action == m_addDev) {//增加设备 } if(action == m_grpFlush) {//刷新 }}void MainWindow::createTreeWidget(){ QIcon icon; icon.addPixmap(QPixmap(":/image/openStatus.png"), QIcon::Normal, QIcon::On);//节点打开状态 icon.addPixmap(QPixmap(":/image/closeStatus.png"), QIcon::Normal, QIcon::Off);//节点关闭状态 ui->treeWidget->setColumnCount(1); QStringList headers; headers << QObject::tr("管理"); ui->treeWidget->setHeaderLabels(headers); QStringList rootName_1; rootName_1 << QObject::tr("常用文件夹"); QTreeWidgetItem *rootItem_1 = new QTreeWidgetItem(ui->treeWidget, rootName_1); // rootItem_1->setIcon(0, QIcon(":/image/add.png")); //增加静态图标 rootItem_1->setIcon(0,icon); QStringList childName_1_1; childName_1_1 << "所有未读"; QTreeWidgetItem *childItem_1_1 = new QTreeWidgetItem(rootItem_1, childName_1_1); rootItem_1->addChild(childItem_1_1); QStringList childName_1_2; childName_1_2 << QObject::tr("置顶邮件"); QTreeWidgetItem *childItem_1_2 = new QTreeWidgetItem(rootItem_1, childName_1_2); rootItem_1->addChild(childItem_1_2); QStringList rootName_2; rootName_2 << QObject::tr("我的邮箱"); QTreeWidgetItem *rootItem_2 = new QTreeWidgetItem(ui->treeWidget, rootName_2); rootItem_2->setIcon(0,icon); QStringList childName_2_1; childName_2_1 << QObject::tr("收件箱"); QTreeWidgetItem *childItem_2_1 = new QTreeWidgetItem(rootItem_2, childName_2_1); rootItem_2->addChild(childItem_2_1); QStringList childName_2_2; childName_2_2 << QObject::tr("草稿箱"); QTreeWidgetItem *childItem_2_2 = new QTreeWidgetItem(rootItem_2, childName_2_2); rootItem_2->addChild(childItem_2_2); QStringList childName_2_3; childName_2_3 << QObject::tr("发件箱"); QTreeWidgetItem *childItem_2_3 = new QTreeWidgetItem(rootItem_2, childName_2_3); rootItem_2->addChild(childItem_2_3); ui->treeWidget->addTopLevelItem(rootItem_1); ui->treeWidget->addTopLevelItem(rootItem_2); QStringList childName_1_2_1; childName_1_2_1 << "测试"; QTreeWidgetItem *childItem_1_2_1 = new QTreeWidgetItem(childItem_1_2, childName_1_2_1); childItem_1_2->addChild(childItem_1_2_1);}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表