| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 6 | |
I have an object of class B and want to check whether it inherits from class A or not. Qt docs state B must be successor to QObject, have Q_OBJECT macro etc. ... and how about A ? Does it have to match same requirements as B or not? -- [ signature omitted ]
In a pure C++ point of view, wouldn't a 'dynamic_cast' work better? On Tue, Aug 29, 2006 at 02:00:31PM +0400, Dmitry Teslenko wrote: | I have an object of class B and want to check whether it inherits from | class A or not. Qt docs state B must be successor to QObject, have -- [ signature omitted ]
> In a pure C++ point of view, wouldn't a 'dynamic_cast' work better? .. or 'qobject_cast' in the Qt world ... Malte -- [ signature omitted ]
Dmitry Teslenko wrote: > I have an object of class B and want to check whether it inherits from > class A or not. Qt docs state B must be successor to QObject, have > Q_OBJECT macro etc. ... and how about A ? Does it have to match same > requirements as B or not? Can you explain your problem a bit more? If you already know that your object is of type B, you know at the same time if it inherits from A, since that information is fixed at compile-time. So presumably you deal with a pointer, and you want to know if the pointed to object inherits from some class or not. Usually, you do not need anything of Qt for that at all, you can just use dynamic_cast for that. -- [ signature omitted ]
In QObject you have two pretty methods: isA() and inherits() that can help you if your class derives from this; but like Volker Lukas said, don't understand why you need to determine this on runtime. What's the problem? Cheers, Javier. Volker Lukas escribió: > Dmitry Teslenko wrote: > >> I have an object of class B and want to check whether it inherits from >> class A or not. Qt docs state B must be successor to QObject, have >> Q_OBJECT macro etc. ... and how about A ? Does it have to match same >> requirements as B or not? >> > Can you explain your problem a bit more? If you already know that your > object is of type B, you know at the same time if it inherits from A, since > that information is fixed at compile-time. So presumably you deal with a > pointer, and you want to know if the pointed to object inherits from some > class or not. Usually, you do not need anything of Qt for that at all, you > can just use dynamic_cast for that. > > -- > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body. > List archive and information: http://lists.trolltech.com/qt-interest/ > > > -- [ signature omitted ]
> Can you explain your problem a bit more? If you already know that your > object is of type B, you know at the same time if it inherits from A, since > that information is fixed at compile-time. So presumably you deal with a > pointer, and you want to know if the pointed to object inherits from some > class or not. Usually, you do not need anything of Qt for that at all, you > can just use dynamic_cast for that. I have collection of abstract classes (interfaces) that describe widget's ability to do something (set/read value, act as a list or grid). And I have another collection of classes (widgets) that can be inherited from one or more interfaces and implement them. What interfaces exactly widget implement I to get to know in runtime. So B doesn't inherit from A. It may be or may be not. > In a pure C++ point of view, wouldn't a 'dynamic_cast' work better? > Ehhm ... I don't know ... if I would know I wouldn't ask. Qt's inherits() function allows only to check ability to successful cast. dynamic_cast performs casting itself. Can we compare theese methods as they serve different purposes? Correct me, please, if I wrong. > .. or 'qobject_cast' in the Qt world ... > I have qt-3 and don't find anything about neither it docs, nor qt's source. Are you sure about it? -- [ signature omitted ]
Dmitry Teslenko wrote: >> Can you explain your problem a bit more? If you already know that your >> object is of type B, you know at the same time if it inherits from A, >> since that information is fixed at compile-time. So presumably you deal >> with a pointer, and you want to know if the pointed to object inherits >> from some class or not. Usually, you do not need anything of Qt for that >> at all, you can just use dynamic_cast for that. > > I have collection of abstract classes (interfaces) that describe > widget's ability to do something (set/read value, act as a list or > grid). > And I have another collection of classes (widgets) that can be > inherited from one or more interfaces and implement them. What > interfaces exactly widget implement I to get to know in runtime. > So B doesn't inherit from A. It may be or may be not. You have abstract classes, so you have polymorphic classes. This means dynamic_cast is able to perform a runtime check in conjunction with them. >> In a pure C++ point of view, wouldn't a 'dynamic_cast' work better? >> > > Ehhm ... I don't know ... if I would know I wouldn't ask. Qt's > inherits() function allows only to check ability to successful cast. > dynamic_cast performs casting itself. Can we compare theese methods as > they serve different purposes? Correct me, please, if I wrong. Sure, when using dynamic_cast you get the exact pointer value needlessly if you only want to know if the cast will succeed. Using QObject::inherits(char const*) you have to specify a string stating the class name. So if you only have a string containing the class name, dynamic_cast on its own will not do everything what is needed (but it could still help). >> .. or 'qobject_cast' in the Qt world ... >> > > I have qt-3 and don't find anything about neither it docs, nor qt's > source. Are you sure about it? qobject_cast was introduced in Qt 4. If your questions aren't answered by now, maybe the best thing to do would be posting sample code of what you are trying to achieve. -- [ signature omitted ]
Volker Lukas schrieb: > Dmitry Teslenko wrote: Sorry to jump right into the middle, didn't follow the whole thread. >>> ... >> Ehhm ... I don't know ... if I would know I wouldn't ask. Qt's >> inherits() function allows only to check ability to successful cast. >> dynamic_cast performs casting itself. Can we compare theese methods as >> they serve different purposes? Correct me, please, if I wrong. That's correct. > Sure, when using dynamic_cast you get the exact pointer value needlessly if > you only want to know if the cast will succeed. Hmmm, not sure if this is correct: if it means "You get a valid pointer if the object to be casted is INDEED the expected class (which you have to specify, i.e. hardcode!) OR you get a 0-pointer if the class isn't of the expected type" then the statement above is correct, too. Example: class A; class B : public A; class C; class A *a = new B(); // dynamic type B class B *b; class C *c = new C(); b = dynamic_cast<B>(a); // will succeed; b = dynamic_cast<B>(c); // will "fail" (b is 0) > > Using QObject::inherits(char const*) you have to specify a string stating > the class name. So you have to do with dynamic_cast, see above. > So if you only have a string containing the class name, > dynamic_cast on its own will not do everything what is needed (but it could > still help). I don't understand this what is meant by "doing everything what is needed". What exactly does dynamic_cast not do? :) Cheers, Oliver -- [ signature omitted ]
Till Oliver Knoll wrote: > Volker Lukas schrieb: >> Dmitry Teslenko wrote: > > Sorry to jump right into the middle, didn't follow the whole thread. > >>>> ... >>> Ehhm ... I don't know ... if I would know I wouldn't ask. Qt's >>> inherits() function allows only to check ability to successful cast. >>> dynamic_cast performs casting itself. Can we compare theese methods as >>> they serve different purposes? Correct me, please, if I wrong. > > That's correct. > >> Sure, when using dynamic_cast you get the exact pointer value needlessly >> if you only want to know if the cast will succeed. > > Hmmm, not sure if this is correct: if it means "You get a valid pointer > if the object to be casted is INDEED the expected class (which you have > to specify, i.e. hardcode!) OR you get a 0-pointer if the class isn't of > the expected type" then the statement above is correct, too. It just meant that for querying the type of object only, one can convert the resultant pointer to a bool and throw the pointer away. The only important cases are null-pointer and non-null pointer, the *exact* value is needless. > Example: > > class A; > class B : public A; > class C; > > class A *a = new B(); // dynamic type B > class B *b; > class C *c = new C(); > > b = dynamic_cast<B>(a); // will succeed; Not necessarily, because in the expression dynamic_cast<T>(x) T must be a pointer or reference type. >> Using QObject::inherits(char const*) you have to specify a string stating >> the class name. > > So you have to do with dynamic_cast, see above. > >> So if you only have a string containing the class name, >> dynamic_cast on its own will not do everything what is needed (but it >> could still help). > > I don't understand this what is meant by "doing everything what is > needed". What exactly does dynamic_cast not do? :) It can not deal with a string representing the class. If you want to use dynamic_cast to query if the type of some object fits with the type represented by a string, you have to implement some form of lookup yourself. If you use QObject::inherits(char const*), you do not need to do this because you can hand the string directly to that function. -- [ signature omitted ]
Volker Lukas schrieb: > Till Oliver Knoll wrote: > ... >> Example: >> >> class A; >> class B : public A; >> class C; >> >> class A *a = new B(); // dynamic type B >> class B *b; >> class C *c = new C(); >> >> b = dynamic_cast<B>(a); // will succeed; > Not necessarily, because in the expression dynamic_cast<T>(x) T must be a > pointer or reference type. Ooops, what I've written was off course plain wrong, sorry about that! Off course that should read b = dynamic_cast<B *>(a); > >>> Using QObject::inherits(char const*) you have to specify a string stating >>> the class name. >> So you have to do with dynamic_cast, see above. >> >>> So if you only have a string containing the class name, >>> dynamic_cast on its own will not do everything what is needed (but it >>> could still help). >> I don't understand this what is meant by "doing everything what is >> needed". What exactly does dynamic_cast not do? :) > It can not deal with a string representing the class. If you want to use > dynamic_cast to query if the type of some object fits with the type > represented by a string, you have to implement some form of lookup > yourself. If you use QObject::inherits(char const*), you do not need to do > this because you can hand the string directly to that function. Ahh okay, I get it: in your case the expected type (class) is also given as a string (char * or whatever) from somewhere, so yes, QObject::inherits() does the job of TESTING whether a given object is of the given class. But at some point you actually need to CAST that object in order to use it, and this is where your problem is/was, right (or is that solved by now)? Should have followed the thread from the beginning... Cheers, Oliver -- [ signature omitted ]
Till Oliver Knoll wrote: >>>> Using QObject::inherits(char const*) you have to specify a string >>>> stating the class name. >>> So you have to do with dynamic_cast, see above. >>> >>>> So if you only have a string containing the class name, >>>> dynamic_cast on its own will not do everything what is needed (but it >>>> could still help). >>> I don't understand this what is meant by "doing everything what is >>> needed". What exactly does dynamic_cast not do? :) >> It can not deal with a string representing the class. If you want to use >> dynamic_cast to query if the type of some object fits with the type >> represented by a string, you have to implement some form of lookup >> yourself. If you use QObject::inherits(char const*), you do not need to >> do this because you can hand the string directly to that function. > > Ahh okay, I get it: in your case the expected type (class) is also given > as a string (char * or whatever) from somewhere, so yes, > QObject::inherits() does the job of TESTING whether a given object is of > the given class. > > But at some point you actually need to CAST that object in order to use > it, [...] The original poster only asked about inquiring the type. I quote "I have an object of class B and want to check whether it inherits from class A or not." > [...] and this is where your problem is/was, right (or is that solved by > now)? Should have followed the thread from the beginning... Ah, a slight misunderstanding. I am not the one having the problem. The original poster replied to multiple answers in *one* message, so your confusion is entirely understandable. I apologize if I contributed to the confusion by answering to a question which was directed at someone else myself. -- [ signature omitted ]
> > now)? Should have followed the thread from the beginning...
> Ah, a slight misunderstanding. I am not the one having the problem. The
> original poster replied to multiple answers in *one* message, so your
> confusion is entirely understandable. I apologize if I contributed to the
> confusion by answering to a question which was directed at someone else
> myself.
Sorry, I should expect this amount of replies, starting topic about
inheritance :)
The original question was "one want to check meta information of A and
B with theBObject->inherits("A"). To do so, B should be QObject
successor and have Q_OBJECT macro. And what about A?".
This question still isn't being answered.
Listening to you I changed my mind and decide I don't need qt's rtti
here. dynamic_cast will satisfy me ... sure in case its price isn't
too high.
So, problem seems to be solved, but I still have interest in original question.
--
[ signature omitted ]
On Wednesday 30. August 2006 07:40, Dmitry Teslenko wrote:
> > > now)? Should have followed the thread from the beginning...
> >
> > Ah, a slight misunderstanding. I am not the one having the problem. The
> > original poster replied to multiple answers in *one* message, so your
> > confusion is entirely understandable. I apologize if I contributed to the
> > confusion by answering to a question which was directed at someone else
> > myself.
>
> Sorry, I should expect this amount of replies, starting topic about
> inheritance :)
>
> The original question was "one want to check meta information of A and
> B with theBObject->inherits("A"). To do so, B should be QObject
> successor and have Q_OBJECT macro. And what about A?".
>
> This question still isn't being answered.
Yes, both A and B need to be subclasses of QObject and both have to have the
Q_OBJECT macro in their class declaration.
While qobject_cast is not available in Qt 3 the equivalent there is called
qt_cast. Despite its lack of documentation qt_cast is preferred over
QObject::inherits("SomeClassName") since the latter will silently continue to
compile if you rename the class, while the qt_cast will produce a compilation
error and help you identify the casting code that needs to be adapted as you
rename the class. Also qt_cast is significantly faster than QObject::inherits
as it can determine the result by just comparing the pointers to the
underlying QMetaObject, while QObject::inherits() has to do string
comparisons.
Simon
Attachment:
Attachment:
pgpvFhcqbcoBL.pgp
Description: PGP signature
Message 14 in thread
Simon Hausmann schrieb:
> ...
> While qobject_cast is not available in Qt 3 the equivalent there is called
> qt_cast. Despite its lack of documentation qt_cast is preferred over
> QObject::inherits("SomeClassName") since the latter will silently continue to
> compile if you rename the class, while the qt_cast will produce a compilation
> error and help you identify the casting code that needs to be adapted as you
Just for the record: the following uses a non-documented Qt-API method,
but it works quite nicely for us as to avoid the above mentioned
problems when renaming classes:
obj->inherits (SomeQObjectBaseClass::staticMetaObject()->className());
instead of
obj->inherits ("SomeQObjectBaseClass");
The 'staticMetaObject' method is not documented and hence not part of
the official Qt API. It is auto-generated though for each
QObject/Q_OBJECT based object in the corresponding moc file. It provides
the static QMetaObject (which is part of the public API again) which on
its turn provides the class name of the class in question.
This way you avoid hardcoding the class names into your code - call it a
dirty trick, but it works quite nicely ;) (I don't know about Qt4
though, but anyway, if it had changed your code won't compile and you
will find the mistake at compile time, too).
Cheers, Oliver
--
[ signature omitted ]