[Qt-interest] Using QTextEdit in QItemDelegate

Josh jnfo-c at grauman.com
Thu Sep 30 19:08:27 CEST 2010


For what it's worth, you may try using a QTextDocument instead like this:

void HtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem & 
option, const QModelIndex & index) const
{
   QVariant value = index.data(Qt::DisplayRole);
   if (value.isValid() && !value.isNull())
   {
     QTextDocument document;
     document.setDocumentMargin(2);
     document.setHtml(value.toString());
     painter->translate(option.rect.topLeft());
     document.drawContents(painter);
     painter->translate(-option.rect.topLeft());
   }
}

This works well for me.

Josh


> Am 30.09.2010 16:54, schrieb Wilhelm:
>> Hi all,
>>
>> I think this is a well known problem, but I still can't find a solution
>> for it:
>>
>> Just for curiosty I tried to use a QTextEdit in a custom QItemDelegate
>> to render html text:
>>
>> void Delegate::paint(QPainter *painter, const QStyleOptionViewItem
>> &option, const QModelIndex &index) const {
>>     QString s = index.model()->data(index, Qt::DisplayRole).toString();
>>     QTextEdit te;
>>     te.setHtml(s);
>>     painter->save();
>>     painter->translate(option.rect.x(), option.rect.y());
>>     b.render(painter);
>>
> sorry, must read:
>
>    te.render(painter);
>
>
>>     painter->restore();
>> }
>>
>> This works almost, but the height of the cells is not correct.
>>
>> What am I doint wrong?
>>
>>
>
>
>



More information about the Qt-interest-old mailing list