Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. TableView background color
Qt 6.12 LTS is out! See what's new in the release blog

TableView background color

Scheduled Pinned Locked Moved Unsolved C++ Gurus
3 Posts 2 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mogli123
    wrote on last edited by koahnig
    #1

    Hello,

    I am new in qt and c++ and hope that someone can help me.
    I will change the background color in a TableView for some cells, but it doesn't work.

    tableanpassen.cpp

    Tableanpassen::Tableanpassen(QObject *parent):QSqlRelationalTableModel (parent)
    {
    }
    
    QVariant Tableanpassen::data(const QModelIndex &index, int role) const
    {
        int row = index.row();
        int col = index.column();
    
        if(role == Qt::BackgroundRole)
        {
            if (row == 1 && col == 2)   //change background only for cell(1,2)
           {
                QBrush Background(Qt::red);
                return Background;
           }
        }
        return QSqlRelationalTableModel::data(index, role);
    }
    

    tableanpassen.h

    #ifndef TABLEANPASSEN_H
    #define TABLEANPASSEN_H
    
    //#include <QSqlQueryModel>
    #include "QSqlRelationalTableModel"
    
    class Tableanpassen : public QSqlRelationalTableModel
    {
        Q_OBJECT
    
    public:
        Tableanpassen(QObject *parent = nullptr);
    
        QVariant data(const QModelIndex &item, int role) const;
    
    };
    
    #endif // TABLEANPASSEN_H
    

    and in the .cpp below i set the model but I think there is the mistake.

    test_screen::test_screen(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::test_screen)
    {
        ui->setupUi(this);
    
    
        model = new QSqlRelationalTableModel(this);
        model->setQuery("SELECT * FROM WekzeugWechselWarnung");
        model->setHeaderData(0, Qt::Horizontal, tr("Werkzeug Nr."));
        model->setHeaderData(1, Qt::Horizontal, tr("Zeit"));
    
        model->setData(model->index(1,2),QBrush(Qt::red), Qt::BackgroundRole);  (is that right)
    
        ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableView->setModel(model);
     ...
    

    Thank you for help.

    [koahnig:code tags added]

    raven-worxR 1 Reply Last reply
    0
    • M Mogli123

      Hello,

      I am new in qt and c++ and hope that someone can help me.
      I will change the background color in a TableView for some cells, but it doesn't work.

      tableanpassen.cpp

      Tableanpassen::Tableanpassen(QObject *parent):QSqlRelationalTableModel (parent)
      {
      }
      
      QVariant Tableanpassen::data(const QModelIndex &index, int role) const
      {
          int row = index.row();
          int col = index.column();
      
          if(role == Qt::BackgroundRole)
          {
              if (row == 1 && col == 2)   //change background only for cell(1,2)
             {
                  QBrush Background(Qt::red);
                  return Background;
             }
          }
          return QSqlRelationalTableModel::data(index, role);
      }
      

      tableanpassen.h

      #ifndef TABLEANPASSEN_H
      #define TABLEANPASSEN_H
      
      //#include <QSqlQueryModel>
      #include "QSqlRelationalTableModel"
      
      class Tableanpassen : public QSqlRelationalTableModel
      {
          Q_OBJECT
      
      public:
          Tableanpassen(QObject *parent = nullptr);
      
          QVariant data(const QModelIndex &item, int role) const;
      
      };
      
      #endif // TABLEANPASSEN_H
      

      and in the .cpp below i set the model but I think there is the mistake.

      test_screen::test_screen(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::test_screen)
      {
          ui->setupUi(this);
      
      
          model = new QSqlRelationalTableModel(this);
          model->setQuery("SELECT * FROM WekzeugWechselWarnung");
          model->setHeaderData(0, Qt::Horizontal, tr("Werkzeug Nr."));
          model->setHeaderData(1, Qt::Horizontal, tr("Zeit"));
      
          model->setData(model->index(1,2),QBrush(Qt::red), Qt::BackgroundRole);  (is that right)
      
          ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
          ui->tableView->setModel(model);
       ...
      

      Thank you for help.

      [koahnig:code tags added]

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Mogli123
      just to make sure: indexes are 0-based. So first row/column has an index of 0 not 1.
      In case you only showing 1 row of data in your table and wondering why the background doesn't get applied.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mogli123
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt
        • Unsolved