Reputation: 743
I have a complex Qt GUI, where one part of the GUI displays a plot with various data points.
When currently left or right clicking on the plot, it returns the x-y values at the location of the click. My job is to update this so that left-click still does the same thing, but right-click selects the nearest data point and opens a context menu allows me to remove said data point. The idea is to manually be able to remove outliers.
UPDATED: I THINK that I have found the segment of code currently responsible for returning x-y values:
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();
}
Like I said, I need to turn this into left-clicking performing the same action, and right-clicking opening a context menu. Any advice would be greatly appreciated.
Upvotes: 1
Views: 495
Reputation: 1498
You have to check which mouse button was used. Generally I prefer to deal with the mouseReleaseEvent
instead of mousePressEvent
as it replicates better the conventional mouse behavior. But both events work. An exemple:
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
}
}
You can also deal with the Qt::MidButton
if so you wish.
Upvotes: 1