[Interest] Multicolored text in a QTableView cell?

Carl Schumann schumann at fnal.gov
Thu Jun 14 14:40:08 CEST 2012


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: results_kludge_on.png
Type: image/png
Size: 3193 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120614/dbbb4b0f/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: results_kludge_off.png
Type: image/png
Size: 3085 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120614/dbbb4b0f/attachment-0001.png>


More information about the Interest mailing list