Results 1 to 2 of 2

Thread: Inheriting QListViewItem

  1. #1
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Inheriting QListViewItem

    I have a class which inherits QListViewItem.
    class myItem : public QListViewItem
    {
    public:
    ......
    void somefunction();
    }
    now I create a list view "mainList" from Qt Designer and add my items in this way
    myItem *item=new myItem(mainList,"label");
    however when I try to fetch the item by iterating the listview why wouldn't the following code work
    myItem *item=mainList->firstChild();
    //this creates an error : InValid conversion from QListViewItem to myItem

    but if I do
    QListViewItem *item=mainList->firstChild();
    then it is ok but I can't call someFunction() which is inside myItem
    I know the working solution is :
    myItem *item=(myItem *) mainList->firstChild();
    but I am wondering if myItem inherits QListViewItem then it "is a" QListViewItem ..so why do we still need to typecast. Shouldn't the "is a " relationship of inheritence hold true here ?

    Thank you to anyone who would like to solve this small query.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Inheriting QListViewItem

    It is true that myItem is a QListViewItem. But not necessarily the other way around. The QListView holds QListViewItems, and knows nothing about myItems. So you have to downcast. It might be a good idea to subclass QListView also, to support myItems, so you won't have to cast from the outside.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Similar Threads

  1. Replies: 7
    Last Post: 12th January 2011, 22:01
  2. QListWidget inheriting custom class
    By phannent in forum Qt Tools
    Replies: 1
    Last Post: 4th August 2008, 14:39
  3. qt2 QListViewItem
    By hungr in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 10:58
  4. Segmentation fault in QListViewItem
    By ederbs in forum Qt Programming
    Replies: 2
    Last Post: 26th November 2006, 01:19
  5. QT3: mouseover effect in QListViewItem
    By karye in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2006, 12:44

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.