C++ Qt 中的右键单击事件以打开上下文菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24254006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:40:47  来源:igfitidea点击:

rightclick event in Qt to open a context menu

c++qteventscontextmenu

提问by ahle6481

I have a segment of code that calls a mousePressEvent. I have the left-click output the coordinates of the cursor, and I have rightclick do the same, but I also want to have the rightclick open a context menu. The code I have so far is:

我有一段调用 mousePressEvent 的代码。我有左键单击输出光标的坐标,我有右键单击做同样的事情,但我也想让右键单击打开上下文菜单。我到目前为止的代码是:

void plotspace::mousePressEvent(QMouseEvent*event)
{
    double trange = _timeonright - _timeonleft;
    int twidth = width();
    double tinterval = trange/twidth;

    int xclicked = event->x();

    _xvaluecoordinate = _timeonleft+tinterval*xclicked;



    double fmax = Data.plane(X,0).max();
    double fmin = Data.plane(X,0).min();
    double fmargin = (fmax-fmin)/40;
    int fheight = height();
    double finterval = ((fmax-fmin)+4*fmargin)/fheight;

    int yclicked = event->y();

    _yvaluecoordinate = (fmax+fmargin)-finterval*yclicked;

    cout<<"Time(s): "<<_xvaluecoordinate<<endl;
    cout<<"Flux: "<<_yvaluecoordinate<<endl;
    cout << "timeonleft= " << _timeonleft << "\n";

    returncoordinates();

    emit updateCoordinates();

    if (event->button()==Qt::RightButton)
    {
            contextmenu->setContextMenuPolicy(Qt::CustomContextMenu);

            connect(contextmenu, SIGNAL(customContextMenuRequested(const QPoint&)),
            this, SLOT(ShowContextMenu(const QPoint&)));

            void A::ShowContextMenu(const QPoint &pos) 
            {
                QMenu *menu = new QMenu;
                menu->addAction(tr("Remove Data Point"), this,  
                SLOT(test_slot()));

                menu->exec(w->mapToGlobal(pos));
            }

    }   

}

I know that my problem is very fundamental in nature, and that 'contextmenu' is not properly declared. I have pieced together this code from many sources, and do not know how to declare something in c++. Any advice would be greatly appreciated.

我知道我的问题本质上是非常基本的,并且没有正确声明“上下文菜单”。我从许多来源拼凑了这段代码,但不知道如何在 C++ 中声明某些东西。任何建议将不胜感激。

回答by Nejat

customContextMenuRequestedis emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu, and the user has requested a context menu on the widget. So in the constructor of your widget you can call setContextMenuPolicyand connect customContextMenuRequestedto a slot to make a custom context menu.

customContextMenuRequested当小部件的 contextMenuPolicy 为Qt::CustomContextMenu并且用户已请求小部件上的上下文菜单时发出。因此,在您的小部件的构造函数中,您可以调用setContextMenuPolicy并连接customContextMenuRequested到一个插槽以制作自定义上下文菜单。

In the constructor of plotspace:

在 的构造函数中plotspace

this->setContextMenuPolicy(Qt::CustomContextMenu);

connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), 
        this, SLOT(ShowContextMenu(const QPoint &)));

ShowContextMenuslot should be a class member of plotspacelike :

ShowContextMenuslot 应该是plotspacelike的类成员:

void plotspace::ShowContextMenu(const QPoint &pos) 
{
   QMenu contextMenu(tr("Context menu"), this);

   QAction action1("Remove Data Point", this);
   connect(&action1, SIGNAL(triggered()), this, SLOT(removeDataPoint()));
   contextMenu.addAction(&action1);

   contextMenu.exec(mapToGlobal(pos));
}