473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

function overloading question


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

Jul 23 '05 #1
2 2989
Kamran wrote:
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
Jul 23 '05 #2
Victor Bazarov wrote:
-----------------------------

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 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.
> 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


And when in those functions, also check the 'this' pointer, if it
contains the same value.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
7
by: Steve Pope | last post by:
Is there a reason I am unable to overload foo() in the following code, which does not compile ("no matching function")? int foo(int a) { return(a); } struct goo { int a;
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
14
by: Pramod | last post by:
I have one question. Can I catch exception in function overloading.In my programm i want to create two function with same signature and same return type. I know its not possible...can i use...
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.