PDA

View Full Version : Rich text in table view



tulsi
28th April 2009, 08:53
How to display rich text in table view?

spirit
28th April 2009, 09:44
you can try this, a little bit tricky, but works :)


MyItemDelegate::MyItemDelegate(QObject *object)
: QItemDelegate(object)
{
m_label = new QLabel();
m_label->setTextFormat(Qt::RichText);
}

MyItemDelegate::~MultistringsItemDelegate()
{
delete m_label;
m_label = 0;
}

QWidget *MyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
return new QLineEdit(parent);
}

void MyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor);
if (!lineEdit)
return;
model->setData(index, lineEdit->text());
model->setData(index, QString("<span style=\" color:#0ff000;\">%1</span>").arg(lineEdit->text()), Qt::DisplayRole);
}

void MyItemDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
{
m_label->setText(text);
QPalette pal(m_label->palette());
pal.setColor(QPalette::Active, QPalette::Window, option.state & QStyle::State_Selected ? pal.color(QPalette::Highlight) : pal.color(QPalette::Base));
m_label->setPalette(pal);
m_label->resize(option.rect.size());
QPixmap p = QPixmap::grabWidget(m_label);
painter->drawPixmap(option.rect, p);
}

wysota
29th April 2009, 01:10
Or you can take a look at the "similar threads" table on the bottom of the thread page.

tulsi
29th April 2009, 11:59
thank u so much guys!

spirit
29th April 2009, 14:28
you can also make like this


...
void MyItemDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
{
Q_UNUSED(rect);
QTextDocument doc;
doc.setHtml(text);
doc.drawContents(painter, option.rect);
}
...