[Qt-interest] Using QTextEdit in QItemDelegate

Josh jnfo-c at grauman.com
Fri Oct 1 17:22:20 CEST 2010


If I rememeber, what I did to solve this was to set the QTextEdit to a 
fixed height. When I was filling my tableWidget, I called
tableWidget->setRowHeight(row, maxHeight+2); on each row.

Then, here is my delegate function to create the editor:

QWidget * HtmlDelegate::createEditor(QWidget *parent, const 
QStyleOptionViewItem &, const QModelIndex &) const
{
   QTextEdit *te = new QTextEdit(parent);
   te->setFixedHeight(maxHeight+maxHeight/8);
   te->document()->setDocumentMargin(0);
   te->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   return(te);
}

I changed a couple things to take out things that my program does that you 
wouldn't want to, so the code is untested, but should work. You may want 
to mess with the two formulas for the height as well...

Josh


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



More information about the Qt-interest-old mailing list