PDA

View Full Version : Inheriting QListViewItem



ct
17th March 2006, 06:33
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.

Michiel
17th March 2006, 06:52
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.