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?
-
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":http://doc.trolltech.com/latest/stylesheet-examples.html#customizing-qtableview
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:
@
QTableView { alternate-background-color: #ebf4ff }
@ -
You can use delegate: http://doc.qt.nokia.com/4.7/qstyleditemdelegate.html
-
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.
-
Jonathan,
QTableView look can be modified in a number of ways:
- 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:
@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()
@QVariant QAbstractItemModel::headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const [virtual]@-
Use style-sheets. Refer to Qt Style Sheets Reference / QTableView in Assistant
-
Use Delegates. See Model/View Programming in Assistant
-
Use custom widgets as items via QAbstractItemView::setIndexWidget()
-
There other ways, but they are just inapropriate for the defined task
I'd suggest using 1 or 2 or 3
-
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.
@
void ModelDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{QStyledItemDelegate::paint(painter,option,index);
QStandardItemModel *m = (QStandardItemModel *) index.model();
StateItem *si = (StateItem *) m->itemFromIndex(index);QVariant v = index.data(Qt::UserRole+2);
QWidget *w = qobject_cast<QWidget *>( (QWidget *) v.value<void *>());
if (!w) {
qWarning() << "No widget " << FILE << LINE;
return;
}
int x2,y2;
QRect rect = option.rect;
if (w->isVisible()) {
x2 = rect.x() + rect.width() - visiblePixmap->width()-4;
y2 = rect.y() + (rect.height()- visiblePixmap->height())/2;
painter->drawPixmap(x2,y2,visiblePixmap);
}
if (si->symbolFilterOn) {
x2 = rect.x() + rect.width() - (2visiblePixmap->width())-4;
y2 = rec.y() + (rect.height()- filterPixmap->height())/2;
painter->drawPixmap(x2,y2,*filterPixmap);
}}
@[edit : code highlighted / Denis Kormalev]
-
DBoosalis, please use @ tags around your code.
-
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.