Results 1 to 6 of 6

Thread: QT Model/View questions

  1. #1
    Join Date
    Apr 2014
    Posts
    34
    Thanks
    14
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default QT Model/View questions

    Hello,

    I am working on a project that requires data to be represented hierarchically. As I am new to the whole M/V/C paradigm, I am uncertain as to which QT class best suits my projects needs.

    I've been exploring QT's examples, and three questions have come up. The first question is my main concern, although the other two confuse me somewhat.

    1) QTreeWidgetItem appears to represent a row in a QTreeWidget; while QStandardItem appears to represent a cell in QStandardItemModel. Is this correct?

    2) The documentation discourages subclassing QTreeWidget. Is this correct? (I've noticed a number of examples from the web that have subclassed QTreeWidget, so I just wanted to verify what the documentation says -- of course, providing a reason would be appreciated too!)

    3) The documentation states that QTreeWidgets cannot have custom delegates. Is this true? (Again, I've seen many examples where a custome delegate has been used with a QTreeWidget.). However, I noted that the documentation says that a) You should not subclass QTreeWidget, and b) QTreeWidgets cannot have custom delegates.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QT Model/View questions

    For the most flexibility, you should be looking at the QTreeView class, not QTreeWidget for starters. The Q*Widget classes are convenience classes but the Q*View classes and model classes derived from QAbstractItemModel offer the most flexibility.

    For the model, you can use any of the Qt built-in Qt models if they fit your data structure needs well, but for complex data structures, you'll want to derive your own model from QAbstractItemModel and likely also a QSortFilterProxyModel as well if you have sorting and/or filtering needs, which are common requirements.

    To give you one quick example, my first use/attempt at a Qt Model/View implementation used QStandardItemModel. The sorting of a few thousand records became an issue, so I re-implemented using my own model I derived from QAbstractItemModel and QSortFilterProxyModel and the sorting became sub-second for the same number of items.

    So, if you really want to learn Qt model/view programming, either avoid the Widget classes (my opinion), or at least understand why you'd choose them over the View/Model classes that provide more flexibility, etc.

    Hope that helps.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Apr 2014
    Posts
    34
    Thanks
    14
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QT Model/View questions

    Quote Originally Posted by jefftee View Post
    For the most flexibility, you should be looking at the QTreeView class, not QTreeWidget for starters. The Q*Widget classes are convenience classes but the Q*View classes and model classes derived from QAbstractItemModel offer the most flexibility.

    For the model, you can use any of the Qt built-in Qt models if they fit your data structure needs well, but for complex data structures, you'll want to derive your own model from QAbstractItemModel and likely also a QSortFilterProxyModel as well if you have sorting and/or filtering needs, which are common requirements.

    To give you one quick example, my first use/attempt at a Qt Model/View implementation used QStandardItemModel. The sorting of a few thousand records became an issue, so I re-implemented using my own model I derived from QAbstractItemModel and QSortFilterProxyModel and the sorting became sub-second for the same number of items.

    So, if you really want to learn Qt model/view programming, either avoid the Widget classes (my opinion), or at least understand why you'd choose them over the View/Model classes that provide more flexibility, etc.

    Hope that helps.
    Thank you for the reply, but I was actually looking for answers to the three questions I asked

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT Model/View questions

    Quote Originally Posted by SpiceWeasel View Post
    1) QTreeWidgetItem appears to represent a row in a QTreeWidget; while QStandardItem appears to represent a cell in QStandardItemModel. Is this correct?
    Yes

    Quote Originally Posted by SpiceWeasel View Post
    2) The documentation discourages subclassing QTreeWidget. Is this correct? (I've noticed a number of examples from the web that have subclassed QTreeWidget, so I just wanted to verify what the documentation says -- of course, providing a reason would be appreciated too!)
    Where does it say that?
    This would be the thing to do e.g. to implement custom mouse handling.

    Quote Originally Posted by SpiceWeasel View Post
    3) The documentation states that QTreeWidgets cannot have custom delegates. Is this true? (Again, I've seen many examples where a custome delegate has been used with a QTreeWidget.). However, I noted that the documentation says that a) You should not subclass QTreeWidget, and b) QTreeWidgets cannot have custom delegates.
    Where does it say that?

    Cheers,
    _

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

    SpiceWeasel (22nd August 2016)

  6. #5
    Join Date
    Apr 2014
    Posts
    34
    Thanks
    14
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QT Model/View questions

    Hello anda_skoa,

    Thank you for your response and I apologize for the slow reply...


    From Assistant, on the topic of Model/View programming

    Convenience classes

    A number of convenience classes are derived from the standard view classes for the benefit of applications that rely on Qt's item-based item view and table classes. They are not intended to be subclassed.
    From Assistant, on QTreeWidget class

    void QTreeWidget::setItemWidget(QTreeWidgetItem *item, int column, QWidget *widget)

    Sets the given widget to be displayed in the cell specified by the given item and column.

    The given widget's autoFillBackground property must be set to true, otherwise the widget's background will be transparent, showing both the model data and the tree widget item.

    This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or implement a custom editor widget, use QTreeView and subclass QItemDelegate instead.
    After researching the topic more, I think I misread the above. I thought it meant that QTreeWidget could not (or should not) have custom delegates. But clearly that is not the case.

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT Model/View questions

    Quote Originally Posted by SpiceWeasel View Post
    From Assistant, on the topic of Model/View programming
    Ah, I see.
    I think that was mostly intended to mean that subclasses should not be used to changing anything drawing related.

    Cheers,
    _

Similar Threads

  1. Questions regarding Model-View Architecture
    By zgulser in forum Qt Programming
    Replies: 0
    Last Post: 14th March 2012, 17:03
  2. Questions about Model-View Architecture in QT
    By Polnareff in forum Newbie
    Replies: 4
    Last Post: 4th June 2010, 16:01
  3. Questions on Model/View
    By joshuajcarson in forum Qt Programming
    Replies: 7
    Last Post: 26th September 2008, 22:29
  4. questions about datachanged() in model/view programming
    By calmspeaker in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2008, 00:48
  5. Questions regarding setting up a Qt SQL Model/View
    By Methedrine in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2007, 10:26

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.