PDA

View Full Version : QT Model/View questions



SpiceWeasel
16th August 2016, 15:44
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.

jefftee
17th August 2016, 04:27
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.

SpiceWeasel
17th August 2016, 14:16
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 :)

anda_skoa
17th August 2016, 20:31
1) QTreeWidgetItem appears to represent a row in a QTreeWidget; while QStandardItem appears to represent a cell in QStandardItemModel. Is this correct?

Yes



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.



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,
_

SpiceWeasel
22nd August 2016, 17:15
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.

anda_skoa
22nd August 2016, 17:36
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,
_