[Qt-interest] Using QTextEdit in QItemDelegate

Wilhelm wilhelm.meier at fh-kl.de
Fri Oct 1 09:21:11 CEST 2010


Am 30.09.2010 19:08, schrieb Josh:
> 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.

Thanks, it works here too.

But if I edit the contents (again using a QTextEdit as itemEditor), the
cell is very enlarged !!!

>
> 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?
>>>
>>>
>>
>>
>>


-- 
Wilhelm





More information about the Qt-interest-old mailing list