PDA

View Full Version : dynamic_cast problems with custom widgets



jftaylor21
18th February 2010, 21:29
I am having troubles using dynamic_cast with some of my custom qt widgets in linux, but not windows. Is there any issues that could cause it to fail:

For example, say I have a custom widget called MyCustomWidget. This fails in linux, but works in windows:

MyCustomWidget* widg(dynamic_cast<MyCustomWidget*>(w));

This, however, works in both:
QWdget* widg(dynamic_cast<QWidget*>(w));

nish
19th February 2010, 06:51
what do you mean by fail? does it give compile error on linux or it always returns 0 on runtime?
dynamic cast requires rtti to be enabled. may be u do not have that in your linux compiler.
u can use qobject_cast() which does not requires rtti.

Slewman
19th February 2010, 15:10
We tried using qobject_cast() as well, still no luck. We did enable rtti in the project file, im not sure though if it is built into the linux compiler. We are using gcc 3.4.6

ill describe the problem a little bit better. We are able to call a find widget on the name of our custom widget. This find widget returns a QWidget*, which is valid. we can call objectName() on this pointer and we get the correct name of it. we can even call a type function on it, i forget the exact function call, and it tells us that it is supposed to be OurCustomWidget class. but as soon as we try to cast it to OurCustomWidget*, we get a null back.

any other ideas?

Slewman
19th February 2010, 15:21
to add more info to our problem, one of our custom widgets is a custom push button. we can use dynamic cast, and qobject cast to cast the QWidget* returned by our find function to a QPushButton, but still, when we try to cast to CustomPushButton, it returns a null pointer.

We have no problem dynamic casting to native Qt widgets, its simply our custom widgets that it cant cast.

wysota
19th February 2010, 15:43
Do you have the Q_OBJECT macro in your custom widget's class declaration? Also what does this return?


QWidget *w = ... ; // your widget here
qDebug() << w->metaObject()->className();

If it doesn't return the name of your class but instead the name of its superclass then it means there is no meta-object generated for your class and you will not be able to qobject_cast to it. As for dynamic_cast, it doesn't work across library boundaries and it will also fail when some virtual methods are not implemented (including the virtual destructor).

Slewman
19th February 2010, 15:47
We do have the Q_OBJECT macro in all of our widgets class declarations.

I will try what you are suggesting but i want to understand what i am supposed to do first

You want me to create a QWidget *w = (just a plain QWidget) or (create an instance of our custom widget)?

jftaylor21
19th February 2010, 15:59
Do you have the Q_OBJECT macro in your custom widget's class declaration? Also what does this return?


QWidget *w = ... ; // your widget here
qDebug() << w->metaObject()->className();

If it doesn't return the name of your class but instead the name of its superclass then it means there is no meta-object generated for your class and you will not be able to qobject_cast to it. As for dynamic_cast, it doesn't work across library boundaries and it will also fail when some virtual methods are not implemented (including the virtual destructor).

Yeah, we tried the metaObject()->className() function and it returns the custom widget classname, so that appears to be working. Will the qobject_cast fail like dynamic_cast if some virtual methods are not implemented as well?