zoukankan      html  css  js  c++  java
  • Qt532.【转】Qt创建鼠标右键菜单

    ZC:可以通过 设置  (QWebView*)->setContextMenuPolicy(NoContextMenu); 来关闭 QWebView的默认右键菜单

    Qt创建鼠标右键菜单_疯华正茂_新浪博客.html(http://blog.sina.com.cn/s/blog_63d0ff2d0102vetz.html

    网页内容保存:

    第一步:

    QWidget及其子类都可有右键菜单,首先设置QWidget与右键菜单有关的函数setContextMenuPolicy()。

    如设置QTreeView的相关函数为:this->ui.treeView->setContextMenuPolicy(Qt::CustomContextMenu);

    Qt::ContextMenuPolicy枚举类型包括:Qt::DefaultContextMenu, Qt::NoContextMenu, Qt::PreventContextMenu, Qt::ActionsContextMenu, and Qt::CustomContextMenu,其中如果设置该类型为Qt::CustomContextMenu,则点击鼠标右键会发射信号customContextMenuRequested(const QPoint)。

    第二步:

    在头文件中声明右键关联的槽函数ShowMouseRightButton(const QPoint); 并且设置信号customContextMenuRequested(const QPoint)与该槽函数的关联,例如:connect(ui.treeView, SIGNAL(customContextMenuRequested(const QPoint)), this, SLOT(ShowMouseRightButton(const QPoint)));

    第三步:

    实现对应的右键关联的槽函数,如ShowMouseRightButton(const QPoint);

    void ShowMouseRightButton(const QPoint& pos)
    {
           QMenu *qMenu = NULL;

           if (qMenu)
           {
                   delete qMenu;
                   qMenu = NULL;
           }

           qMenu = new QMenu(ui.treeView);

           QAction* closePaneAction = new QAction("&Close",this);
           connect(closePaneAction, SIGNAL(triggered()), this, SLOT(close()));

           QAction* addTreeItemAction = new QAction("&AddItem", this);
           connect(addTreeItemAction, SIGNAL(triggered()), this, SLOT(AddTreeItem()));

           qMenu->addAction(closePaneAction);
           qMenu->addAction(addTreeItemAction);

           qMenu->exec(QCursor::pos()); //在鼠标点击的位置显示鼠标右键菜单

    }

    Z

  • 相关阅读:
    9.对话框
    8.布局管理器
    7.对象模型
    6.添加动作
    5.Qt模块简介
    4.自定义信号槽
    3.信号槽
    2.Helloworld
    1.Qt简介
    Problem E: 成绩排序
  • 原文地址:https://www.cnblogs.com/cppskill/p/9018206.html
Copyright © 2011-2022 走看看