zoukankan      html  css  js  c++  java
  • 在TreeWidget中增加右键菜单功能 以及TreeWidget的基本用法

    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文件:

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. private slots:  
    2.   
    3.     void onGrpMenuTriggered(QAction *action);  
    4.     void onMemberMenuTriggered(QAction *action);  
    5.     void onAddAreaMenuTriggered(QAction *action);  
    6.   
    7. private:  
    8.     //grp menu  
    9.     QMenu *m_grpMenu;  
    10.     //child menu  
    11.     QMenu *m_memberMenu;  
    12.     QMenu *m_addAreaMenu;  
    13.   
    14.     //grp action  
    15.     QAction *m_grpAddOneMember;  
    16.     QAction *m_grpDeleteGrp;  
    17.     QAction *m_grpAddOneGrp;  
    18.     //child action  
    19.     QAction *m_memberDelete;  
    20.     QAction *m_memberMove;  
    21.     QAction *m_memberSyncTime;  
    22.     //action  
    23.     QAction *m_addAreaAction;  
    24.     QAction *m_grpFlush;  
    25.     QAction *m_addDev;  
    26.   
    27.     void createItemMenu(); //构造函数中调用此函数  


    cpp文件:

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. void MainWindow::createItemMenu()  
    2. {  
    3.     m_grpMenu = new QMenu(this);  
    4.     m_memberMenu = new QMenu(this);  
    5.     m_addAreaMenu = new QMenu(this);  
    6.     connect(m_grpMenu,SIGNAL(triggered(QAction *)),  
    7.             this,SLOT(onGrpMenuTriggered(QAction *)));  
    8.     connect(m_memberMenu,SIGNAL(triggered(QAction *)),  
    9.             this,SLOT(onMemberMenuTriggered(QAction *)));  
    10.     connect(m_addAreaMenu,SIGNAL(triggered(QAction *)),  
    11.             this,SLOT(onAddAreaMenuTriggered(QAction *)));  
    12.   
    13.     m_grpAddOneMember = new QAction(tr("增加子区域"),this);  
    14.     m_grpAddOneMember->setIcon(QIcon(":/image/add.png"));  
    15.     m_grpAddOneGrp = new QAction(tr("增加设备"),this);  
    16.     m_grpAddOneGrp->setIcon(QIcon(":/image/add.png"));  
    17.     m_grpDeleteGrp = new QAction(tr("删除区域"),this);  
    18.     m_grpDeleteGrp->setIcon(QIcon(":/image/delete.png"));  
    19.   
    20.     m_memberDelete = new QAction(tr("删除设备"),this);  
    21.     m_memberDelete->setIcon(QIcon(":/image/delete.png"));  
    22.     m_memberMove = new QAction(tr("移动设备"),this);  
    23.     m_memberMove->setIcon(QIcon(":/image/next.png"));  
    24.     m_memberSyncTime = new QAction(tr("同步时间"),this);  
    25.     m_memberSyncTime->setIcon(QIcon(":/image/move.png"));  
    26.   
    27.     m_addAreaAction = new QAction(tr("增加区域"),this);  
    28.     m_addAreaAction->setIcon(QIcon(":/image/add.png"));  
    29.     m_addDev = new QAction(tr("增加设备"),this);  
    30.     m_addDev->setIcon(QIcon(":/image/add.png"));  
    31.     m_grpFlush = new QAction(tr("刷新列表"),this);  
    32.     m_grpFlush->setIcon(QIcon(":/image/update.png"));  
    33. }  
    34.   
    35. //重构contextMenuEvent函数,记得#include <QContextMenuEvent>  
    36. void MainWindow::contextMenuEvent(QContextMenuEvent *event)  
    37. {  
    38.     QTreeWidgetItem *item = ui->treeWidget->currentItem();  
    39.     if(item == NULL)  
    40.     {  
    41.         qDebug()<<"空白处";  
    42.         if (m_addAreaMenu->isEmpty())  
    43.         {  
    44.             //增加区域  
    45.             m_addAreaMenu->addAction(m_addAreaAction);  
    46.             m_addAreaMenu->addAction(m_addDev);  
    47.             m_addAreaMenu->addAction(m_grpFlush);  
    48.         }  
    49.         //菜单出现的位置为当前鼠标的位置  
    50.         m_addAreaMenu->exec(QCursor::pos());  
    51.     }  
    52.     else if(ui->treeWidget->currentItem()->childCount() > 0)  
    53.     {  
    54.         if (m_grpMenu->isEmpty())  
    55.         {  
    56.             //增加子区域  
    57.             m_grpMenu->addAction(m_grpAddOneMember);  
    58.             //删除区域  
    59.             m_grpMenu->addAction(m_grpDeleteGrp);  
    60.             m_grpMenu->addAction(m_grpAddOneGrp);  
    61.         }  
    62.         m_grpMenu->exec(QCursor::pos());  
    63.     }  
    64.     else if(ui->treeWidget->currentItem()->childCount() == 0)  
    65.     {  
    66.   
    67.         if (m_memberMenu->isEmpty())  
    68.         {  
    69.             //删除设备  
    70.             m_memberMenu->addAction(m_memberDelete);  
    71.             //移动设备  
    72.             m_memberMenu->addAction(m_memberMove);  
    73.             //同步时间  
    74.             m_memberMenu->addAction(m_memberSyncTime);  
    75.         }  
    76.         m_memberMenu->exec(QCursor::pos());  
    77.     }  
    78.   
    79.     event->accept();  
    80. }  
    81.   
    82. void MainWindow::onGrpMenuTriggered(QAction *action)  
    83. {  
    84.     if (action == m_grpAddOneMember)  
    85.     {//增加子区域  
    86.   
    87.     }  
    88.     else if (action == m_grpDeleteGrp)  
    89.     {//删除区域  
    90.   
    91.     }  
    92.     else if(action == m_grpAddOneGrp)  
    93.     {  
    94.   
    95.     }  
    96. }  
    97.   
    98. void MainWindow::onMemberMenuTriggered(QAction *action)  
    99. {  
    100.     if (action == m_memberDelete)  
    101.     {//删除设备  
    102.   
    103.     }  
    104.     else if(action == m_memberMove)  
    105.     {//移动设备  
    106.   
    107.     }  
    108.     else if(action == m_memberSyncTime)  
    109.     {//同步时间  
    110.   
    111.     }  
    112. }  
    113.   
    114. void MainWindow::onAddAreaMenuTriggered(QAction *action)  
    115. {  
    116.     if (action == m_addAreaAction)  
    117.     {//增加区域  
    118.   
    119.     }  
    120.     if(action == m_addDev)  
    121.     {//增加设备  
    122.   
    123.     }  
    124.     if(action == m_grpFlush)  
    125.     {//刷新  
    126.   
    127.     }  
    128. }  

    树型结构的基本用法:

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. void MainWindow::createTreeWidget()  
    2. {  
    3.     QIcon icon;  
    4.     icon.addPixmap(QPixmap(":/image/openStatus.png"), QIcon::Normal, QIcon::On);//节点打开状态  
    5.     icon.addPixmap(QPixmap(":/image/closeStatus.png"), QIcon::Normal, QIcon::Off);//节点关闭状态  
    6.   
    7.     ui->treeWidget->setColumnCount(1);  
    8.     QStringList headers;  
    9.     headers << QObject::tr("管理");  
    10.     ui->treeWidget->setHeaderLabels(headers);  
    11.   
    12.     QStringList rootName_1;  
    13.     rootName_1 << QObject::tr("常用文件夹");  
    14.     QTreeWidgetItem *rootItem_1 = new QTreeWidgetItem(ui->treeWidget, rootName_1);  
    15.    // rootItem_1->setIcon(0, QIcon(":/image/add.png")); //增加静态图标  
    16.     rootItem_1->setIcon(0,icon);  
    17.   
    18.   
    19.     QStringList childName_1_1;  
    20.     childName_1_1 << "所有未读";  
    21.     QTreeWidgetItem *childItem_1_1 = new QTreeWidgetItem(rootItem_1, childName_1_1);  
    22.   
    23.     rootItem_1->addChild(childItem_1_1);  
    24.   
    25.     QStringList childName_1_2;  
    26.     childName_1_2 << QObject::tr("置顶邮件");  
    27.     QTreeWidgetItem *childItem_1_2 = new QTreeWidgetItem(rootItem_1, childName_1_2);  
    28.   
    29.     rootItem_1->addChild(childItem_1_2);  
    30.   
    31.     QStringList rootName_2;  
    32.     rootName_2 << QObject::tr("我的邮箱");  
    33.     QTreeWidgetItem *rootItem_2 = new QTreeWidgetItem(ui->treeWidget, rootName_2);  
    34.   
    35.     rootItem_2->setIcon(0,icon);  
    36.   
    37.     QStringList childName_2_1;  
    38.     childName_2_1 << QObject::tr("收件箱");  
    39.     QTreeWidgetItem *childItem_2_1 = new QTreeWidgetItem(rootItem_2, childName_2_1);  
    40.   
    41.     rootItem_2->addChild(childItem_2_1);  
    42.   
    43.     QStringList childName_2_2;  
    44.     childName_2_2 << QObject::tr("草稿箱");  
    45.     QTreeWidgetItem *childItem_2_2 = new QTreeWidgetItem(rootItem_2, childName_2_2);  
    46.   
    47.     rootItem_2->addChild(childItem_2_2);  
    48.   
    49.     QStringList childName_2_3;  
    50.     childName_2_3 << QObject::tr("发件箱");  
    51.     QTreeWidgetItem *childItem_2_3 = new QTreeWidgetItem(rootItem_2, childName_2_3);  
    52.   
    53.     rootItem_2->addChild(childItem_2_3);  
    54.   
    55.     ui->treeWidget->addTopLevelItem(rootItem_1);  
    56.     ui->treeWidget->addTopLevelItem(rootItem_2);  
    57.   
    58.   
    59.     QStringList childName_1_2_1;  
    60.     childName_1_2_1 << "测试";  
    61.     QTreeWidgetItem *childItem_1_2_1 = new QTreeWidgetItem(childItem_1_2, childName_1_2_1);  
    62.     childItem_1_2->addChild(childItem_1_2_1);  
    63.   
    64. }  

    另一篇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

  • 相关阅读:
    How To Scan QRCode For UWP (4)
    How To Crop Bitmap For UWP
    How To Scan QRCode For UWP (3)
    How To Scan QRCode For UWP (2)
    How To Scan QRCode For UWP (1)
    How to change windows applicatioin's position via Win32 API
    8 Ways to Become a Better Coder
    How to resize or create a thumbnail image from file stream on UWP
    C# winform压缩文件夹带进度条
    MS ACCESS MID函数
  • 原文地址:https://www.cnblogs.com/findumars/p/5599436.html
Copyright © 2011-2022 走看看