PDA

View Full Version : Subclassing QStandardItem



aLiNuSh
2nd April 2007, 21:04
Hi,

I have a QTreeView that uses a subclassed QStandardItemModel model.
The treeview should keep an user list for a chat program.

What I want to do is to subclass QStandardItem in order to make user insertion/deletion/hiding easier but I'm not sure how should I reimplement the QStandardItem * QStandardItem::clone () const [virtual] (http://doc.trolltech.com/4.2/qstandarditem.html#clone) method. I know that I need to do that in order to call void QStandardItemModel::setItemPrototype ( const QStandardItem * item ) (http://doc.trolltech.com/4.2/qstandarditemmodel.html#setItemPrototype) in the model.

Here's what I was thinking the derived class should look like:



class MyQStandardItem : public QStandardItem {
private:
bool online;
/* some other flags */
public:
/* default constructors and overloaded ones */
bool isOnline () const { return online; }
/* other functions */
};


Considering I'm not using pointers as data members should I worry about clone () ?

I'm really confused on this one.
Thanks!

wysota
3rd April 2007, 00:27
I don't think you should subclass QStandardItem. If you want to add the "isOnline" thing, it will be simpler to add a "OnlineRole" role to the model and just use setData(..., OnlineRole)

aLiNuSh
5th April 2007, 14:55
I don't think you should subclass QStandardItem. If you want to add the "isOnline" thing, it will be simpler to add a "OnlineRole" role to the model and just use setData(..., OnlineRole)

Yeah I know I could do that too... and maybe it would be better to do that in this situation, but I'm still curious on how to implement the clone() method.

Thanks for your reply.

wysota
5th April 2007, 19:49
If you need it (do you?) you can treat it a little bit like a copy constructor. It can't be a copy constructor as QStandardItem subclass might want to inherit QObject, so you need a factory method that acts in a simmilar way. Just create an item using operator new and give it the properties of the other item.

aLiNuSh
5th April 2007, 20:00
If you need it (do you?) you can treat it a little bit like a copy constructor. It can't be a copy constructor as QStandardItem subclass might want to inherit QObject, so you need a factory method that acts in a simmilar way. Just create an item using operator new and give it the properties of the other item.

I don't know for sure :(

The thing is I'm trying to set up a tree view with Groups and Users and I can't find an easy way to set up the model in order to make it easier to move user to groups, delete groups, delete users, drag and drop users, sort the tree and other tasks. So I thought one solution would be subclassing QStandardItem and make a User class and a Group class. I'm gonna think on other solutions as I don't see this one working either.

Thanks for your answer.