Sorting and Displaying a Custom QVariant Type
I have a custom type that I would like to use with a QVariant, but I do not know how to get the QVariant to display in a table or sort it in a QSortFilterProxyModel.
I register a type with Q_DECLARE_METATYPE and wrote stream statements registered with qRegisterMetaTypeStreamOperators, but for whatever reason, when I use a type with a tabular model, it does not display or sort anything.
I have to point out that this custom type cannot be changed. This has a copy and a default constructor, but I can't go in and change the source to make it work with QVariant. Is there a way to non-intrusively get the behavior I would like?
a source to share
Screen
It looks like your model is not returning reasonable content for DisplayRole
. The QAbstractItemDelegate (often QStyledItemDelegate ), which is used to display all content from the model, needs to understand how to display the content returned data()
for Qt::DisplayRole
.
You have two main options:
- Modify your model to return a sane Qt :: DisplayRole , OR
- Subclass one of the existing delegates and modify it so that it can correctly display your custom variant.
If you want to edit elements of this type, you need to call registerEditor so that you can associate your own type with the editor. See the QItemEditorFactory documentation .
Sorting
You cannot rely on the comparison operator for QVariant as it does not work with custom types, so you will need to execute QSortFilterProxyModel::lessThan
for custom sorting.
a source to share