December 1, 2010

Jonathan Jonathan
Lab Rat
50 posts

How can I modify the appearance of a QTableView?

 

How can I modify the appearance of a QTableView? For instance by changing the background colour of a column, or changing the colours or fonts used in row and column headers?

(see http://doc.qt.nokia.com/4.7/sql-cachedtable.html)

9 replies

December 1, 2010

Milot Shala Milot Shala
Lab Rat
396 posts

You can use Qt style sheets for customizing the table view, you can see more detailed description how to so here: http://doc.trolltech.com/latest/stylesheet-examples.html#customizing-qtableview [doc.trolltech.com]

You can use also the setAlternatingRowColors(bool); to set the alternate colors, which it has a default color but you can change it via the style sheets using the following style sheet syntax within the setStyleSheet(QString) function:

  1. QTableView { alternate-background-color: #ebf4ff }

December 1, 2010

blex blex
Lab Rat
192 posts

You can use delegate: http://doc.qt.nokia.com/4.7/qstyleditemdelegate.html

 Signature 

————————————

Oleksiy Balabay

December 1, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

In addition to blex. If you need to have some rich text in your table than you can use QTextDocument in delegates. It will do all stuff for you.

December 1, 2010

infoctopus infoctopus
Lab Rat
120 posts

Jonathan,

QTableView look can be modified in a number of ways:

1. Background color of a column, text color or font used in items or headers can be specified in a model, that your QTableView is connected to:

  1. QVariant QAbstractItemModel::data ( const QModelIndex & index, int role = Qt::DisplayRole ) const [pure virtual]

Implement this function in your model and return different values depending on values of role and index (i.e. row, column).

Similarly you can define look for headers. Use yourModel::headerData()

  1. QVariant QAbstractItemModel::headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const [virtual]

2. Use style-sheets. Refer to Qt Style Sheets Reference / QTableView in Assistant

3. Use Delegates. See Model/View Programming in Assistant

4. Use custom widgets as items via QAbstractItemView::setIndexWidget()

5. There other ways, but they are just inapropriate for the defined task

I’d suggest using 1 or 2 or 3

 Signature 

Qt rulez

December 1, 2010

DBoosalis DBoosalis
Ant Farmer
51 posts

Here is an example that shows how I use a delegate to paint pixmaps in a cell which is dependent on the value of some user data.

Hope this example is of some help.

  1. void ModelDelegate::paint(QPainter *painter,
  2.      const QStyleOptionViewItem &option,
  3.      const QModelIndex &index) const
  4. {
  5.  
  6.   QStyledItemDelegate::paint(painter,option,index);
  7.   QStandardItemModel *m = (QStandardItemModel *) index.model();
  8.   StateItem *si = (StateItem  *) m->itemFromIndex(index);
  9.  
  10.   QVariant v   = index.data(Qt::UserRole+2);
  11.   QWidget *w   = qobject_cast<QWidget *>( (QWidget *) v.value<void *>());
  12.   if (!w) {
  13.   qWarning() << "No widget " << __FILE__ << __LINE__;
  14.   return;
  15.   }
  16.   int x2,y2;  
  17.   QRect rect = option.rect;
  18.   if (w->isVisible()) {
  19.     x2 = rect.x() + rect.width() - visiblePixmap->width()-4;
  20.     y2 = rect.y() + (rect.height()- visiblePixmap->height())/2;
  21.     painter->drawPixmap(x2,y2,*visiblePixmap);
  22.   }
  23.   if (si->symbolFilterOn) {
  24.     x2  = rect.x() + rect.width() - (2*visiblePixmap->width())-4;
  25.     y2 = rec.y() + (rect.height()- filterPixmap->height())/2;
  26.     painter->drawPixmap(x2,y2,*filterPixmap);
  27.   }
  28.  
  29. }

edit : code highlighted / Denis Kormalev

December 1, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

DBoosalis, please use @ tags around your code.

December 1, 2010

Jonathan Jonathan
Lab Rat
50 posts

All,

Many thanks for your extremely helpful responses. These were just what I needed.

December 2, 2010

Andre Andre
Robot Herder
6296 posts

Note that adapting the style of the headers takes more effort. You’ll have to resort to subclassing QHeaderView and reimplementing paint().

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 3, 2010

Irfan Irfan
Lab Rat
3 posts

If you want to do more c++ Qt codeing then Delegate is there to manipulate further. You can change the color of particular cell on some condition, you can have your own cell (cutstomized widget cell) and what not ….?
There are hell of the text available to read more about it i am sorry but it is really a bit complex too.
http://doc.qt.nokia.com/4.1/model-view-delegate.html
You can google further for examples.

 
  ‹‹ Qt winXP binary on windows7?      ActiveRecord in Qt ››

You must log in to post a reply. Not a member yet? Register here!