QSize ChatViewItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItemV4 options; initStyleOption(&options, index); // Create QTextDocument to render HTML QTextDocument doc; doc.setHtml(options.text); // Set document width the same as QListView's width doc.setTextWidth(options.rect.width()); return QSize(doc.idealWidth(), doc.size().height());}