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. Failed to change background color of table widget by QItemDelegate derived class
Qt 6.11 is out! See what's new in the release blog

Failed to change background color of table widget by QItemDelegate derived class

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 3.2k 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.
  • jronaldJ Offline
    jronaldJ Offline
    jronald
    wrote on last edited by jronald
    #1
    void MyTableItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QStyleOptionViewItem myOption = option;
        // not work
        myOption.palette.setColor(QPalette::All, QPalette::Background, Qt::red);
        drawBackground(painter, myOption, index);
        // works
        myOption.palette.setColor(QPalette::All, QPalette::Text, Qt::red);
        drawDisplay(painter, myOption, myOption.rect, "Hello");
    }
    

    The text color can be changed, but the background color can't be changed.
    How to change the background color? (Better in the paint function.)
    Thanks.

    1 Reply Last reply
    0
    • jronaldJ Offline
      jronaldJ Offline
      jronald
      wrote on last edited by
      #2

      QPainter ::fillRect get it done, still confused why QItemDelegate ::drawBackground not work as expected.

      EddyE 1 Reply Last reply
      0
      • jronaldJ jronald

        QPainter ::fillRect get it done, still confused why QItemDelegate ::drawBackground not work as expected.

        EddyE Offline
        EddyE Offline
        Eddy
        wrote on last edited by Eddy
        #3

        @jronald

        instead of QPalette::All which doesn't exist in enum QPalette::ColorGroup
        you can choose between :

        QPalette::Disabled		 
        QPalette::Active		 
        QPalette::Inactive		 
        QPalette::Normal
        

        what errors did you get?

        Qt Certified Specialist
        www.edalsolutions.be

        jronaldJ 1 Reply Last reply
        0
        • EddyE Eddy

          @jronald

          instead of QPalette::All which doesn't exist in enum QPalette::ColorGroup
          you can choose between :

          QPalette::Disabled		 
          QPalette::Active		 
          QPalette::Inactive		 
          QPalette::Normal
          

          what errors did you get?

          jronaldJ Offline
          jronaldJ Offline
          jronald
          wrote on last edited by
          #4

          @Eddy Already tried, no error is reported, just not work.

          1 Reply Last reply
          0
          • EddyE Offline
            EddyE Offline
            Eddy
            wrote on last edited by
            #5

            I see in your code you use
            QItemDelegate
            and
            QStyleOptionViewItem

            AFAIR they don't mix well.

            Why are you not using QStyledItemDelegate?

            Qt Certified Specialist
            www.edalsolutions.be

            jronaldJ 1 Reply Last reply
            2
            • EddyE Eddy

              I see in your code you use
              QItemDelegate
              and
              QStyleOptionViewItem

              AFAIR they don't mix well.

              Why are you not using QStyledItemDelegate?

              jronaldJ Offline
              jronaldJ Offline
              jronald
              wrote on last edited by jronald
              #6

              @Eddy QStyledItemDelegate doesn't have a method of drawBackground

              1 Reply Last reply
              0
              • EddyE Offline
                EddyE Offline
                Eddy
                wrote on last edited by
                #7

                @jronald
                QStyledItemDelegate uses Qt::BackgroundRole

                link to docs

                Qt Certified Specialist
                www.edalsolutions.be

                jronaldJ 1 Reply Last reply
                1
                • EddyE Eddy

                  @jronald
                  QStyledItemDelegate uses Qt::BackgroundRole

                  link to docs

                  jronaldJ Offline
                  jronaldJ Offline
                  jronald
                  wrote on last edited by
                  #8

                  @Eddy Just don't know why QItemDelegate::drawBackground not work

                  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