Implement sizeHint in TemplatePicker's HTML item delegate. (#279)

Height is not always dominated by icon size on all platforms, potentially
cutting off text.
This commit is contained in:
Jaye Evins
2026-01-13 00:59:47 -05:00
committed by GitHub
parent 1c902230fe
commit 7cadf5308d
+13 -3
View File
@@ -43,14 +43,12 @@ namespace
// //
// Based on solutions at // Based on solutions at
// https://stackoverflow.com/questions/1956542/how-to-make-item-view-render-rich-html-text-in-qt/1956781#1956781 // https://stackoverflow.com/questions/1956542/how-to-make-item-view-render-rich-html-text-in-qt/1956781#1956781
// Note: assumes that the text rectangle does not need to be resized, so does not reimplement sizeHint().
// This delegate does not work correctly in IconMode, and may not work correctly in other applications,
// for instance, when the height is not dominated by the icon.
// //
class HtmlDelegate : public QStyledItemDelegate class HtmlDelegate : public QStyledItemDelegate
{ {
protected: protected:
void paint ( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const override; void paint ( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const override;
QSize sizeHint ( const QStyleOptionViewItem& option, const QModelIndex& index ) const override;
}; };
@@ -90,6 +88,18 @@ namespace
painter->restore(); painter->restore();
} }
QSize HtmlDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
auto opt = option;
initStyleOption( &opt, index );
QTextDocument doc;
doc.setHtml( opt.text );
doc.setTextWidth( opt.rect.width() );
return QSize( doc.idealWidth(), doc.size().height() );
}
} }