I have a requirement for my project to be able to draw a vertical or
horizontal line at the current mouse position a given plot. The vertical
line should extend from the top of the canvas to the bottom at the mouses
current x position. The horizontal line should extend from the left side of
the canvas to the right at the mouses current y position.
I already have a QwtPlotZoomer that i have attached to the plot so i
decided to subclass QwtPlotZoomer and override the drawTracker(QPainter*)
function (below is the implementation).
Note the following are member variables defined in the header:
void CustomZoomer::drawTracker(QPainter* pPainter) const
// is there an orientation?
// no orientation
// trackerPosition() returns a point with x = -1, y = -1 when mouse is
// outside the plot
const QPoint tracker_pos = trackerPosition();
if (tracker_pos.x() < 0 || tracker_pos.y() < 0)
// mouse is outside the plot
// ensure the pen is valid
if (mTrackerLinesPen.style() != Qt::NoPen &&
mTrackerLinesPen.width() > 0)
// horizontal line
if (mTrackerLinesOrientations & Qt::Horizontal)
const QPoint left_point(0, tracker_pos.y());
const QPoint right_point(canvas()->width(), tracker_pos.y());
// vertical line
if (mTrackerLinesOrientations & Qt::Vertical)
const QPoint top_point(tracker_pos.x(), 0);
const QPoint bottom_point(tracker_pos.x(), canvas()->height());
} // end pen is valid
This code simply would not draw my vertical or horizontal line. After some
digging i found the QwtPicker::PickerWidget::updateMask() function which
sets the mask of the text widget to the the bounding box around the tracker
text. I added the following code to the function before i started painting:
QWidget* widget_device = dynamic_cast<QWidget*>(pPainter->device());
if (widget_device != NULL)
// clear the mask so there is no restriction to where we are painting.
Does setting this mask provide a significant performance improvement?
Is clearing the mask like i've shown above a bad idea, if do you have any
suggestions on how i can implement these vertical/horizontal lines (i was
trying to avoid creating widget that attaches to the canvas, but if i have
to that's ok)?