Results 1 to 6 of 6

Thread: How to define class RTTI info macro?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to define class RTTI info macro?

    I am trying to define some macro for RTTI identification to get some class info, such as class name and base class name for object reconstruction. The class is derived from QGraphicsItem.

    For instance:

    Qt Code:
    1. class MyGraphicsItem : public QGraphicsItem {
    2. ...
    3. }
    To copy to clipboard, switch view to plain text mode 

    I need some macro so I can do
    obj->getClassName() // return "MyGraphicsItem"
    or
    obj->getBase()->getClassName() // return "QGraphicsItem"

    How do I define those macros and how to use it?

    Many thanks

  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: How to define class RTTI info macro?

    Inherit from both QObject and QGraphicsItem and make sure you put the Q_OBJECT macro in the class header.
    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
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to define class RTTI info macro?

    There will be costly to inherit from QObject as there are thousand of QGraphicsItem.

    I just need a macro so I can get class info, such as

    Qt Code:
    1. class MyGraphicsItem : public QGraphicsItem
    2. {
    3. TYPE_INFO( MyGraphicsItem )
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    But I don't know how to write such macro...

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to define class RTTI info macro?

    I think that much more simple would by to use QGraphicsItem::type()
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to define class RTTI info macro?

    I have many subclass from QGraphicsItem, it is very hard to track the type(). I have to make sure they don't return the same value. It would be easier to have a macro ....

  6. #6
    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: How to define class RTTI info macro?

    But what would this macro return? And why a macro and not a method? You can create a virtual method in your common base class to return the name of the class and reimplement the method in each subclass. Of course that's more or less equivalent of using QGraphicsItem::Type...
    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. Q_OBJECT macro - what exactly does it do?
    By magland in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2007, 10:30
  2. Macro
    By comlink21 in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2007, 11:28
  3. rtti option
    By sar_van81 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 5th January 2007, 15:10
  4. rtti() of derived class
    By quickNitin in forum Newbie
    Replies: 4
    Last Post: 8th October 2006, 14:20
  5. Speed penalty when using RTTI?
    By pir in forum General Programming
    Replies: 5
    Last Post: 19th May 2006, 00:36

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.