Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set background of specific row in QTableView
Qt 6.11 is out! See what's new in the release blog

Set background of specific row in QTableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 3.7k 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.
  • T Offline
    T Offline
    t0msk
    wrote on last edited by
    #1

    Hello, please how can I set background of specific row (for example second) in QTableView (programmatically).

    Student who loves C/C++

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      You can set it at the model level (all views connected to the model are affected)

      void setRowColor(QAbstractItemModel* model, int row, const QBrush& color, const QModelIndex& parent = QModelIndex()){
      if(!model) return;
      if(row<0 || row>=model->rowCount(parent)) return;
      const int colCount = model->columnCount(parent);
      for (int j = 0; j < colCount; ++j)
      model->setData(model->index(row,j,parent),color,Qt::BackgroundRole);
      }
      

      or use a delegate for the specific view (the one below lets you specify the color for entire row and column and for individual cell):

      #include <QStyledItemDelegate>
      #include <QApplication>
      #include <QHash>
      class BackGroungColorDelegate : public QStyledItemDelegate
      {
          Q_OBJECT
          Q_DISABLE_COPY(BackGroungColorDelegate);
      public:
          explicit BackGroungColorDelegate(QObject *parent = Q_NULLPTR)
              :QStyledItemDelegate(parent){}
          virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
          
              Q_ASSERT(index.isValid());
      
              QStyleOptionViewItem opt = option;
              initStyleOption(&opt, index);
              if (!index.parent().isValid()) {
                  if (hasCellColor(index.row(), index.column()))
                      opt.backgroundBrush = colorForCell(index.row(), index.column());
              }
              const QWidget *widget = option.widget;
              QStyle *style = widget ? widget->style() : QApplication::style();
              style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
          }
          void setColorForRow(int row, const QBrush& val){
              m_rowColors[row] = val;
          }
          QBrush colorForRow(int row) const {
              return m_rowColors.value(row);
          }
          void removeColorForRow(int row){
              m_rowColors.remove(row);
          }
          void clearRowColors(){
              m_rowColors.clear();
          }
          void setColorForCol(int col, const QBrush& val)
          {
              m_colColors[col] = val;
          }
          QBrush colorForCol(int col) const
          {
              return m_colColors.value(col);
          }
          void removeColorForCol(int col)
          {
              m_colColors.remove(col);
          }
          void clearColColors()
          {
              m_colColors.clear();
          }
          void setColorForCell(int row, int col, const QBrush& val){
              m_cellColors[cellKey(row, col)] = val;
          }
          QBrush colorForCell(int row, int col) const
          {
              return m_cellColors.value(cellKey(row, col), m_rowColors.value(row, m_colColors.value(col)));
          }
          void removeColorForCell(int row, int col)
          {
              m_cellColors.remove(cellKey(row, col));
          }
          void clearCellColors()
          {
              m_cellColors.clear();
          }
      private:
          QHash<quint64,QBrush> m_cellColors;
          QHash<int, QBrush> m_rowColors;
          QHash<int, QBrush> m_colColors;
          Q_DECL_CONSTEXPR quint64 cellKey(int row, int col) const
          {
              return (static_cast<quint64>(row) << 32) | static_cast<quint64>(col);
          }
      protected:
          bool hasCellColor(int row, int col) const{
              return m_cellColors.contains(cellKey(row, col))
                 || m_rowColors.contains(row)
                 || m_colColors.contains(col);
          }
      };
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1

      • Login

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