function overloading question

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::pointSelection | dataPicker::DragSelection,
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
 
V

Victor Bazarov

Kamran said:
I have very little experience of C++, nevertheless I have
been asked to write a gui using QT/QWT.

I know what you mean. People just don't understand that they should
look for a better man for the job...
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++.

After reading your post the first time, I don't think so. Let's see
what happens when I do it again while answering some of your questions.
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.

Actually, the term is "override" if the aforementioned function is
declared 'virtual'.
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:

You _derive_ from the QwtPicker.
------------------
class dataPicker: public QwtPicker // subclassing Picker
{

// here I send one extra parameter "QString &str" to my picker

It would be better if the 'str' argument were passed by a const ref,
but it wouldn't affect the outcome, just make the work a bit more
convenient.
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

I know one should trust standard output, but could you put a breakpoint
here and look at the {*this} object in the debugger? Does the string
(picker_s) contain the right value?
return label;
}
private:

QString picker_s;

Are you sure this is not a reference? It shouldn't be, I'm just checking.
};



---------------

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::pointSelection | dataPicker::DragSelection,
dataPicker::VLineRubberBand, dataPicker::AlwaysOn,
this->canvas());

If your function 'cursorLabel' gets called (and I presume it does), you
have done it right so far.
------------------------------

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 am not sure what "rolls back" means here. Any movement on the part of
the 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 !

Well, it's OK during the construction, but somebody must be changing it.
What you need to do is to use a decent debugger and (a) put a breakpoint
in the constructor, let your program stop there, then (b) put another
breakpoint, this time of the type "when data changes" and make your
debugger stop when the contents of the string
Seems to me that I havn't quite grasped the overloading concept.

If your function gets called, you've grasped the _overriding_ concept
fine.
Why does the variable 'picker_s' behaves like this in the
overload function ?

In the overridden function it should of course behave correctly. But it
seems to me that something makes a change to that variable somewhere in
between. Make sure you don't pass it anywhere else. For experimenting's
sake you could try declaring a global variable and changing its value in
the constructor and returning it in your overridden member function.
See if it changes anything.

Well, do this: change the name of the data member to m_picker_s, and do it
only in the class definition, and not in any function. Then compile, and
you should get an error message where 'picker_s' is used. Make sure you
fix the name to 'm_picker_s' but only in the constructor (initialiser list
and debug printout) and in the function 'cursolLabel'. Then compile your
program again. See if there is another place in your code that accesses
that data member.

Good luck and post back soon!

V
 
K

Karl Heinz Buchegger

Victor said:
If your function 'cursorLabel' gets called (and I presume it does), you
have done it right so far.


I am not sure what "rolls back" means here. Any movement on the part of
the text?

I have no experience with Qt but there is one thing that puzzles me.
Everytime MouseMove is called a new dataPicker object gets created.
I can't see whats happening with that and I even don't know if
this is the right thing to do with Qt (I would have expected
to always use the same dataPicker object, just give it a new
value). But the symptoms the OP sees would fit to:
The dataPicker object which gets the new text to show is not the
same object then the one used for displaying that text.
Well, it's OK during the construction, but somebody must be changing it.
What you need to do is to use a decent debugger and (a) put a breakpoint
in the constructor, let your program stop there, then (b) put another
breakpoint, this time of the type "when data changes" and make your
debugger stop when the contents of the string

And when in those functions, also check the 'this' pointer, if it
contains the same value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top