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. Qtableview set background color to some columns
QtWS25 Last Chance

Qtableview set background color to some columns

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 7 Posters 6.7k Views
  • 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.
  • N Offline
    N Offline
    n-2204
    wrote on last edited by
    #1

    I have 3 Qtableview with different no. of rows and columns. I need to set column color to 2-3 column in each Table if new row is added then color should be added.
    How to do that one way is using delegate any other way to set column color ?
    Thanks in advance3b092add-5fd3-4214-b9be-342f2e724529-image.png

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Yes you can also set via Qt::BackgroundRole either via setData
      or via your customs models data function if you have a custom model.

      1 Reply Last reply
      4
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        In addition to @mrjj, QIdentityProxyModel is also a solution.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          While the answers above are perfectly valid I think in your case it might be easier to just use a delegate:

          class BackgroundBrushDelegate : public QStyledItemDelegate{
              Q_OBJECT
              Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush NOTIFY backgroundBrushChanged)
              Q_DISABLE_COPY(BackgroundBrushDelegate)
          public:
              explicit BackgroundBrushDelegate(QObject *parent = nullptr)
                  : QStyledItemDelegate(parent)
              {}
              BackgroundBrushDelegate(const QBrush& brush, QObject *parent = nullptr)
                  : QStyledItemDelegate(parent)
                  , m_backgroundBrush(brush)
              {}
              const QBrush& backgroundBrush() const { return m_backgroundBrush; }
              void setBackgroundBrush(const QBrush& brush)
              {
                  if(m_backgroundBrush==brush)
                      return;
                  m_backgroundBrush=brush;
                  backgroundBrushChanged(m_backgroundBrush);
              }
          signals:
              void backgroundBrushChanged(const QBrush& brush);
          protected:
              void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
              {
                  QStyledItemDelegate::initStyleOption(option,index);
                  option->backgroundBrush = m_backgroundBrush;
              }
          private:
              QBrush m_backgroundBrush;
          }
          

          You can use it with tableView->setItemDelegateForColumn(0,new BackgroundBrushDelegate(QBrush(Qt::yellow),tableView));

          "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

          N C 3 Replies Last reply
          3
          • N Offline
            N Offline
            n-2204
            wrote on last edited by
            #5

            Actually i already set delegate on column for integer value so if i am now doing using delegate i am not getting color on that column where i set delegate.
            so anyother approach to color the columns

            VRoninV 1 Reply Last reply
            0
            • N n-2204

              Actually i already set delegate on column for integer value so if i am now doing using delegate i am not getting color on that column where i set delegate.
              so anyother approach to color the columns

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @n-2204 said in Qtableview set background color to some columns:

              Actually i already set delegate on column for integer value

              You can just mix the two delegates. Can you show us the code of your current delegate?

              "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
              0
              • N Offline
                N Offline
                n-2204
                wrote on last edited by VRonin
                #7

                How can i mix the delegate?
                Delegate for int data ,
                Code:

                #include "intvalmodel.h"
                #include "GAS_tool.h"
                
                intvalmodel::intvalmodel(QObject* parent) :QStyledItemDelegate(parent)
                {
                
                
                }
                QWidget* QStyledItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
                	const QModelIndex& index) const
                {
                	QLineEdit* editor = new QLineEdit(parent);
                	// QIntValidator* data = new QIntValidator(0, 999);
                	 //editor->setValidator(data);
                	editor->setValidator(new QIntValidator);
                	return editor;
                
                }
                void intvalmodel::setEditorData(QWidget* editor, const QModelIndex& index) const
                {
                	 QString value1 = index.model()->data(index, Qt::EditRole).toString();
                 QLineEdit* line = static_cast<QLineEdit*>(editor);
                 line->setText(value1);
                }
                void intvalmodel::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
                {
                	  QLineEdit* line = static_cast<QLineEdit*>(editor);
                   QString value1 = line->text();
                   model->setData(index, value1);
                }
                void intvalmodel::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
                {
                	editor->setGeometry(option.rect);
                }
                
                  delegate = new intvalmodel();
                 ui.tableView->setItemDelegateForColumn(1,delegate); 
                    ui.tableView->setItemDelegateForColumn(2, delegate);
                  ui.tableView->setItemDelegateForColumn(3,delegate); 
                

                i need to set color for column 1,2,3,4,5 as my 3 column used in int delegate how to set color to those also ?

                VRoninV 1 Reply Last reply
                0
                • N n-2204

                  How can i mix the delegate?
                  Delegate for int data ,
                  Code:

                  #include "intvalmodel.h"
                  #include "GAS_tool.h"
                  
                  intvalmodel::intvalmodel(QObject* parent) :QStyledItemDelegate(parent)
                  {
                  
                  
                  }
                  QWidget* QStyledItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
                  	const QModelIndex& index) const
                  {
                  	QLineEdit* editor = new QLineEdit(parent);
                  	// QIntValidator* data = new QIntValidator(0, 999);
                  	 //editor->setValidator(data);
                  	editor->setValidator(new QIntValidator);
                  	return editor;
                  
                  }
                  void intvalmodel::setEditorData(QWidget* editor, const QModelIndex& index) const
                  {
                  	 QString value1 = index.model()->data(index, Qt::EditRole).toString();
                   QLineEdit* line = static_cast<QLineEdit*>(editor);
                   line->setText(value1);
                  }
                  void intvalmodel::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
                  {
                  	  QLineEdit* line = static_cast<QLineEdit*>(editor);
                     QString value1 = line->text();
                     model->setData(index, value1);
                  }
                  void intvalmodel::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
                  {
                  	editor->setGeometry(option.rect);
                  }
                  
                    delegate = new intvalmodel();
                   ui.tableView->setItemDelegateForColumn(1,delegate); 
                      ui.tableView->setItemDelegateForColumn(2, delegate);
                    ui.tableView->setItemDelegateForColumn(3,delegate); 
                  

                  i need to set color for column 1,2,3,4,5 as my 3 column used in int delegate how to set color to those also ?

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @n-2204 said in Qtableview set background color to some columns:

                  QString value1 = line->text();
                  model->setData(index, value1);

                  Why do you have an editor for integers but then you save the data as a string? do you have a particular need?

                  "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

                  N 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    @n-2204 said in Qtableview set background color to some columns:

                    QString value1 = line->text();
                    model->setData(index, value1);

                    Why do you have an editor for integers but then you save the data as a string? do you have a particular need?

                    N Offline
                    N Offline
                    n-2204
                    wrote on last edited by n-2204
                    #9

                    @VRonin how can i mix two delegates?
                    I need to enter integer values in column

                    VRoninV 1 Reply Last reply
                    0
                    • N n-2204

                      @VRonin how can i mix two delegates?
                      I need to enter integer values in column

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #10

                      @n-2204 said in Qtableview set background color to some columns:

                      I need to enter integer values in column

                      You don't need to do anything special. The default delegate already does this.
                      If, and this is a guess on my part, you are using QStandardItemModel you can solve your problem by never using the constructors QStandardItem(QIcon,QString) or QStandardItem(QString). Just construct an empty item and then call item->setData and pass yor integer to it, do not convert it to a string. You'll see the default delegate (or the modifed version I posted above) will do exactly what you want

                      "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

                      N 1 Reply Last reply
                      2
                      • VRoninV VRonin

                        @n-2204 said in Qtableview set background color to some columns:

                        I need to enter integer values in column

                        You don't need to do anything special. The default delegate already does this.
                        If, and this is a guess on my part, you are using QStandardItemModel you can solve your problem by never using the constructors QStandardItem(QIcon,QString) or QStandardItem(QString). Just construct an empty item and then call item->setData and pass yor integer to it, do not convert it to a string. You'll see the default delegate (or the modifed version I posted above) will do exactly what you want

                        N Offline
                        N Offline
                        n-2204
                        wrote on last edited by
                        #11

                        @VRonin so in above share your code how can i add int delegate ?
                        item->setdata i tried but not working for me
                        if you have code can you pls share it?
                        Thanks

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

                          Disclaimer

                          I think your delegate is doing more harm than good but i'll go along with it anyway.


                          Inside the class you defined in intvalmodel.h add:

                          public:
                          void setBrushForColumn(int col, const QBrush& brush);
                          protected:
                          void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override;
                          private:
                          QMap<int,QBrush> m_brushes;
                          

                          now intvalmodel.cpp

                          #include "intvalmodel.h"
                          #include "GAS_tool.h"
                          
                          intvalmodel::intvalmodel(QObject* parent) :QStyledItemDelegate(parent)
                          {
                          
                          
                          }
                          QWidget* QStyledItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
                          	const QModelIndex& index) const
                          {
                          	QLineEdit* editor = new QLineEdit(parent);
                          	// QIntValidator* data = new QIntValidator(0, 999);
                          	 //editor->setValidator(data);
                          	editor->setValidator(new QIntValidator);
                          	return editor;
                          
                          }
                          void intvalmodel::setEditorData(QWidget* editor, const QModelIndex& index) const
                          {
                          	 QString value1 = index.model()->data(index, Qt::EditRole).toString();
                           QLineEdit* line = static_cast<QLineEdit*>(editor);
                           line->setText(value1);
                          }
                          void intvalmodel::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
                          {
                          	  QLineEdit* line = static_cast<QLineEdit*>(editor);
                             model->setData(index, line->text().toInt());
                          }
                          void intvalmodel::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
                          {
                          	editor->setGeometry(option.rect);
                          }
                          
                          void intvalmodel::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
                              {
                                  QStyledItemDelegate::initStyleOption(option,index);
                                  option->backgroundBrush = m_brushes.value(index.column(),option->backgroundBrush);
                              }
                          void intvalmodel::setBrushForColumn(int col, const QBrush& brush){
                          if(col<0 || col >= columnCount())
                          return;
                          m_brushes[col]=brush;
                          }
                          

                          Now you can use it:

                          delegate = new intvalmodel();
                          delegate ->setBrushForColumn(1,QBrush(Qt::yellow));
                          delegate ->setBrushForColumn(2,QBrush(Qt::red));
                          delegate ->setBrushForColumn(3,QBrush(Qt::blue));
                           ui.tableView->setItemDelegateForColumn(1,delegate); 
                              ui.tableView->setItemDelegateForColumn(2, delegate);
                            ui.tableView->setItemDelegateForColumn(3,delegate); 
                          

                          "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

                          N 1 Reply Last reply
                          2
                          • VRoninV VRonin

                            Disclaimer

                            I think your delegate is doing more harm than good but i'll go along with it anyway.


                            Inside the class you defined in intvalmodel.h add:

                            public:
                            void setBrushForColumn(int col, const QBrush& brush);
                            protected:
                            void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override;
                            private:
                            QMap<int,QBrush> m_brushes;
                            

                            now intvalmodel.cpp

                            #include "intvalmodel.h"
                            #include "GAS_tool.h"
                            
                            intvalmodel::intvalmodel(QObject* parent) :QStyledItemDelegate(parent)
                            {
                            
                            
                            }
                            QWidget* QStyledItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
                            	const QModelIndex& index) const
                            {
                            	QLineEdit* editor = new QLineEdit(parent);
                            	// QIntValidator* data = new QIntValidator(0, 999);
                            	 //editor->setValidator(data);
                            	editor->setValidator(new QIntValidator);
                            	return editor;
                            
                            }
                            void intvalmodel::setEditorData(QWidget* editor, const QModelIndex& index) const
                            {
                            	 QString value1 = index.model()->data(index, Qt::EditRole).toString();
                             QLineEdit* line = static_cast<QLineEdit*>(editor);
                             line->setText(value1);
                            }
                            void intvalmodel::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
                            {
                            	  QLineEdit* line = static_cast<QLineEdit*>(editor);
                               model->setData(index, line->text().toInt());
                            }
                            void intvalmodel::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
                            {
                            	editor->setGeometry(option.rect);
                            }
                            
                            void intvalmodel::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
                                {
                                    QStyledItemDelegate::initStyleOption(option,index);
                                    option->backgroundBrush = m_brushes.value(index.column(),option->backgroundBrush);
                                }
                            void intvalmodel::setBrushForColumn(int col, const QBrush& brush){
                            if(col<0 || col >= columnCount())
                            return;
                            m_brushes[col]=brush;
                            }
                            

                            Now you can use it:

                            delegate = new intvalmodel();
                            delegate ->setBrushForColumn(1,QBrush(Qt::yellow));
                            delegate ->setBrushForColumn(2,QBrush(Qt::red));
                            delegate ->setBrushForColumn(3,QBrush(Qt::blue));
                             ui.tableView->setItemDelegateForColumn(1,delegate); 
                                ui.tableView->setItemDelegateForColumn(2, delegate);
                              ui.tableView->setItemDelegateForColumn(3,delegate); 
                            
                            N Offline
                            N Offline
                            n-2204
                            wrote on last edited by n-2204
                            #13

                            @VRonin Thanks

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              n-2204
                              wrote on last edited by
                              #14

                              if just to color one column and no validation on input in column then that done using Qt::BackgroundRole either via setData so need to refer which class QAbstractitem

                              1 Reply Last reply
                              0
                              • VRoninV VRonin

                                While the answers above are perfectly valid I think in your case it might be easier to just use a delegate:

                                class BackgroundBrushDelegate : public QStyledItemDelegate{
                                    Q_OBJECT
                                    Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush NOTIFY backgroundBrushChanged)
                                    Q_DISABLE_COPY(BackgroundBrushDelegate)
                                public:
                                    explicit BackgroundBrushDelegate(QObject *parent = nullptr)
                                        : QStyledItemDelegate(parent)
                                    {}
                                    BackgroundBrushDelegate(const QBrush& brush, QObject *parent = nullptr)
                                        : QStyledItemDelegate(parent)
                                        , m_backgroundBrush(brush)
                                    {}
                                    const QBrush& backgroundBrush() const { return m_backgroundBrush; }
                                    void setBackgroundBrush(const QBrush& brush)
                                    {
                                        if(m_backgroundBrush==brush)
                                            return;
                                        m_backgroundBrush=brush;
                                        backgroundBrushChanged(m_backgroundBrush);
                                    }
                                signals:
                                    void backgroundBrushChanged(const QBrush& brush);
                                protected:
                                    void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
                                    {
                                        QStyledItemDelegate::initStyleOption(option,index);
                                        option->backgroundBrush = m_backgroundBrush;
                                    }
                                private:
                                    QBrush m_backgroundBrush;
                                }
                                

                                You can use it with tableView->setItemDelegateForColumn(0,new BackgroundBrushDelegate(QBrush(Qt::yellow),tableView));

                                N Offline
                                N Offline
                                n-2204
                                wrote on last edited by VRonin
                                #15

                                @VRonin

                                class BackgroundBrushDelegate : public QStyledItemDelegate{
                                    Q_OBJECT
                                    Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush NOTIFY backgroundBrushChanged)
                                    Q_DISABLE_COPY(BackgroundBrushDelegate)
                                public:
                                    explicit BackgroundBrushDelegate(QObject *parent = nullptr)
                                        : QStyledItemDelegate(parent)
                                    {}
                                    BackgroundBrushDelegate(const QBrush& brush, QObject *parent = nullptr)
                                        : QStyledItemDelegate(parent)
                                        , m_backgroundBrush(brush)
                                    {}
                                    const QBrush& backgroundBrush() const { return m_backgroundBrush; }
                                    void setBackgroundBrush(const QBrush& brush)
                                    {
                                        if(m_backgroundBrush==brush)
                                            return;
                                        m_backgroundBrush=brush;
                                        backgroundBrushChanged(m_backgroundBrush);
                                    }
                                signals:
                                    void backgroundBrushChanged(const QBrush& brush);
                                protected:
                                    void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
                                    {
                                        QStyledItemDelegate::initStyleOption(option,index);
                                        option->backgroundBrush = m_backgroundBrush;
                                    }
                                private:
                                    QBrush m_backgroundBrush;
                                }
                                

                                when i am using this code
                                after applying brushdelegate in that column i am not able to type other then integer can you plz help me how to do ?

                                JonBJ 1 Reply Last reply
                                0
                                • N Offline
                                  N Offline
                                  n-2204
                                  wrote on last edited by
                                  #16

                                  Please help
                                  stuck for some assignment
                                  Thankyou

                                  VRoninV 1 Reply Last reply
                                  0
                                  • N n-2204

                                    @VRonin

                                    class BackgroundBrushDelegate : public QStyledItemDelegate{
                                        Q_OBJECT
                                        Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush NOTIFY backgroundBrushChanged)
                                        Q_DISABLE_COPY(BackgroundBrushDelegate)
                                    public:
                                        explicit BackgroundBrushDelegate(QObject *parent = nullptr)
                                            : QStyledItemDelegate(parent)
                                        {}
                                        BackgroundBrushDelegate(const QBrush& brush, QObject *parent = nullptr)
                                            : QStyledItemDelegate(parent)
                                            , m_backgroundBrush(brush)
                                        {}
                                        const QBrush& backgroundBrush() const { return m_backgroundBrush; }
                                        void setBackgroundBrush(const QBrush& brush)
                                        {
                                            if(m_backgroundBrush==brush)
                                                return;
                                            m_backgroundBrush=brush;
                                            backgroundBrushChanged(m_backgroundBrush);
                                        }
                                    signals:
                                        void backgroundBrushChanged(const QBrush& brush);
                                    protected:
                                        void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
                                        {
                                            QStyledItemDelegate::initStyleOption(option,index);
                                            option->backgroundBrush = m_backgroundBrush;
                                        }
                                    private:
                                        QBrush m_backgroundBrush;
                                    }
                                    

                                    when i am using this code
                                    after applying brushdelegate in that column i am not able to type other then integer can you plz help me how to do ?

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by JonB
                                    #17

                                    @n-2204 said in Qtableview set background color to some columns:

                                    after applying brushdelegate in that column i am not able to type other then integer can you plz help me how to do ?

                                    This does not sound like a brush delegate issue. Earlier you have in createEditor() :

                                    editor->setValidator(new QIntValidator);
                                    

                                    Make sure that is applying to the column (look at index parameter) where you want an integer, not some other column, at a guess?

                                    1 Reply Last reply
                                    2
                                    • N n-2204

                                      Please help
                                      stuck for some assignment
                                      Thankyou

                                      VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by
                                      #18

                                      @n-2204 said in Qtableview set background color to some columns:

                                      stuck for some assignment

                                      I'll quote the wise @mrjj

                                      the teacher will catch it and fail them if too much code is just copied.

                                      We gave you 2 possible solutions, one with and one without validator. We even integrated it in your existing code. What more can we do to help?

                                      "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
                                      0
                                      • N Offline
                                        N Offline
                                        n-2204
                                        wrote on last edited by
                                        #19

                                        Thanks all for you help
                                        May be i asked more that is easy for you ,but as i am new to c++ and qt, so i am not aware of all the concepts.
                                        so for me its little tough in implementation

                                        1 Reply Last reply
                                        0
                                        • VRoninV VRonin

                                          While the answers above are perfectly valid I think in your case it might be easier to just use a delegate:

                                          class BackgroundBrushDelegate : public QStyledItemDelegate{
                                              Q_OBJECT
                                              Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush NOTIFY backgroundBrushChanged)
                                              Q_DISABLE_COPY(BackgroundBrushDelegate)
                                          public:
                                              explicit BackgroundBrushDelegate(QObject *parent = nullptr)
                                                  : QStyledItemDelegate(parent)
                                              {}
                                              BackgroundBrushDelegate(const QBrush& brush, QObject *parent = nullptr)
                                                  : QStyledItemDelegate(parent)
                                                  , m_backgroundBrush(brush)
                                              {}
                                              const QBrush& backgroundBrush() const { return m_backgroundBrush; }
                                              void setBackgroundBrush(const QBrush& brush)
                                              {
                                                  if(m_backgroundBrush==brush)
                                                      return;
                                                  m_backgroundBrush=brush;
                                                  backgroundBrushChanged(m_backgroundBrush);
                                              }
                                          signals:
                                              void backgroundBrushChanged(const QBrush& brush);
                                          protected:
                                              void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override
                                              {
                                                  QStyledItemDelegate::initStyleOption(option,index);
                                                  option->backgroundBrush = m_backgroundBrush;
                                              }
                                          private:
                                              QBrush m_backgroundBrush;
                                          }
                                          

                                          You can use it with tableView->setItemDelegateForColumn(0,new BackgroundBrushDelegate(QBrush(Qt::yellow),tableView));

                                          C Offline
                                          C Offline
                                          Chuck333
                                          wrote on last edited by
                                          #20
                                          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 Extensions
                                          • Unsolved