This is what I've done right now.
Using the same code for painting and widget, I'm able to use a custom widget as usual; moreover, for listview I'm able to render the same widget correctly (because calling render method of widget, the widget is able to read .qss stylesheet)
Here is the example:
this is a single widget
0_1562332451778_widget.png
and this is the rendered widget inside the listview
0_1562332527764_listview-render.png
in listview, as you can see, I'm able to render widget correctly and, in case of focus, I can change focus style using a custom property
this is the code:
void AlertItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
QVariant data = index.data();
if (data.canConvert<Error>()) {
auto&& styleOption = QStyleOptionViewItem(option);
ErrorItemWidget widget;
if (styleOption.state & QStyle::State_HasFocus)
widget.setProperty("Test", true); // <-- custom focus property
else
widget.setProperty("Test", QVariant::Invalid); // <-- remove property for normal state
widget.resize(option.rect.width(), option.rect.height());
widget.setError(qvariant_cast<Error>(data));
painter->save();
painter->translate(option.rect.topLeft());
widget.render(painter);
painter->restore();
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}
The drawback of this solution is that I need to create a widget for each model item (however, I've render 2000 elements and I don't see any performance hit)
The other drawback is that the height of each item is hardly code inside size-hint method, that is:
QSize AlertItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
int width = option.rect.width();
int height = 48; // <--- if I can get this from stylesheet, I'll be happy :)
return QSize(width, height);
}
The good thing is that I can change widget stylesheet using .qss file; inside C++ code there isn't any style detail.
Any suggestion to get height value from stylesheet?
Thanks all!
Nicola