Results 1 to 4 of 4

Thread: QAbstractItemModel question

  1. #1
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default QAbstractItemModel question

    I am using the QAbstractItemModel and below is the implementation of the data function. I would like to keep my current data structure separate and intact so that is why I chose the abstract rather than the standard. The problem I am having is that I am not sure if I am re-inventing the wheel. Does this code seem unusual and unnecessary? Would I have an easier time using the QStandardItemModel along with a subclassed QStandardItem?

    (BUS is a namespace where I defined the icon type ids. I also created a member in my BUSDataMover object that stores the icon type which can be obtained with getIconType())

    Qt Code:
    1. QVariant TreeModel::data(const QModelIndex &index, int role) const {
    2. if(!index.isValid())
    3. return QVariant();
    4. BUSDataMover* item;
    5. switch(role) {
    6. case Qt::DisplayRole:
    7. item = static_cast<BUSDataMover*>(index.internalPointer());
    8. return item->data(index.column());
    9. break;
    10. case Qt::DecorationRole:
    11. item = static_cast<BUSDataMover*>(index.internalPointer());
    12. if(index.column() != 0)
    13. return QVariant();
    14. switch(item->getIconType()) {
    15. case BUS::NetworkIcon:
    16. return QIcon(":/images/network.png");
    17. break;
    18. case BUS::ClientIcon:
    19. return QIcon(":images/client.png");
    20. break;
    21. case BUS::ServerIcon:
    22. return QIcon(":images/server.png");
    23. break;
    24. case BUS::NICIcon:
    25. return QIcon(":images/nic.png");
    26. default:
    27. return QIcon(":/images/unknown.png");
    28. }
    29. break;
    30. default:
    31. return QVariant();
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

  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: QAbstractItemModel question

    It's fine, although using QStandardItemModel with a subclassed QStandardItem would be fine as well. In both cases you have to reimplement the data() method and a few others as well - fewer with the QStandardItem method, but if you already implemented the model then this doesn't really matter...

  3. #3
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QAbstractItemModel question

    Thanks Wysota.

    One more question. You mention that I could use QStandardItemModel and then subclass QStandardItem. If I were to do this, would I be able to provide a pointer to my custom data structure and then use QStandardItem::UserType for each of the columns in my custom data structure to set/get data?

  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: QAbstractItemModel question

    Not exactly. UserType is a constant which marks the beginning of the id space for standard item subclasses. You should reimplement QStandardItem::type() and return a value greater than UserType that will uniquely identify your class, so that you can later identify the type of item (for instance when comparing items using operator<). This has nothing to do with structures or data. To return contents of your structure as different columns of the item you need to reimplement QStandardItem::data() and query your structure for particular piece of data for a particular column of the model. The same goes for QStandardItem::setData() if you want your item to be editable.

Similar Threads

  1. QAbstractItemModel newbie question
    By okellogg in forum Qt Programming
    Replies: 14
    Last Post: 18th February 2008, 12:30
  2. Question on QAbstractItemModel vs. QTableView
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2007, 07:29
  3. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 17:49
  4. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 15:34

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.