[Development] Multicolored text in a QTableView cell?
Konstantin Ritt
ritt.ks at gmail.com
Thu Jun 14 14:43:36 CEST 2012
development at qt-project.org is a wrong list for questions like that.
Konstantin
2012/6/14 Carl Schumann <schumann at fnal.gov>:
> On 6/13/2012 11:51 AM, Konrad Rosenbaum wrote:
>>
>> Hi,
>>
>> On Wednesday 13 June 2012 17:27:20 Carl Schumann wrote:
>>>
>>> Where MccModel subclassed from QAbstractTableModel. This did not
>>> work, specifically it displayed the whole long rich text string in
>>> black. How should I be doing multicolored table cells please?
>>
>> Use a custom delegate instead: derive a class from QItemDelegate and
>> override
>>
>> the paint method to do your custom drawing. If you want to use rich text
>> you
>> can for example use QTextDocument::drawContents as a helper.
>>
>>
>> Konrad
>
> Konrad,
>
> Thanks for the help thus far. I have done as you recommend. There must
> be something I don't quite get yet though as my cell text is offset from
> where I would expect it to be. The code for my paint method is as follows:
>>
>> void MccModelItemDelegate::paint( QPainter * painter, const
>> QStyleOptionViewItem & option, const QModelIndex & index ) const
>> {
>> painter->save();
>> QLabel text( "<font color=\"blue\">" + QString::number(index.row())
>> + "</font> and "
>> "<font color=\"red\">" + QString::number(index.column())
>> + "</font>" );
>> text.adjustSize();
>> int top = option.rect.top() + (option.rect.height() - text.height()) /
>> 2; // center text in cell vertically
>> int left = option.rect.left() + text.height() / 3; // indent a third of
>> the height - this is somewhat arbitary
>> #if 1 // XXX: Kludge
>> top += 22;
>> left += 20;
>> #endif
>> QPoint render_at( left, top );
>> text.render( painter, render_at );
>> painter->restore();
>> return;
>> }
>
> On my system I need the kludge to get the cell text positioned correctly.
> I have attached two screen shots to indicate the effect of the kludge. Any
> insights would be appreciated please. Thanks for any help.
>
> Sincerely,
> Carl Schumann
>
>
>
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development
>
More information about the Development
mailing list