标签:
TreeWidget 与 TreeView 中实现右键菜单稍有不同,
TreeView 中是靠信号与槽
connect(ui->treeView,SIGNAL(customContextMenuRequested(constQPoint&)), this,SLOT(onCustomContextMenuRequested(constQPoint&)));
实现TreeView内不同地方的右键菜单,文章见 http://blog.csdn.net/liukang325/article/details/22734735
TreeWidget 中是靠重构 contextMenuEvent(QContextMenuEvent*event) 函数实现不同地方的不同右键菜单的。具体代码如下:
h文件:
- private slots:
-
- void onGrpMenuTriggered(QAction *action);
- void onMemberMenuTriggered(QAction *action);
- void onAddAreaMenuTriggered(QAction *action);
-
- private:
-
- QMenu *m_grpMenu;
-
- QMenu *m_memberMenu;
- QMenu *m_addAreaMenu;
-
-
- QAction *m_grpAddOneMember;
- QAction *m_grpDeleteGrp;
- QAction *m_grpAddOneGrp;
-
- QAction *m_memberDelete;
- QAction *m_memberMove;
- QAction *m_memberSyncTime;
-
- QAction *m_addAreaAction;
- QAction *m_grpFlush;
- QAction *m_addDev;
-
- void createItemMenu();
cpp文件:
- 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"));
- }
-
- 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)
- {
-
- }
- else if (action == m_grpDeleteGrp)
- {
-
- }
- else if(action == m_grpAddOneGrp)
- {
-
- }
- }
-
- 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,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);
-
- }
另一篇TreeWidget 的用法,更加清晰明了的实现树状结构:http://blog.csdn.net/liukang325/article/details/13768523
补充:
childItem_1_2_1->setForeground(0,QBrush(QColor(Qt::blue)));
// 可将某一项的字体变颜色!
http://blog.csdn.net/liukang325/article/details/23694585
在TreeWidget中增加右键菜单功能 以及TreeWidget的基本用法
标签:
原文地址:http://www.cnblogs.com/findumars/p/5599436.html