Results 1 to 7 of 7

Thread: dynamic_cast problems with custom widgets

  1. #1
    Join Date
    Oct 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default dynamic_cast problems with custom widgets

    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));

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: dynamic_cast problems with custom widgets

    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.

  3. #3
    Join Date
    Sep 2009
    Posts
    64

    Default Re: dynamic_cast problems with custom widgets

    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?

  4. #4
    Join Date
    Sep 2009
    Posts
    64

    Default Re: dynamic_cast problems with custom widgets

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: dynamic_cast problems with custom widgets

    Do you have the Q_OBJECT macro in your custom widget's class declaration? Also what does this return?

    Qt Code:
    1. QWidget *w = ... ; // your widget here
    2. qDebug() << w->metaObject()->className();
    To copy to clipboard, switch view to plain text mode 

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2009
    Posts
    64

    Default Re: dynamic_cast problems with custom widgets

    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)?

  7. #7
    Join Date
    Oct 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dynamic_cast problems with custom widgets

    Quote Originally Posted by wysota View Post
    Do you have the Q_OBJECT macro in your custom widget's class declaration? Also what does this return?

    Qt Code:
    1. QWidget *w = ... ; // your widget here
    2. qDebug() << w->metaObject()->className();
    To copy to clipboard, switch view to plain text mode 

    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?

Similar Threads

  1. Custom Widgets
    By walden02 in forum Qt Programming
    Replies: 3
    Last Post: 23rd December 2009, 11:43
  2. Replies: 0
    Last Post: 15th May 2009, 15:38
  3. Help with custom widgets!!
    By superteny in forum Qt Programming
    Replies: 4
    Last Post: 29th April 2009, 12:43
  4. dynamic_cast not working
    By kloffy in forum Newbie
    Replies: 2
    Last Post: 11th October 2007, 23:07
  5. dynamic_cast and templates
    By KShots in forum General Programming
    Replies: 7
    Last Post: 7th August 2007, 20:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.