简体   繁体   English

如何在 QGraphicsView 中为右键单击创建上下文菜单

[英]How to create a context menu for right click in QGraphicsView

I created a graphics view using the designer in Qt creator.我使用 Qt creator 中的设计器创建了一个图形视图。

The hierarchy is the following:层次结构如下:

在此处输入图像描述

When I do a right click in the area of my graphics view nothing happens (which is the default behavior afaik).当我在图形视图区域中单击鼠标右键时,什么也没有发生(这是默认行为 afaik)。 Since I'm trying to show a drop down menu on right click but I can't get it to work I added a connect and custom slot.由于我试图在右键单击时显示一个下拉菜单,但我无法让它工作,我添加了一个连接和自定义插槽。

Here is my code:这是我的代码:


MainWindow::MainWindow(QWidget *parent) :
   QMainWindow(parent),
   ui(new Ui::MainWindow)
{
   ui->setupUi(this);
   MainWindow:: makePlot();
=
   ui->graphView->setContextMenuPolicy(Qt::CustomContextMenu);
   
   QObject::connect(ui->graphView ,SIGNAL(customContextMenuRequested(const QPoint &)),this,SLOT(on_graphView_customContextMenuRequested(const QPoint &)));
   scene = new QGraphicsScene();
   ui->graphView->setScene(scene);
...
}

And the slot function that is called to handle the menu:以及调用来处理菜单的插槽 function:

` `

void MainWindow::on_graphView_customContextMenuRequested(const QPoint &pos)
{

    qDebug() << " custom menuu!!!!!!!!!"<<  "##"<< pos.x() << "  ;  "<< pos.y();
//    QMenu menu(this);
//        menu.addAction("...1");
//        menu.addAction("...2");
//        menu.exec(pos);
}

` `

The problem (s):问题:

  1. the qdebug string is printed twice on my console on each single right click, why does that happen?每次右键单击时,qdebug 字符串都会在我的控制台上打印两次,为什么会这样? It is supposed to appear only once...它应该只出现一次...

  2. I'm drawing Qgraphicsitems on my scene, the on_graphView_customContextMenuRequested seems to be triggered even when I right click a drawn item and not only a blank space in the scene, how do I get it to trigger only for graphview, in areas with nothing on top (no overlay of item on scene).我在我的场景上绘制 Qgraphicsitems,on_graphView_customContextMenuRequested 似乎被触发,即使我右键单击一个绘制的项目,而不仅仅是场景中的空白区域,我如何让它只为 graphview 触发,在顶部没有任何东西的区域(场景上没有项目覆盖)。

  3. the coordinates in pos are relative the graphview dimensions, therefore when I uncomment the menu.exec in the above code, the menu is shown in a wrong location. pos 中的坐标是相对于 graphview 尺寸的,因此当我在上面的代码中取消注释menu.exec时,菜单显示在错误的位置。 How do I convert my pos to global coordinates relative to the entire Windows and not only the graphicsview.如何将我的 pos 转换为相对于整个 Windows 而不仅仅是图形视图的全局坐标。

Explanation:解释:

  1. It gets 2 impressions because the on_graphView_customContextMenuRequested slot is invoked 2 times since there are 2 connections.它获得 2 次展示,因为 on_graphView_customContextMenuRequested 插槽被调用了 2 次,因为有 2 个连接。 The first connection is the one made by using QtDesigner that uses the uic tool to generate C++ code using the.ui, this connection is automatic if the name of the slot complies with the void on_<object name>_<signal name>(<signal parameters>);第一个连接是使用 QtDesigner 建立的,使用 uic 工具生成 C++ 代码,使用 .ui,如果插槽名称符合void on_<object name>_<signal name>(<signal parameters>); rule (See this ), and the second connection is the one you show in the code.规则(参见this ),第二个连接是您在代码中显示的连接。 There are several options:有几种选择:

    1.1 Change the name of the slot. 1.1 更改插槽的名称。

    1.2 Remove the connection that you show. 1.2 删除您显示的连接。

  2. The customContextMenuRequested signal is emitted whether or not there is an item, the solution in that case is to filter if there is an item or not. customContextMenuRequested 信号无论是否有项目都会发出,这种情况下的解决方案是过滤是否有项目。

  3. The exec() method must use the global position but the customContextMenuRequested signal passes the position with respect to the widget that emits the signal, so you must convert it to global position. exec() 方法必须使用全局 position 但 customContextMenuRequested 信号通过 position 相对于发出信号的小部件,因此您必须将其转换为全局 Z4757FE07FD492A8BEEZEA6A760D683D6。

MWE: MWE:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->graphView->setContextMenuPolicy(Qt::CustomContextMenu);
    scene = new QGraphicsScene();
    ui->graphView->setScene(scene);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_graphView_customContextMenuRequested(const QPoint &pos)
{
    if(ui->graphView->itemAt(pos))
        return;
    QMenu menu(this);
    menu.addAction("...1");
    menu.addAction("...2");
    menu.exec(ui->graphView->mapToGlobal(pos));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在列表框上右键单击创建上下文菜单? - How to create a context menu on listbox right click? 如何将右键单击菜单操作连接到 QGraphicsView 中的某些 function? - How to connect right click menu action to some function in QGraphicsView? 如何添加Windows右键单击上下文菜单项? - How can I add Windows right click context menu items? 如何显示 Windows 资源管理器上下文(右键单击)菜单? - How to show Windows Explorer context (right click) menu? 电子内部或外部方法来创建自己的右键单击上下文菜单-Windows - Electron internal or external method to create own right click context menu - Windows 如何在列表控件中创建右键菜单? - How do I create a right-click menu in a list control? 如何在 Qt 中使用 eventFilter 创建鼠标右键单击菜单选项? - How to create Mouse right click menu option using eventFilter in Qt? 如何用我自己的文件打开对话框替换默认的右键单击上下文菜单 - How do I replace the default right click context menu in a file open dialog box with my own 如何获得右键单击文件夹背景的完整Shell上下文菜单 - How to obtain full shell context menu of right-click a folder background 如何在Qt Creator中编辑QtWebKit的右键单击上下文菜单? - How to edit QtWebKit's right-click context menu in Qt Creator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM