Sorting and displaying a custom QVariant type
I have a custom type I'd like to use with QVariant but I don开发者_开发知识库't know how to get the QVariant to display in a table or have it sort in a QSortFilterProxyModel.
I register the type with Q_DECLARE_METATYPE and wrote streaming operators registered via qRegisterMetaTypeStreamOperators but for whatever reason when I use the type with a table model, it doesn't display anything and it doesn't sort.
I should specify that this custom type can not be modified. It has a copy and default constructor, but I can not go in and modify the source code to get it to work with QVariant. Is there a way of non-intrusively getting the behaviour I'd like?
Display:
It sounds like your model isn't returning sensible content for the DisplayRole
. The QAbstractItemDelegate (often a QStyledItemDelegate) that is used to display all content from the model needs to understand how to render the content of returned by data()
for the Qt::DisplayRole
.
You have two main options:
- Modify your model so that it returns a sensible Qt::DisplayRole, OR
- Subclass one of the existing delegates and modify it so that it can display your custom variant type correctly.
If you want to edit items of that type, you'll need to call registerEditor so you can associate your custom type to an editor. See the QItemEditorFactory documentation.
Sorting:
You can't rely on the comparison operator for QVariant as it doesn't work with custom types, so you'll need to implement QSortFilterProxyModel::lessThan
to have custom sorting.
精彩评论