: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/usieve/stoopidsimple.com/includes/common.inc on line 1782.
: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/usieve/stoopidsimple.com/includes/common.inc on line 1782.
QTable alternating colors | stoopidsimple.com

QTable alternating colors

salem | 2005-10-04 01:19

Most modern table widgets are displayed with alternating colors to help visually distinguish between rows.
Qt 4 makes this stoopid simple for you by providing QTableWidget::setAlternatingColors(). However in Qt 3.x you have to roll your own. Read on for discussion and sample implementation.

The way I do it to subclass QTableItem and conditionaly change the QColorGroup::Base of the item (note that the background of a QTableItem is drawn with QColorGroup::Base). I use the "midlight" role of the QColorGroup to give light contrast between the normal background rows and the darker background rows.
Here is some example code:

class AlternatingColorItem : public QTableItem {
  AlternatingColorItem( QTable *t, EditType et, const QString &text = "" )
    : QTableItem(t,et,txt) {}
  void paint( QPainter          *p,
              const QColorGroup &cg,
              const QRect       &cr,
              bool               selected ) {
    QColorGroup g(cg);
    if( row()%2 ) {
      g.setColor( QColorGroup::Base,cg.midlight() );
    }
    QTableItem::paint(p,g,cr,selected);
};

The nice part of this is that it works regardless of system colors (good idea Trolltech!).

Here is an example screenshot:

Lfhex

Tonality

User login

Navigation

PHP

Kccmp

Java

Homestar

Slashdot


Advertisement