在Qt中管理右键和左键
managing right and left clicks in Qt
我有一个复杂的Qt GUI,其中GUI的一部分显示具有各种数据点的绘图。
当前在绘图上左击或右击时,它返回单击位置的x-y值。我的工作是更新这一点,使左键点击仍然做同样的事情,但右键选择最近的数据点,并打开一个上下文菜单,允许我删除所述数据点。这个想法是手动能够去除异常值。
更新:我认为我已经找到了目前负责返回x-y值的代码段:
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();
}
就像我说的,我需要把它变成左击执行相同的操作,右击打开一个上下文菜单。如有任何建议,不胜感激。
您必须检查使用了哪个鼠标按钮。一般来说,我更喜欢处理mouseReleaseEvent
而不是mousePressEvent
,因为它可以更好地复制传统的鼠标行为。但这两件事都行得通。一个例子:
void mouseReleaseEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton) // Left button...
{
// Do something related to the left button
}
else if (e->button() == Qt::RightButton) // Right button...
{
// Do something related to the right button
}
}
如果你愿意,你也可以处理Qt::MidButton
相关文章:
- 如何将我的程序添加到文件和文件夹的macOS右键单击菜单?
- Qt - 右键单击时编辑按钮文本
- 如何处理鼠标右键单击qsystemtrayicon
- 当右键单击时,来自Menubar的Popupmenu
- 如何在列表控件中创建右键单击菜单
- QT:检测 QGraphicsItem 上的鼠标左键和右键按下事件
- Qt:鼠标右键单击QDockWidget标题
- 电子内部或外部方法创建自己的右键单击上下文菜单 - Windows
- 将项目插入桌面右键单击菜单从外壳扩展
- 如何使用C++禁用窗口上的右键单击按钮
- 如何处理列表控制标头中的右键单击事件
- 当鼠标右键单击(C /C#)时,如何获得快捷方式显示名称
- 右键单击按钮GTKMM-3.0的信号
- 右键单击系统托盘图标,然后单击C 中的上下文菜单
- 将菜单项添加到Windows Explorer右键单击Java中的上下文菜单
- 如何在打开的cv c ++中单击鼠标右键时从视频中捕获图像
- Windows 资源管理器右键单击文件和文件夹的上下文菜单
- C++-使用SendInput方法模拟按键,无法区分右键
- 如何显示 Windows 资源管理器上下文(右键单击)菜单
- 在Qt中管理右键和左键