Question

I need to get x and y coordinates of mouse click on QwtPlot drawing area (not the whole widget!). The problem is that there's only method bool event(QEvent*) that is called on every event. I've found some solutions (http://www.qtcentre.org/archive/index.php/t-9502.html), using QwtPlotPicker but that doesn't work for me, I'm using Qwt 6 and there are no such methods like setSelectionState().

What is other methods for achieving mouse click events on drawing area in QwtPlot?

Was it helpful?

Solution

There's been some changes in Qwt 6 comparing to 5.

Now we need to set state machine using QwtPlotPicker::setStateMachine(QwtPickerMachine) method. There are a few options (derived classes):

  • QwtPickerClickPointMachine
  • QwtPickerClickRectMachine
  • QwtPickerDragPointMachine
  • QwtPickerDragRectMachine
  • QwtPickerPolygonMachine
  • QwtPickerTrackerMachine

depending on our needs.

Next thing we need to do is connect() signal selected(...) from QwtPlotPicker with our custom slot where we can obtain x and y coordinates or other interesting data.

OTHER TIPS

I'm capturing the mouse events using the mouse event methods in a subclass of QwtPlotCanvas, and using the conversion methods in QwtPlot to map the mouse event x and y coordinates to the values they represent in the plot.

Create a subclass of QwtPlotCanvas and override the mousePressEvent method like this.

void SpecialMapPlotCanvas::mousePressEvent (QMouseEvent* event) {
    QWidget::mousePressEvent (event);
    double x = plot() -> invTransform (plot() -> xBottom, event -> pos().x());
    double y = plot() -> invTransform (plot() -> yLeft, event -> pos().y());
    std::cout << "Values " << x << " " << y << "\n";
}

Then set the canvas on the QwtPlot by instantiating an object of this class and passing it to QwtPlot::setCanvas. Then SpecialMapPlotCanvas::plot() gives you a reference to the owning QwtPlot, and its invTransform methods can be used to convert the click coordinates to plot values. If you use the mouse events on QwtPlot itself you get the wrong answers because the mouse event coordinates here relate to the whole QwtPlot widget area (as you say), not just the canvas.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top