PDA

View Full Version : Dragging a vertina line (x=const) on graph (QwtPlot) and return its x value



shadowzzz
25th March 2014, 23:15
I have gone through the examples, but couldn't figure out how to implement it. Ideally I can use Qt Designer to set up the UI (since I need other inputs as well, and its easier to make in Designer than hardcoding it from scratch).

I would like to have 2 lines on my plot that act as x limits to a 2D curve. The curve should be draggable along the x axis, and once the mouse is released, a function will run to update the graph.

Many thanks

Qt Creator 3.0.1, Qt 5.2.1, qwt 6.1.0, Mac OS 10.9

Cah
26th March 2014, 11:08
I have a Ruler class that does exactly what you want

The class is a subclass of QwtPlotMarker and "HAS A" subclass of QwtPlotPicker

I will find a way to share the files with you.. Its a little to bulky for here..

I will do this within the next 24hrs

shadowzzz
26th March 2014, 15:12
thats absolutely wonderful
thank you so much =D

Cah
27th March 2014, 02:19
Here you go ...
http://sourceforge.net/projects/caruler/?source=directory

I be happy to learn of any bugs, improvements, etc.

shadowzzz
27th March 2014, 15:00
Its been great so far! Thank you so much for the code.

shadowzzz
28th March 2014, 09:41
Is it possible to offset/manipulate the label of the ruler when you are dragging it? Say the ruler is for limiting something from a point that is not x=0, and I would like a label that reflects the offset.

Many thanks

Cah
28th March 2014, 14:18
Is it possible to offset/manipulate the label of the ruler when you are dragging it? Say the ruler is for limiting something from a point that is not x=0, and I would like a label that reflects the offset.


It sounds like you want to be able to track relative positions. I do not know why... but, with a little work, it can be done.

In the picker.h, augment the class by adding a variable _xReference and the accessor functions. Be sure to initialize _xReference in the picker constructor.


class Picker: public QwtPlotPicker
{
Q_OBJECT
public:
....
void setReferenePosition(double pos);
double referenePosition()const;
protected:
....

double _xReference;
};
...
inline void setReferenePosition(double pos)
{
_xReference = pos;
}

inline double referenePosition()const {return _xReference ;}
...


In picker.cpp, make a change to trackerTextF()

replace... return xTitle.text() + "=" + QString::number(_rulerPos);
with... return xTitle.text() + "=" + QString::number(_rulerPos - _xReference);

and
replace... return QString::number(_rulerPos);
with... return QString::number(_rulerPos- _xReference);


Client side
...
Ruler* rv1 = new RulerV(_plot, "Vertical_1");
rv1->picker()->setReferenePosition(2);
...

Dragging rv1 to a absolute position 5.0 displays a tracking text (relative) of 3.0

Let me know how this went.

shadowzzz
28th March 2014, 14:31
Works perfectly.
Thank you so much.

Cah
28th March 2014, 17:18
Ok..
But it is a intrusive way to add new functionality. Not the C++ way.

A better solution may be to provide a member function in the Ruler class that allows clients to install a Picker of their choice.

Thus,
...
void setRulerPicker(Picker* picker)

client do..

class CustomPicker: public Picker
{
....
QwtText trackerTextF (const QPointF & pt) const; //reimplemented

void setReferenePosition(double pos);
private:
double _xReference;
...
};

Ruler* rv1 = new RulerV(_plot, "Vertical_1");
rv1->setRulerPicker(new CustomPicker(...));

...
((CustomPicke*)rv1->picker())->setReferenePosition(2);
...

As you may suspect, the Ruler class is a stripped down version... I'll tidy up things and commit. You could expect to find a ruler editor in the form of a right click menu. I'll augmented Ruler class to provide setRulerPicker()

I'll try to get this done within the next 24hrs or so

Cah
29th March 2014, 14:59
Added right click menu and a little more
see..http://sourceforge.net/projects/caruler/?source=directory