Results 1 to 5 of 5

Thread: Subclassing QStandardItem

  1. #1
    Join Date
    Mar 2007
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Subclassing QStandardItem

    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] method. I know that I need to do that in order to call void QStandardItemModel::setItemPrototype ( const QStandardItem * item ) in the model.

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

    Qt Code:
    1. class MyQStandardItem : public QStandardItem {
    2. private:
    3. bool online;
    4. /* some other flags */
    5. public:
    6. /* default constructors and overloaded ones */
    7. bool isOnline () const { return online; }
    8. /* other functions */
    9. };
    To copy to clipboard, switch view to plain text mode 

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

    I'm really confused on this one.
    Thanks!

  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: Subclassing QStandardItem

    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)

  3. #3
    Join Date
    Mar 2007
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QStandardItem

    Quote Originally Posted by wysota View Post
    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.

  4. #4
    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: Subclassing QStandardItem

    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.

  5. The following user says thank you to wysota for this useful post:

    aLiNuSh (5th April 2007)

  6. #5
    Join Date
    Mar 2007
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QStandardItem

    Quote Originally Posted by wysota View Post
    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.

Similar Threads

  1. QStandardItem : subpart of the text as bold
    By titoo in forum Qt Programming
    Replies: 5
    Last Post: 1st November 2012, 09:34
  2. Problem with QTreeWidget after subclassing
    By steve918 in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2006, 18:51
  3. Problem in SubClassing QTableItem.
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2006, 10:21
  4. Subclassing QScrollView
    By sumsin in forum Qt Programming
    Replies: 13
    Last Post: 16th March 2006, 14:20
  5. Subclassing
    By joseph in forum Newbie
    Replies: 1
    Last Post: 25th February 2006, 14:06

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.