QStyledItemDelegate with QListView grows, but doesn't shrink
-
I have a class derived from QStyledItemDelegate that I am using to display HTML in a QListView. In order to bind my item delegate to the QListView, I use the following:
ui->myListView->setItemDelegate(new HTMLDelegate(this));
The QListView has defaults set, with the exception of the resizeMode property, which I have changed from Fixed to Adjust.
From the sizeHint method, the option.rect property only reflects growth, not shrinking, which causes my resulting undesired behavior:
QSize HTMLDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItemV4 options = option; initStyleOption(&options, index); QTextDocument doc; doc.setHtml(index.data().toString()); doc.setTextWidth(option.rect.width()); return QSize(doc.idealWidth(), doc.size().height()); }
I can manually call a reset() on the QListView, and that will properly resize my items, but it also deselects any selected items. Sure, I could re-select the item, but I don't really see it as an elegant solution.
Any ideas around my problem?