Results 1 to 4 of 4

Thread: qobject_cast on a base class

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default qobject_cast on a base class

    Hey guys, I have the following class:

    Qt Code:
    1. class myWidget : public QObject, public myInterface
    To copy to clipboard, switch view to plain text mode 

    - Now I'm in myInterface::myFunction()
    - I want to cast "this" into myWidget:

    Qt Code:
    1. qobject_cast<myWidget *> (this);
    To copy to clipboard, switch view to plain text mode 

    This doesn't seem to work anyone knows why?

  2. #2
    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: qobject_cast on a base class

    Do you have Q_OBJECT macro in your class?
    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.


  3. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: qobject_cast on a base class

    Let me clarify what I was trying to do:

    I have a myAbstractClass supposed to be used like that :

    Qt Code:
    1. class myObject : public QObject, public myAbstractClass
    To copy to clipboard, switch view to plain text mode 

    But,

    In my library, I'm not defining the myObject class, just the myAstractClass.

    It's "supposed" to be used with a QObject all the time to have the benefit of parency. (which is a bad design at this point).

    So I wanted to qobject_cast that base class in QObject assuming it was inheriting QObject.

    In that "ugly" case I guess dynamic_cast is my only option.

  4. #4
    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: qobject_cast on a base class

    Use Q_DECLARE_INTERFACE in myAbstractClass. Then you'll be able to use object_cast. And remember about Q_OBJECT in the subclass.
    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.


Similar Threads

  1. Replies: 3
    Last Post: 27th December 2008, 19:34
  2. MVC - Abstract Widget Base Class - setupUI
    By SenSej in forum Newbie
    Replies: 0
    Last Post: 13th October 2008, 10:44
  3. Replies: 4
    Last Post: 19th March 2008, 17:47
  4. Connecting to a base class signal?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 22:37
  5. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 21:26

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.