K
Kamran
Hi
I have very little experience of C++, nevertheless I have
been asked to write a gui using QT/QWT.
I know that I should direct the question to the relevant
mailing list and I have done that but I think my problem
has to do with my lack of understandign of some issues in C++.
There is a class in QWT called QwtPicker which allows one to
make rubberbands and select part of the drawing canvas, very
useful in zooming etc.
Function 'cursorLabel()'in the QwtPicker class makes a marker
(a text string) on the canvas that displays the position of the
cursor when moving the cursor.
Now instead of displaying the psoition I want to replace
this text with another (displaying times of some curves I have
drawn). So I have been told to overload the aforementioned function.
The Qwtpicker class has a constructor :
---------------------
QwtPicker::QwtPicker(int selectionFlags, RubberBand rubberBand,
DisplayMode cursorLabelMode, QWidget *parent):
QObject(parent, name)
{
init(parent, selectionFlags, rubberBand, cursorLabelMode);
}
---------------------
and the cursorLabel function takes a struct containing cursor position
(QPoint &pos) and returns a QString which is the text displayed on
the canvas:
---------------------
QString QwtPicker::cursorLabel(const QPoint &pos) const
{
QString label;
switch(rubberBand())
{
case HLineRubberBand:
label.sprintf("%d", pos.y());
break;
case VLineRubberBand:
label.sprintf("%d", pos.x());
break;
default:
label.sprintf("%d, %d", pos.x(), pos.y());
}
return label;
}
------------------
I overload the QwtPicker, dataPicker which looks like:
------------------
class dataPicker: public QwtPicker // subclassing Picker
{
// here I send one extra parameter "QString &str" to my picker
public:
dataPicker(QString &str, int selectionFlags,
RubberBand rubberBand, DisplayMode cursorLabelMode,
QwtPlotCanvas *canvas):
QwtPicker(selectionFlags, rubberBand, cursorLabelMode, canvas),
picker_s(str)
{
cout << picker_s << endl; // this shows correct text
}
virtual QString cursorLabel(const QPoint &pos) const //overloading
{
QString label = QString(picker_s);
cout << picker_s << endl; // this shows wrong text
return label;
}
private:
QString picker_s;
};
---------------
and make a new dataPicker in my eventhandling:
-----------------------------
case QEvent::MouseMove:
{
// building the string ds and passing it to dataPicker
ds.sprintf("%02d.%02d.%02d.%03d",t.hour(),t.minute(),
t.second(),millisecs);
dpicker = new dataPicker(ds,
dataPicker:ointSelection | dataPicker:ragSelection,
dataPicker::VLineRubberBand, dataPicker::AlwaysOn,
this->canvas());
------------------------------
The problem is that when the cursor first enters the canvas and displays
the text at the desired position then it hangs on to this first value
and evenif I can see the text changing when I move the cursor it
then rolls back to its initial text. I have put a 'cout << picker_s'
statement in my overloaded cursorLabel function and print
out the value and observe the same problem reassuring me that
it is not the graphics. Then I put the 'cout << picker_s' in
the dataPicker constructor and there it is all ok !
Seems to me that I havn't quite grasped the overloading concept.
Why does the variable 'picker_s' behaves like this in the
overload function ?
Sorry for the lengthy post and hope you people can help me.
Thanks
Kamran
I have very little experience of C++, nevertheless I have
been asked to write a gui using QT/QWT.
I know that I should direct the question to the relevant
mailing list and I have done that but I think my problem
has to do with my lack of understandign of some issues in C++.
There is a class in QWT called QwtPicker which allows one to
make rubberbands and select part of the drawing canvas, very
useful in zooming etc.
Function 'cursorLabel()'in the QwtPicker class makes a marker
(a text string) on the canvas that displays the position of the
cursor when moving the cursor.
Now instead of displaying the psoition I want to replace
this text with another (displaying times of some curves I have
drawn). So I have been told to overload the aforementioned function.
The Qwtpicker class has a constructor :
---------------------
QwtPicker::QwtPicker(int selectionFlags, RubberBand rubberBand,
DisplayMode cursorLabelMode, QWidget *parent):
QObject(parent, name)
{
init(parent, selectionFlags, rubberBand, cursorLabelMode);
}
---------------------
and the cursorLabel function takes a struct containing cursor position
(QPoint &pos) and returns a QString which is the text displayed on
the canvas:
---------------------
QString QwtPicker::cursorLabel(const QPoint &pos) const
{
QString label;
switch(rubberBand())
{
case HLineRubberBand:
label.sprintf("%d", pos.y());
break;
case VLineRubberBand:
label.sprintf("%d", pos.x());
break;
default:
label.sprintf("%d, %d", pos.x(), pos.y());
}
return label;
}
------------------
I overload the QwtPicker, dataPicker which looks like:
------------------
class dataPicker: public QwtPicker // subclassing Picker
{
// here I send one extra parameter "QString &str" to my picker
public:
dataPicker(QString &str, int selectionFlags,
RubberBand rubberBand, DisplayMode cursorLabelMode,
QwtPlotCanvas *canvas):
QwtPicker(selectionFlags, rubberBand, cursorLabelMode, canvas),
picker_s(str)
{
cout << picker_s << endl; // this shows correct text
}
virtual QString cursorLabel(const QPoint &pos) const //overloading
{
QString label = QString(picker_s);
cout << picker_s << endl; // this shows wrong text
return label;
}
private:
QString picker_s;
};
---------------
and make a new dataPicker in my eventhandling:
-----------------------------
case QEvent::MouseMove:
{
// building the string ds and passing it to dataPicker
ds.sprintf("%02d.%02d.%02d.%03d",t.hour(),t.minute(),
t.second(),millisecs);
dpicker = new dataPicker(ds,
dataPicker:ointSelection | dataPicker:ragSelection,
dataPicker::VLineRubberBand, dataPicker::AlwaysOn,
this->canvas());
------------------------------
The problem is that when the cursor first enters the canvas and displays
the text at the desired position then it hangs on to this first value
and evenif I can see the text changing when I move the cursor it
then rolls back to its initial text. I have put a 'cout << picker_s'
statement in my overloaded cursorLabel function and print
out the value and observe the same problem reassuring me that
it is not the graphics. Then I put the 'cout << picker_s' in
the dataPicker constructor and there it is all ok !
Seems to me that I havn't quite grasped the overloading concept.
Why does the variable 'picker_s' behaves like this in the
overload function ?
Sorry for the lengthy post and hope you people can help me.
Thanks
Kamran