Set background of specific row in QTableView



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



  • 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);
        }
    };
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.