Please nominate your Qt Champions for 2021!

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: "":

    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 }

  • 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:

    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:

    @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]@

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

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

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

    4. 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

    QStandardItemModel *m = (QStandardItemModel *) index.model();
    StateItem *si = (StateItem *) m->itemFromIndex(index);

    QVariant v =;
    QWidget *w = qobject_cast<QWidget *>( (QWidget *) v.value<void *>());
    if (!w) {
    qWarning() << "No widget " << FILE << LINE;
    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;
    if (si->symbolFilterOn) {
    x2 = rect.x() + rect.width() - (2
    y2 = rec.y() + (rect.height()- filterPixmap->height())/2;


    [edit : code highlighted / Denis Kormalev]

  • DBoosalis, please use @ tags around your code.

  • All,

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

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

  • 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.
    You can google further for examples.

Log in to reply