PDA

View Full Version : Change the position of Label of the QwtPlotMarker



kerberos84
24th June 2013, 13:41
Hello,

i am trying to change the position of the label of the qwtplotmarker. (User will drag it to another position but only label should move. not the marker.)

is it possible?

if no, what can i do? Can i use QwtText and add it to any positon(x,y) on the plot?

Uwe
24th June 2013, 21:13
Drag an drop could be done using a QwtPlotPicker, where the text label is used as tracker text. Or you can do it like in the itemeditor example.
The position of the label is always aligned to the marker position, but in your case you probably want to set an offset in pixels. For this you have to derive from QwtPlotMarker and overload QwtPlotMarker::drawLabel().



Uwe

kerberos84
26th June 2013, 13:22
Hello,
I have tried to sub-class and override draw method or drawLabel method. But these two methods are "const". I can not save the label position, because these are "const" methods.

I just have to save the label coordinates, in order to select it and move to another position like in the itemeditor example.

Do you have any suggestions?

Uwe
27th June 2013, 06:40
There are ways to drive around the constness in C++, but they are of no importance here as a draw method is for drawing - not for changing a position.

You derived class has to add a new member for the offset ( in pixels ) from the marker position, that is used by your overloaded drawLabel method. Setting the offset has to be done on a selected signal of the picker.

Uwe

kerberos84
27th June 2013, 14:22
Hello i am using qwt 5.2 and qt 4.6 versions.

1) I checked the itemeditor example and hence qwt_widget_overlay class does not exist in qwt 5.2 i copied it to my project and tried to compile it.

But when i compile it i get a linking error for the line "QMetaObject::invokeMethod" line. I think the method trying to be invoked does not exist in my qwt version, right? (QwtPlotCanvas::borderPath).

What can i do in this situation? Please help, i have written everything for moving label, only this left.


void QwtWidgetOverlay::draw( QPainter *painter ) const
{
QWidget *widget = const_cast< QWidget *>( parentWidget() );
if ( widget )
{
painter->setClipRect( parentWidget()->contentsRect() );

// something special for the plot canvas
QPainterPath clipPath;

( void )QMetaObject::invokeMethod(
widget, "borderPath", Qt::DirectConnection,
Q_RETURN_ARG( QPainterPath, clipPath ), Q_ARG( QRect, rect() ) );

if (!clipPath.isEmpty())
{
painter->setClipPath( clipPath, Qt::IntersectClip );
}
}

drawOverlay( painter );
}

2) in the QwtWidgetOverlay class it calls "QwtPlotCanvas::invalidateBackingStore" method. Again this method does not exist in qwt 5.2. There is a method called "invalidateCache" in qwt 5.2.
Does it do the similar things? Can i use it?

Uwe
27th June 2013, 14:56
The tracker text of the QwtPlotPicker is implemented as a widget overlay moving a text around. If you overload its trackerText() method - always returning the text of the selected label - you would have an implementation without having to backport from Qwt 6.1.
When using your code above you can simply remove the lines 8-18. They are for setting a clip for a canvas with rounded borders - something you don't have in Qwt 5.x.
Yes you can use invalidateCache instead.



Uwe

kerberos84
28th June 2013, 10:06
Thanks for your answers Uwe,

I have managed it by backporting qwtwidgetoverlay class to my code.

I have another question. There is a zoom function in my plot so i use qwtplotzoomer to zoom in. So currently while i am dragging the label it is also drawing the zoom rectangle and when i release mouse button it zooms in. How can i disable Zooming while i am dragging a label?

I tried to call zoomer->setEnable(false) when mousePressed and acceptted dragging and zoomer->setEnable(true) when mouse is released but it does not work.

Uwe
30th June 2013, 13:01
There is a zoom function in my plot so i use qwtplotzoomer to zoom in. So currently while i am dragging the label it is also drawing the zoom rectangle and when i release mouse button it zooms in. How can i disable Zooming while i am dragging a label?
Well using the same mouse button for zooming and moving a marker is IMHO not a good idea - at least from the point of view of usability. So I recommend to use different modes or mouse button/key combinations.

But if you really need to do so: both input handlers install an event filter for the plot canvas.



Event filters are called in reverse order to how they have been installed.
When an event filter returns true the event it is not propagated any further


So you could simply set up the zoomer first and return true from the event filter of your marker editor, when a click position matches a label.

Uwe

kerberos84
1st July 2013, 12:20
Thanks for your answers.

I decided to use dragging with a mouse click + key combinitaion.