Results 1 to 4 of 4

Thread: Subclassing QStandardItemModel to QTrevview

  1. #1
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Subclassing QStandardItemModel to QTrevview

    I've created an application that subclasses the QAbstractItemModel to load item from at database and display them in a QTreeview. I store data in a QMap and I've implemented new functions for;

    QModelIndex index(int row, int column,const QModelIndex &parent) const;
    QModelIndex parent(const QModelIndex &child) const;

    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;

    Now I want to change the model from QAbstractItemModel to QStandardItemModel to utilize the sort and findItems functions. I'm changing my implementation class from QAbstractItemModel to QStandardItemModel but now the Qtreeview is empty when I run the code. What going on here?
    Does anyone have an example of a QStandardItemModel bieng subclasses (editable) to be used in a QTreeview?

    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 QStandardItemModel to QTrevview

    QStandardItemModel has those method you mentioned already implemented. sort() and findItems() from QStandardItemModel will not work with your reimplementations. It will be much easier if you just reimplement appropriate methods for your (abstract item model based) model. In general you can't sort a map, so you either need to implement artificial mapping or switch from using QMap to QList. If you do the latter, sorting items is equivalent to sorting the data container. There is also one alternative to doing all that - wrap your model in a QSortFilterProxyModel and set that as the model for your view.
    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
    May 2009
    Location
    Copenhagen
    Posts
    50
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subclassing QStandardItemModel to QTrevview

    Thanks for the advise wysota!
    I think I will change from QMap to QList.
    Actually I was considering the QStandardItemModel for more than one reason. I can't seem to iterate through the tree to retrieve all the QModelIndexes from my model. I want the user to be able to rearrange the items in the tree - for example to move down items and to change the parent of items. My problem is that I must reset the model to refresh the changes - and the reset makes the tree collapse all notes. My workaround was to iterate through the tree items to see which notes are expanded and remember that after the reset. But with the QAbstractItemModel I've found no way of iterating all the QModelindex in the model which is why I was looking to the QStandardItemModel instead.

    That was a long story but do you follow?

  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 QStandardItemModel to QTrevview

    Yes, I can follow you quite fine. You'd need to implement a few more methods to make drag and drop available. It's probably more work than switching to QStandardItemModel.
    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.


Similar Threads

  1. Replies: 2
    Last Post: 24th August 2008, 14:42

Tags for this Thread

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.