Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, December 2006
Updating QTableItem's the smart way


Message 1 in thread

Pretty sure I'm doing this right.
I've got a subclassed QTableItem, which I use to change the font style &
color, etc.
If the status of the cell changes (which would warrant a different
color), I simply delete the QTableItem and create a new one w/ the right
colors.  Seems like a roundabout way to do it, but I can't think of any
other way.

table_item::table_item( QTable* t, EditType et, QColor c, QColor f,
const QString txt ) : QTableItem( t, et, txt )
{
        color           = c;
        fnt_color       = f;
        text = txt;
}

table_item::~table_item()
{

}

int table_item::alignment() const
{
        return Qt::AlignLeft;
}

void table_item::paint( QPainter* p, const QColorGroup &cg, const QRect
&cr, bool selected )
{
        QColorGroup g(cg);
        QColor txt_c( 255, 255, 255 );
        // txt_c.setNamedColor( "FFFFFF" );

        g.setColor( QColorGroup::Base, color );
        g.setColor( QColorGroup::Text, fnt_color );

        p->setPen( Qt::white );
        QFont f( "Tahoma", 10, QFont::Bold );
        p->setFont( f );
        QTableItem::paint( p, g, cr, selected );
}

This is essentially how I create a cell:

void trdview::insert_cell( int col, QString t, int trd )
{
        QPainter p( tbl );
        QColorGroup cg;
        cg.setColor( QColorGroup::Background, Qt::red );
        cg.setColor( QColorGroup::Text, Qt::green );

        QRect rect = tbl->cellRect( 0, col );
        QColor c( 232, 232, 232 );
        c.setNamedColor( "#003366" );

        QColor f( 255, 255, 255 );

        table_item *temp;
        temp = new table_item( tbl, QTableItem::Never, c, f, t );
        temp->paint( &p, cg, rect, FALSE );
        tbl->setItem( 0, col, temp );
}

and an update one:

void trdview::update_cell( int row, int col, QString t, int trd )
{
        QPainter p( tbl );
        QColorGroup cg;
        cg.setColor( QColorGroup::Background, Qt::red );
        cg.setColor( QColorGroup::Text, Qt::green );

        QRect rect = tbl->cellRect( row, col );
        QColor c( 0, 0, 0 );
        c.setNamedColor( "#003366" );  // couldn't find the RGB code,
use HEX
        QColor f( 255, 255, 255 );
         f.setNamedColor( "#FFFF66" ); // couldn't find the RGB code,
use HEX

        QTableItem *old = tbl->item( row, col );
        tbl->takeItem( old );
        delete old;

        table_item *temp;
        temp = new table_item( tbl, QTableItem::Never, c, f, t );
        temp->paint( &p, cg, rect, FALSE );
        tbl_trades->setItem( row, col, temp );
}

--
 [ signature omitted ] 

Message 2 in thread

Paul England a écrit :
> Pretty sure I'm doing this right.
> I've got a subclassed QTableItem, which I use to change the font style &
> color, etc.
> If the status of the cell changes (which would warrant a different
> color), I simply delete the QTableItem and create a new one w/ the right
> colors.  Seems like a roundabout way to do it, but I can't think of any
> other way.
>
> table_item::table_item( QTable* t, EditType et, QColor c, QColor f,
> const QString txt ) : QTableItem( t, et, txt )
> {
>         color           = c;
>         fnt_color       = f;
>         text = txt;
> }
>
> table_item::~table_item()
> {
>
> }
>
> int table_item::alignment() const
> {
>         return Qt::AlignLeft;
> }
>
> void table_item::paint( QPainter* p, const QColorGroup &cg, const QRect
> &cr, bool selected )
> {
>         QColorGroup g(cg);
>         QColor txt_c( 255, 255, 255 );
>         // txt_c.setNamedColor( "FFFFFF" );
>
>         g.setColor( QColorGroup::Base, color );
>         g.setColor( QColorGroup::Text, fnt_color );
>
>         p->setPen( Qt::white );
>         QFont f( "Tahoma", 10, QFont::Bold );
>         p->setFont( f );
>         QTableItem::paint( p, g, cr, selected );
> }
>
> This is essentially how I create a cell:
>
> void trdview::insert_cell( int col, QString t, int trd )
> {
>         QPainter p( tbl );
>         QColorGroup cg;
>         cg.setColor( QColorGroup::Background, Qt::red );
>         cg.setColor( QColorGroup::Text, Qt::green );
>
>         QRect rect = tbl->cellRect( 0, col );
>         QColor c( 232, 232, 232 );
>         c.setNamedColor( "#003366" );
>
>         QColor f( 255, 255, 255 );
>
>         table_item *temp;
>         temp = new table_item( tbl, QTableItem::Never, c, f, t );
>         temp->paint( &p, cg, rect, FALSE );
>         tbl->setItem( 0, col, temp );
> }
>
> and an update one:
>
> void trdview::update_cell( int row, int col, QString t, int trd )
> {
>         QPainter p( tbl );
>         QColorGroup cg;
>         cg.setColor( QColorGroup::Background, Qt::red );
>         cg.setColor( QColorGroup::Text, Qt::green );
>
>         QRect rect = tbl->cellRect( row, col );
>         QColor c( 0, 0, 0 );
>         c.setNamedColor( "#003366" );  // couldn't find the RGB code,
> use HEX
>         QColor f( 255, 255, 255 );
>          f.setNamedColor( "#FFFF66" ); // couldn't find the RGB code,
> use HEX
>
>         QTableItem *old = tbl->item( row, col );
>         tbl->takeItem( old );
>         delete old;
>
>         table_item *temp;
>         temp = new table_item( tbl, QTableItem::Never, c, f, t );
>         temp->paint( &p, cg, rect, FALSE );
>         tbl_trades->setItem( row, col, temp );
> }
Hi,

Why don't you just add a method to your own table item class 
implementation?
You could just do the following once done:
table_item *cell = dynamic_cast<table_item*>(tbl->item(row, col));
if(cell)
  cell->setNewColors(color, fontColor);

BTW, why are you calling those paints on the newly created cells? I 
don't see their use -but maybe I missed something, sorry in this case ;)

Regards,

Denys

PS Sorry for the spam Paul, I forgot to answer to the qt-interest :-/

--
 [ signature omitted ]