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. Delegate the QtableView colors
Qt 6.11 is out! See what's new in the release blog

Delegate the QtableView colors

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.1k 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.
  • KaguroK Offline
    KaguroK Offline
    Kaguro
    wrote on last edited by Kaguro
    #1

    Hi Guys!
    I have a QtableView with QSqlTableModel model. I would like to achive different color rows and the default celector's frame disappear. I found on the internet some solution for these problems, but i cant use these solutions in the same time.
    This is for different row colors:

    class ColorDelegate: public QItemDelegate
    {
    public:
        QColor color;
        ColorDelegate(QColor colo,QObject *parent = 0) : QItemDelegate(parent) {color = colo;}
    
    public:
        virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
            drawBackground(painter, option, index);
            QItemDelegate::paint(painter, option, index);
        }
    
    protected:
        virtual void drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
            Q_UNUSED(index);
            painter->fillRect(option.rect, color);
        }
    };
    

    and usage:

    for (int i = 0;i<model->rowCount();i++) 
    {
        if(i%2==0)
            view->setItemDelegateForRow(i,new ColorDelegate( QColor(216, 216, 216),view));
        else
            view->setItemDelegateForRow(i,new ColorDelegate( QColor(238, 236, 238),view));
    }
    

    And this is for default non default select (i want to keep the keyboard navigation, so this solution was the good for it)

    class NoFocusDelegate : public QStyledItemDelegate
    {
    protected:
        void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
    };
    
    void NoFocusDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
    {
        QStyleOptionViewItem itemOption(option);
        if (itemOption.state & QStyle::State_HasFocus)
            itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
        QStyledItemDelegate::paint(painter, itemOption, index);
    };
    

    and usage:

    view->setItemDelegate(new NoFocusDelegate());
    

    Any ideas to to achieve both? Thanks! (And sorry for my bad english again!)

    1 Reply Last reply
    0
    • KaguroK Kaguro

      @mrjj If i use it this way like your suggestion:

      for (int i = 0;i<model->rowCount();i++) 
      {
          if(i%2==0)
              view->setItemDelegateForRow(i,new ColorDelegate( QColor(216, 216, 216),view));
          else
              view->setItemDelegateForRow(i,new ColorDelegate( QColor(238, 236, 238),view));
      }
      

      the rows repaint is working but the default select frame is not disapear :(
      2020-11-15_19h04_21.png

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @Kaguro
      ah my bad. we used the wrong option here
      QStyledItemDelegate::paint(painter, option, index);
      should be
      QStyledItemDelegate::paint(painter, itemOption, index);

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

        Hi
        Should not be an issue to combine to one.
        QStyledItemDelegate is the replacement for QItemDelegate
        So you can just do merge the 2 delegates to one.

        KaguroK 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          Should not be an issue to combine to one.
          QStyledItemDelegate is the replacement for QItemDelegate
          So you can just do merge the 2 delegates to one.

          KaguroK Offline
          KaguroK Offline
          Kaguro
          wrote on last edited by
          #3

          @mrjj hmm, and how can i merge to one? Can i get an example? Cuse when i tried it i did something wrong :(

          mrjjM 1 Reply Last reply
          0
          • KaguroK Kaguro

            @mrjj hmm, and how can i merge to one? Can i get an example? Cuse when i tried it i did something wrong :(

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Kaguro
            Hi
            Like

            class ColorDelegate: public QStyledItemDelegate
            {
            public:
                QColor color;
                ColorDelegate(QColor colo, QObject *parent = 0) : QStyledItemDelegate(parent)
                {
                    color = colo;
                }
            
            public:
                virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
                                   const QModelIndex &index) const
                {
                    drawBackground(painter, option, index);
                    QStyleOptionViewItem itemOption(option);
                    if (itemOption.state & QStyle::State_HasFocus)
                        itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
                    QStyledItemDelegate::paint(painter, option, index);
                }
            
            protected:
                virtual void drawBackground(QPainter *painter, const QStyleOptionViewItem &option,
                                            const QModelIndex &index) const
                {
                    Q_UNUSED(index);
                    painter->fillRect(option.rect, color);
                }
            };
            
            KaguroK 1 Reply Last reply
            1
            • mrjjM mrjj

              @Kaguro
              Hi
              Like

              class ColorDelegate: public QStyledItemDelegate
              {
              public:
                  QColor color;
                  ColorDelegate(QColor colo, QObject *parent = 0) : QStyledItemDelegate(parent)
                  {
                      color = colo;
                  }
              
              public:
                  virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
                                     const QModelIndex &index) const
                  {
                      drawBackground(painter, option, index);
                      QStyleOptionViewItem itemOption(option);
                      if (itemOption.state & QStyle::State_HasFocus)
                          itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
                      QStyledItemDelegate::paint(painter, option, index);
                  }
              
              protected:
                  virtual void drawBackground(QPainter *painter, const QStyleOptionViewItem &option,
                                              const QModelIndex &index) const
                  {
                      Q_UNUSED(index);
                      painter->fillRect(option.rect, color);
                  }
              };
              
              KaguroK Offline
              KaguroK Offline
              Kaguro
              wrote on last edited by
              #5

              @mrjj If i use it this way like your suggestion:

              for (int i = 0;i<model->rowCount();i++) 
              {
                  if(i%2==0)
                      view->setItemDelegateForRow(i,new ColorDelegate( QColor(216, 216, 216),view));
                  else
                      view->setItemDelegateForRow(i,new ColorDelegate( QColor(238, 236, 238),view));
              }
              

              the rows repaint is working but the default select frame is not disapear :(
              2020-11-15_19h04_21.png

              mrjjM 1 Reply Last reply
              0
              • KaguroK Kaguro

                @mrjj If i use it this way like your suggestion:

                for (int i = 0;i<model->rowCount();i++) 
                {
                    if(i%2==0)
                        view->setItemDelegateForRow(i,new ColorDelegate( QColor(216, 216, 216),view));
                    else
                        view->setItemDelegateForRow(i,new ColorDelegate( QColor(238, 236, 238),view));
                }
                

                the rows repaint is working but the default select frame is not disapear :(
                2020-11-15_19h04_21.png

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @Kaguro
                ah my bad. we used the wrong option here
                QStyledItemDelegate::paint(painter, option, index);
                should be
                QStyledItemDelegate::paint(painter, itemOption, index);

                KaguroK 1 Reply Last reply
                0
                • mrjjM mrjj

                  @Kaguro
                  ah my bad. we used the wrong option here
                  QStyledItemDelegate::paint(painter, option, index);
                  should be
                  QStyledItemDelegate::paint(painter, itemOption, index);

                  KaguroK Offline
                  KaguroK Offline
                  Kaguro
                  wrote on last edited by
                  #7

                  @mrjj and it works! You are my hero! :D

                  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