Results 1 to 3 of 3

Thread: QTreeWidgetItem does not inherit QObject

  1. #1
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default QTreeWidgetItem does not inherit QObject

    Curiously, QTreeWidgetItem does not inherit QObject. Because of that, a subclass pointer cannot be retrieved with qobject_cast, but the normal C++ dynamic_cast must be used. Is there a reason for this omission? (I'm using a subclass to store a viewable widget for another view.) It's no great problem, but just seems odd.

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

    Default Re: QTreeWidgetItem does not inherit QObject

    QObject is a tool, not a panaceum. Not every class has to be derived from QObject.

    To quote the docs:

    When subclassing QTreeWidgetItem to provide custom items, it is possible to define new types for them so that they can be distinguished from standard items. The constructors for subclasses that require this feature need to call the base class constructor with a new type value equal to or greater than UserType.
    In other words:
    Qt Code:
    1. class MyItem : public QTreeWidgetItem {
    2. public:
    3. enum { Type = QTreeWidgetItem::UserType+1 };
    4. MyItem() : QTreeWidgetItem(Type){}
    5. };
    6.  
    7. QTreeWidgetItem *item = new MyItem;
    8. if(item->type()==MyItem::Type) {
    9. MyItem *myItem = static_cast<MyItem*>(item);
    10. }
    To copy to clipboard, switch view to plain text mode 
    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 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidgetItem does not inherit QObject

    Thanks, I saw the possibility. I got caught trying to use a slot into the item to change an internal pointer, the "displayable", but I found another way to accomplish the task. I was just indulging in my curiosity with this post. I was probably overusing signals and slots, but I find that abstraction so convenient.

Similar Threads

  1. QGraphicsItem doesn't inherit QObject?
    By xyzt in forum Qt Programming
    Replies: 6
    Last Post: 26th September 2011, 14:59
  2. How to use the ui_*.h,inherit it or as a member?
    By 75543255 in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2009, 10:45
  3. Inherit the QAxWidget
    By MrShahi in forum Qt Programming
    Replies: 3
    Last Post: 29th April 2008, 08:06
  4. Inherit from QObject for data-classes?
    By niko in forum Qt Programming
    Replies: 7
    Last Post: 14th July 2007, 15:09
  5. Object Inherit from QTreeWidgetItem
    By xgoan in forum Qt Programming
    Replies: 6
    Last Post: 18th August 2006, 12:20

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.