class Delegate : public QStyledItemDelegate
{
Q_OBJECT
protected:
};
{
QStyleOptionViewItemV4 options = option;
initStyleOption(&options, index);
painter->save();
doc.setHtml(options.text);
/* Call this to get the focus rect and selection background. */
options.text = "";
options.
widget->style
()->drawControl
(QStyle::CE_ItemViewItem,
&options, painter
);
/* Draw using our rich text document. */
painter->translate(options.rect.left(), options.rect.top());
QRect clip
(0,
0, options.
rect.
width(), options.
rect.
height());
doc.drawContents(painter, clip);
painter->restore();
}
{
QStyleOptionViewItemV4 options = option;
initStyleOption(&options, index);
doc.setHtml(options.text);
doc.setTextWidth(options.rect.width());
return QSize(doc.
idealWidth(), doc.
size().
height());
}
/* Demonstration. */
MainWindow
::MainWindow(QWidget *parent
){
model->setData(model->index(0, 0), "<b>Test</b><br />i<img src=\"img.png\" />ng123", Qt::DisplayRole);
model->setData(model->index(1, 0), "<b>Test</b>ing234", Qt::DisplayRole);
Delegate * delegate = new Delegate;
lv->setModel(model);
lv->setItemDelegate(delegate);
setCentralWidget(lv);
}
class Delegate : public QStyledItemDelegate
{
Q_OBJECT
protected:
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
};
void Delegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
{
QStyleOptionViewItemV4 options = option;
initStyleOption(&options, index);
painter->save();
QTextDocument doc;
doc.setHtml(options.text);
/* Call this to get the focus rect and selection background. */
options.text = "";
options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
/* Draw using our rich text document. */
painter->translate(options.rect.left(), options.rect.top());
QRect clip(0, 0, options.rect.width(), options.rect.height());
doc.drawContents(painter, clip);
painter->restore();
}
QSize Delegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QStyleOptionViewItemV4 options = option;
initStyleOption(&options, index);
QTextDocument doc;
doc.setHtml(options.text);
doc.setTextWidth(options.rect.width());
return QSize(doc.idealWidth(), doc.size().height());
}
/* Demonstration. */
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QStandardItemModel * model = new QStandardItemModel(2, 1);
model->setData(model->index(0, 0), "<b>Test</b><br />i<img src=\"img.png\" />ng123", Qt::DisplayRole);
model->setData(model->index(1, 0), "<b>Test</b>ing234", Qt::DisplayRole);
QListView * lv = new QListView;
Delegate * delegate = new Delegate;
lv->setModel(model);
lv->setItemDelegate(delegate);
setCentralWidget(lv);
}
To copy to clipboard, switch view to plain text mode
Bookmarks