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. QTableWidget horizontal QHeader background color don't change
Forum Updated to NodeBB v4.3 + New Features

QTableWidget horizontal QHeader background color don't change

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.2k Views 2 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.
  • D Offline
    D Offline
    dwatanabe
    wrote on last edited by
    #1

    When creating a new QTableWidget and changing the QPalette to the following QPalette:

    fdb1e264-3af9-4517-8bf0-5d0be6e9884d-image.png

    The result is that the horizontal background of the QHeader don't change the color:

    66d3f25c-f1b2-4c1b-9237-dab74ee58700-image.png

    ps.: To change the color of each Item of the header, it is possible to set each individual QTableWidgetItem. However, the background on the right of the horizontal header remains white.

    I don't want to use stylesheet, however, with the following stylesheet code:

    background-color:black
    

    The entire TableWidget turns black:

    a58327de-2270-4d04-b21b-3c162cf4da10-image.png

    How can QPalette paint the horizontal header background as it is done with the vertical header? With a pink color on the example.

    ps.: It was not possible to setPalette on the horizontalHeader()

    M 1 Reply Last reply
    0
    • D dwatanabe

      When creating a new QTableWidget and changing the QPalette to the following QPalette:

      fdb1e264-3af9-4517-8bf0-5d0be6e9884d-image.png

      The result is that the horizontal background of the QHeader don't change the color:

      66d3f25c-f1b2-4c1b-9237-dab74ee58700-image.png

      ps.: To change the color of each Item of the header, it is possible to set each individual QTableWidgetItem. However, the background on the right of the horizontal header remains white.

      I don't want to use stylesheet, however, with the following stylesheet code:

      background-color:black
      

      The entire TableWidget turns black:

      a58327de-2270-4d04-b21b-3c162cf4da10-image.png

      How can QPalette paint the horizontal header background as it is done with the vertical header? With a pink color on the example.

      ps.: It was not possible to setPalette on the horizontalHeader()

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @dwatanabe
      Try this:

      QPalette pal = tableWidget->palette();
       pal.setColor(QPalette::Window, Qt::black);
       tableWidget->setPalette(pal);
      

      [EDIT] it works with horizontal header as well:

      auto hd=tableWidget->horizontalHeader();
      pal = hd->palette();
      pal.setColor(QPalette::Window, Qt::magenta);
      hd->setPalette(pal);
      
      D 1 Reply Last reply
      2
      • M mpergand

        @dwatanabe
        Try this:

        QPalette pal = tableWidget->palette();
         pal.setColor(QPalette::Window, Qt::black);
         tableWidget->setPalette(pal);
        

        [EDIT] it works with horizontal header as well:

        auto hd=tableWidget->horizontalHeader();
        pal = hd->palette();
        pal.setColor(QPalette::Window, Qt::magenta);
        hd->setPalette(pal);
        
        D Offline
        D Offline
        dwatanabe
        wrote on last edited by dwatanabe
        #3

        @mpergand

        I tried it before, but isn't working, I am using Qt 6.4.2

            QPalette qPal = ui->tableWidget->palette();
            qPal.setColor(QPalette::Base, Qt::black);
            // Header Background
            qPal.setColor(QPalette::Button, Qt::black);
            // Outline
            qPal.setColor(QPalette::Window, Qt::black);
            // Header Text
            qPal.setColor(QPalette::ButtonText, Qt::black);
            // Body Text
            qPal.setColor(QPalette::Text, Qt::black);
            // Highlight
            qPal.setColor(QPalette::Highlight, Qt::black);
            // Highlight Text
            qPal.setColor(QPalette::HighlightedText, Qt::black);
            //3D Bevel
            qPal.setColor(QPalette::Dark, Qt::black);
            qPal.setColor(QPalette::Light, Qt::black);
        
            ui->tableWidget->horizontalHeader()->setPalette(qPal);
        

        Result for ui->tableWidget->horizontalHeader()->setPalette(qPal);:

        f003e990-9e42-4252-86a3-6c857e204f47-image.png

        Result for ui->tableWidget->horizontalHeader()->setPalette(qPal);

        b9f3a80c-6a92-4085-b229-798cc3e01fae-image.png

        M 1 Reply Last reply
        0
        • D dwatanabe

          @mpergand

          I tried it before, but isn't working, I am using Qt 6.4.2

              QPalette qPal = ui->tableWidget->palette();
              qPal.setColor(QPalette::Base, Qt::black);
              // Header Background
              qPal.setColor(QPalette::Button, Qt::black);
              // Outline
              qPal.setColor(QPalette::Window, Qt::black);
              // Header Text
              qPal.setColor(QPalette::ButtonText, Qt::black);
              // Body Text
              qPal.setColor(QPalette::Text, Qt::black);
              // Highlight
              qPal.setColor(QPalette::Highlight, Qt::black);
              // Highlight Text
              qPal.setColor(QPalette::HighlightedText, Qt::black);
              //3D Bevel
              qPal.setColor(QPalette::Dark, Qt::black);
              qPal.setColor(QPalette::Light, Qt::black);
          
              ui->tableWidget->horizontalHeader()->setPalette(qPal);
          

          Result for ui->tableWidget->horizontalHeader()->setPalette(qPal);:

          f003e990-9e42-4252-86a3-6c857e204f47-image.png

          Result for ui->tableWidget->horizontalHeader()->setPalette(qPal);

          b9f3a80c-6a92-4085-b229-798cc3e01fae-image.png

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @dwatanabe
          Maybe a bug in Qt 6 ?
          For me with Qt 5.12, with your code the header is all black (text, background, etc)

          Christian EhrlicherC 1 Reply Last reply
          0
          • M mpergand

            @dwatanabe
            Maybe a bug in Qt 6 ?
            For me with Qt 5.12, with your code the header is all black (text, background, etc)

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @mpergand said in QTableWidget horizontal QHeader background color don't change:

            For me with Qt 5.12, with your code the header is all black (text, background, etc)

            Also on windows?
            See QPalette documentation: "Warning: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows Vista and the macOS styles."

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            D 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              @mpergand said in QTableWidget horizontal QHeader background color don't change:

              For me with Qt 5.12, with your code the header is all black (text, background, etc)

              Also on windows?
              See QPalette documentation: "Warning: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows Vista and the macOS styles."

              D Offline
              D Offline
              dwatanabe
              wrote on last edited by
              #6

              @Christian-Ehrlicher Yes, you are right, it only breaks on Windows.

              I tested on a Remote Linux Device and it works just for the horizontal header:

              ad960eb8-0a53-4750-b772-2aaa4c423f83-image.png

              Or on the entire QTableWidget:

              27e7beba-2b30-4a36-b6d2-d9e26c76b382-image.png

              Maybe I have to reimplement the QPainter on Windows?

              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