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
rightclick event in Qt to open a context menu
提问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
customContextMenuRequested
is 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 setContextMenuPolicy
and connect customContextMenuRequested
to 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 &)));
ShowContextMenu
slot should be a class member of plotspace
like :
ShowContextMenu
slot 应该是plotspace
like的类成员:
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));
}