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
Forum Updated to NodeBB v4.3 + New Features

Qtableview set background color to some columns

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 7 Posters 7.8k Views 3 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.
  • 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
    #21

    @VRonin Hi! I am trying to do something similar; however, I am building an application in python, not C++. Could you help me how to understand this code better so I can convert it to python? Or could you maybe show me how it would be in python?

    jsulmJ 1 Reply Last reply
    0
    • C Chuck333

      @VRonin Hi! I am trying to do something similar; however, I am building an application in python, not C++. Could you help me how to understand this code better so I can convert it to python? Or could you maybe show me how it would be in python?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #22

      @Chuck333 What is your exact problem/question?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      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