#include <QtGui>
class MyDelegate : public QStyledItemDelegate
{
public:
: QStyledItemDelegate(parent) {};
{
// you could also do a quick reg exp to check for
// HTML tags.
if (true == index.data(Qt::UserRole + 1234).toBool())
{
td.setPageSize(option.rect.size());
td.setTextWidth(option.rect.width());
td.setDefaultFont(option.font);
td.setDocumentMargin(0);
td.setHtml(index.data().toString());
painter->translate(option.rect.topLeft());
td.
drawContents(painter,
QRect(QPoint(0,
0), option.
rect.
size()));
painter->translate(-option.rect.topLeft());
}
else
QStyledItemDelegate::paint(painter, option, index);
}
};
int main(int argc, char *argv[])
{
tw.setColumnCount(1);
tw.setItemDelegate(new MyDelegate(&tw));
QList<QTreeWidgetItem *> items;
it->setText(0, "plain Text");
items << it;
it->setText(0, "m s<sup>-1</sup>");
it->setData(0, Qt::UserRole + 1234, true);
// you can choose any value for 1234
// this just tells if the item should
// rendered as HTML
items << it;
it->setText(0, "m s<sup>-3</sup>");
it->setData(0, Qt::UserRole + 1234, true);
items << it;
it->setText(0, "m s<sup>-1</sup>");
it->setData(0, Qt::UserRole + 1234, false);
items << it;
tw.insertTopLevelItems(0, items);
tw.show();
return a.exec();
}
#include <QtGui>
class MyDelegate : public QStyledItemDelegate
{
public:
MyDelegate(QObject* parent = 0)
: QStyledItemDelegate(parent) {};
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// you could also do a quick reg exp to check for
// HTML tags.
if (true == index.data(Qt::UserRole + 1234).toBool())
{
QTextDocument td;
td.setPageSize(option.rect.size());
td.setTextWidth(option.rect.width());
td.setDefaultFont(option.font);
td.setDocumentMargin(0);
td.setHtml(index.data().toString());
painter->translate(option.rect.topLeft());
td.drawContents(painter, QRect(QPoint(0,0), option.rect.size()));
painter->translate(-option.rect.topLeft());
}
else
QStyledItemDelegate::paint(painter, option, index);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeWidget tw;
tw.setColumnCount(1);
tw.setItemDelegate(new MyDelegate(&tw));
QList<QTreeWidgetItem *> items;
QTreeWidgetItem *it;
it = new QTreeWidgetItem;
it->setText(0, "plain Text");
items << it;
it = new QTreeWidgetItem;
it->setText(0, "m s<sup>-1</sup>");
it->setData(0, Qt::UserRole + 1234, true);
// you can choose any value for 1234
// this just tells if the item should
// rendered as HTML
items << it;
it = new QTreeWidgetItem;
it->setText(0, "m s<sup>-3</sup>");
it->setData(0, Qt::UserRole + 1234, true);
items << it;
it = new QTreeWidgetItem;
it->setText(0, "m s<sup>-1</sup>");
it->setData(0, Qt::UserRole + 1234, false);
items << it;
tw.insertTopLevelItems(0, items);
tw.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks