zoukankan      html  css  js  c++  java
  • QT学习之窗口右键菜单

    QT学习之窗口右键菜单


    QWidget 及其子类都有右键菜单,主要因为其有两个与右键菜单相关联的函数:

    Qt::ContextMenuPolicy contextMenuPlicy() const
    void setContextMenuPolicy( Qt::ContextMenuPolicy policy)
    

    可以看到这里使用的是一个枚举类型:

    Constant Value Description
    Qt::NoContextMenu 0 the widget does not feature a context menu, context menu handling is deferred to the widget's parent.
    Qt::PreventContextMenu 4 the widget does not feature a context menu, and in contrast to NoContextMenu, the handling is not deferred to the widget's parent. This means that all right mouse button events are guaranteed to be delivered to the widget itself through mousePressEvent(), and mouseReleaseEvent().
    Qt::DefaultContextMenu 1 the widget's QWidget::contextMenuEvent() handler is called.
    Qt::ActionsContextMenu 2 the widget displays its QWidget::actions() as context menu.
    Qt::CustomContextMenu 3 the widget emits the QWidget::customContextMenuRequested() signal.

    Qt::NoContextMenu

    没有右键菜单,右键点击功能传递给widget的父窗口处理。

    Qt::DefaultContextMenu

    这个是默认的。利用右键菜单事件 QWidget::contextMenuEvent() 来处理右键事件,所以需要重写此函数!

    Qt::ActionsContextMenu

    通过动作事件添加右键菜单,(要给动作绑定信号槽!)

    MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    {
         setWindowTitle(tr("右键菜单显示"));
         //为窗口添加QActions
         addAction(new QAction(tr("&Open"), this));
         addAction(new QAction(QIcon(":/images/Opt.png"), tr("&Opt"), this));
         addAction(new QAction(tr("&Quit"), this));
         //设置contextMenuPolicy属性值为 '以Actions为弹出菜单的菜单项组成菜单
        setContextMenuPolicy(Qt::ActionsContextMenu);
    }
    
    QAction *ascendSortAction = new QAction("升序", this);
    QAction *descendSortAction = new QAction("降序", this);
    QAction *filterAction = new QAction("过滤", this);
    QAction *unfilterAction = new QAction("取消过滤", this);    
    connect(ascendSortAction, SIGNAL(triggered(bool)), this, SLOT(sort_ascend()));
    connect(descendSortAction, SIGNAL(triggered(bool)), this, SLOT(sort_descend()));
    connect(filterAction, SIGNAL(triggered(bool)), this, SLOT(filter_table()));
    connect(unfilterAction, SIGNAL(triggered(bool)), this, SLOT(unfilter_table()));
    datatable->horizontalHeader()->addAction(ascendSortAction);
    datatable->horizontalHeader()->addAction(descendSortAction);
    datatable->horizontalHeader()->addAction(filterAction);
    datatable->horizontalHeader()->addAction(unfilterAction); 
    

    Qt::CustomContextMenu

    这个枚举意味着会发出一个信号:
    void QWidget::customContextMenuRequested(const QPoint & pos) [signal]
    但其只是发送信号,所以要自己去写槽函数slot。信号发出的条件为:
    用户鼠标右击且被击中的Widget的contextMenuPolicy 又是Qt::CustomContextMenu

    pos是该widget接收右键菜单事件的位置,一般是在该部件的坐标系中。但是对于QAbstratScrollArea及其子类例外,是对应着其视口viewport()的坐标系。如常用的QTableView、QHeaderView就是QAbstratScrollArea的子类。
    因为仅发信号,所以需自己写显示右键菜单的slot来响应。

    例如窗口显示右键菜单槽:

    MyWidget::MyWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::MyWidget)
    {
        ui->setupUi(this);
        popMenu = new QMenu(this); //popMenu为类私有成员
        QAction *addDir = popMenu->addAction("增加目录");
        connect(addDir, SIGNAL(triggered(bool)), this, SLOT(popAddDir()));
        QAction *addTemplate = popMenu->addAction("增加模板");
        connect(addTemplate, SIGNAL(triggered(bool)), this, SLOT(popAddTemplate()));
    
        setContextMenuPolicy(Qt::CustomContextMenu);
        connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
                this, SLOT(sltShowPopMenu(const QPoint&)));
    
    }
    void MyWidget::sltShowPopMenu(const QPoint& )//槽函数
    {
        if(popMenu){
            popMenu->exec(QCursor::pos());
        }
    }
    

    void QWidget::contextMenuEvent ( QContextMenuEvent * event ) [virtual protected]

    重写 QWidget 的被保护的虚函数

    MyWidget::MyWidget(QWidget *parent)
        : QWidget(parent)
    {
        setWindowTitle(tr("Context Menu Show 2"));
        setContextMenuPolicy(Qt::DefaultContextMenu); //其实不用设置,默认就是这个值
    }
    void MyWidget::contextMenuEvent(QContextMenuEvent *event)
    {
        QMenu *menu = new QMenu(this);
        menu->addAction(new QAction(tr("&Open"), menu));
        menu->addAction(new QAction(QIcon(":/images/mark.png"), tr("&Mark"), menu));
        menu->addAction(new QAction(tr("&Quit"), menu));
        menu->move(cursor().pos()); //让菜单显示的位置在鼠标的坐标上
        menu->show();
    }
    

    http://tmjfzy.blog.163.com/blog/static/6644702520126523645391/
    http://blog.sina.com.cn/s/blog_98a4dde701013dzh.html
    http://blog.csdn.net/addfourliu/article/details/7164923

  • 相关阅读:
    PHP中使用CURL实现GET和POST请求
    PHP 正则表达式匹配函数 preg_match 与 preg_match_all
    微信跳转黑科技:微信跳转技术让微信电商从此不缺流量
    PHP通过get方法获得form表单数据方法总结
    php获取微信基础接口凭证Access_token
    PHP命名空间与自动加载类详解
    PHP如何搭建百度Ueditor富文本编辑器
    PHP调用微博接口实现微博登录的方法示例
    PHP常用日期加减计算方法实例
    微信公众平台---带参数二维码生成和扫描事件
  • 原文地址:https://www.cnblogs.com/lomper/p/4268888.html
Copyright © 2011-2022 走看看