| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 2 | |
Is it possible to emit a signal from within a member function declared const? I need to emit a signal from a fixup function used with a class derived from QValidator. the fixup routine is declared const as in: void fixup(QString&) const; When I attemp to compile code using an emit, I get the following compiler error: "In method 'void DateValidator::fixup(QString &) const': passing 'const DateValidator' as 'this' argument of void DateValidator::goodDate(int,int,int) discards qualifiers" If it is possible, I would appreciate an example. Thanks David
At 7:16 PM -0600 3/4/02, David Reeves wrote: >When I attemp to compile code using an emit, I get the following compiler error: > >"In method 'void DateValidator::fixup(QString &) const': >passing 'const DateValidator' as 'this' argument of >void DateValidator::goodDate(int,int,int) discards qualifiers" > >If it is possible, I would appreciate an example. Are you sure the problem has anything to do with signals? The error message suggests that the problem DateValidator::goodDate needs to be declared const, but it hasn't been. Does adding const fix the error-message? Eric Smith Tarkvara Design Inc.
<<< text/html: EXCLUDED >>>
On Tue, 2002-03-05 at 15:28, David Reeves wrote: > I tried that but it also fails to compile with the following error: > > "prototype for DateValidator::goodDate(int,int,int)' does not match any in > class 'DateValidator' candidate is: void > DateValidator::goodDate(init,init,int) const" > You have to make sure that both method signatures are exactly the same. That is if you declare "goodDate(init,init,int) const" in your header file then you must declare it exatly the same way in your implementation file, ie "DateValidator::goodDate(init,init,int) const". cheers, tomek
Hi David, signals are supposed to be emitted to notify connected objects when an internal state of an instance of a class changes. Obviously, this cannot be done in a const function anyway. You can however cast the this pointer to a non-const that pointer: MyClass *that = (MyClass*)this; emit that->someSignal(); Best regards, Volker ----- Original Message ----- From: "Tomek Piatek" <tomek.piatek@met.co.nz> To: <dnreeves@swbell.net> Cc: "Eric Smith" <eric@tarkvara.org>; "Qt Interest Mailing List" <qt-interest@trolltech.com> Sent: Monday, March 04, 2002 8:46 PM Subject: Re: Signal problem - need help > On Tue, 2002-03-05 at 15:28, David Reeves wrote: > > I tried that but it also fails to compile with the following error: > > > > "prototype for DateValidator::goodDate(int,int,int)' does not match any in > > class 'DateValidator' candidate is: void > > DateValidator::goodDate(init,init,int) const" > > > > You have to make sure that both method signatures are exactly the same. > That is if you declare "goodDate(init,init,int) const" in your header > file then you must declare it exatly the same way in your implementation > file, ie "DateValidator::goodDate(init,init,int) const". > > cheers, > tomek > > -- > List archive and information: http://qt-interest.trolltech.com >
<<< text/html: EXCLUDED >>>
C++ tip: make the casting away of the const attribute
more explicit with: const_cast<YourClass*>(this)
Tim Howland
-----Original Message-----
From: owner-qt-interest@trolltech.com
[mailto:owner-qt-interest@trolltech.com]On Behalf Of David Reeves
Sent: Tuesday, March 05, 2002 10:43 AM
To: John F. Dumas
Cc: qt-interest@trolltech.com
Subject: Re: Signal problem - need help
Thanks John - I just made the change you suggested and it compiles now.
I will now attempt to connect the signal to a slot and cross my fingers.
I agree - that I don't like messing with re-casting to force a soultion.
But - I'm not sure I have an alternative.
David
John F. Dumas wrote:
On Mon, 4 Mar 2002, David Reeves wrote:
Is it possible to emit a signal from within a member function declared
const?I need to emit a signal from a fixup function used with a class
derived from QValidator. the fixup routine is declared const as in:void
fixup(QString&) const;When I attemp to compile code using an emit, I get the
following compiler error:"In method 'void DateValidator::fixup(QString &)
const':passing 'const DateValidator' as 'this' argument ofvoid
DateValidator::goodDate(int,int,int) discards qualifiers"If it is possible,
I would appreciate an example.ThanksDavid
Hey David, Well, it ain't pretty but it'll work - 'emit' just gets
#defined downto nothingness so you could do ...void
YourClass::fixup(QString&) const{ // blah blah blah YourClass *nonConst
= (YourClass *)this; nonConst->goodDate(1, 2, 3);} The alternative (as
the other fellow suggested) would be to makeyour signal itself 'const' as in
...void DateValidator::goodDate(int, int, int) const But it escapes me at
the moment whether or not one's allowed to makesignals/slots 'const' or not.
At any rate, if you can't make the signal'const' the casting hack above will
probably do the
trick.//********************************************************************
***// John F. Dumas Contract Software Developer//
5311 Jasper Grove Unix, Win32, Web - C/C++, Java, CGI Scripting//
Kingwood TX, 77345 jdumas[at]
locutus[dot]kingwoodcable[dot]com// (281)-360-5290
http://locutus.kingwoodcable.com///*****************************************
******************************