PDA

View Full Version : Crosshairs that are always on



xburgerhout
14th November 2008, 12:50
Hi all,

I'm trying to create some crosshairs that follow the mouse. The effect I'm trying to achieve is that of QwtPicker(PointSelection, CrossRubberBand, AlwaysOn), but without having to click the mouse to activate the rubberband.

Of course, I can probably achieve this by using two plot markers that follow the mouse position (or drawing the lines directly in a paintEvent()), but I was wondering if there is an easier solution / better way to do it.

rbp
9th February 2009, 05:03
I'm not familiar with that Qwt function, but you could use this to get a crosshairs:

QApplication::setOverrideCursor(Qt::CrossCursor);
http://doc.trolltech.com/4.4/qt.html#CursorShape-enum

xburgerhout
9th February 2009, 14:00
That's not the effect I had in mind, but thanks for the reply anyway.

What I want is for the cross to span the entire plot canvas. This is what you get when you create a picker with PointSelection and a CrossRubberband. Clicking the mouse shows the cross rubberband on the plot at the current mouse location. However, you will need to click the mouse, and I want the CrossRubberband to be shown always.

As far as I can tell from the qwt-5.2 (svn) sources, qwt only draws the CrossRubberband in 'PointSelection' mode.

Using a plot marker has the disadvantage that moving the mouse results in a repaint off the entire plot, so this is a solution that will only work if the plot is simple.

Alternatively, it is possible to implement an overlay widget that paints the cross (this is exactly what qwt does for the CrossRubberband, that's why I was hoping I could use a plot picker).

Uwe
10th February 2009, 08:26
class PickerMachine: public QwtPickerMachine
{
public:
virtual QwtPickerMachine::CommandList transition(
const QwtEventPattern &, const QEvent *e)
{
QwtPickerMachine::CommandList cmdList;
if ( e->type() == QEvent::MouseMove )
cmdList += Move;

return cmdList;
}
};

class Picker: public QwtPlotPicker
{
public:
Picker(QwtPlotCanvas *canvas):
QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, canvas)
{
setRubberBand(QwtPlotPicker::CrossRubberBand);
setRubberBandPen(QColor(Qt::green));
setRubberBand(QwtPicker::CrossRubberBand);

canvas->setMouseTracking(true);
}

void widgetMouseMoveEvent(QMouseEvent *e)
{
if ( !isActive() )
{
setSelectionFlags(QwtPicker::PointSelection);

begin();
append(e->pos());
}

QwtPlotPicker::widgetMouseMoveEvent(e);
}

void widgetLeaveEvent(QEvent *)
{
end();
}

virtual QwtPickerMachine *stateMachine(int) const
{
return new PickerMachine;
}
};



HTH,
Uwe

xburgerhout
10th February 2009, 12:45
Thanks Uwe,

That's exactly what I was looking for.