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. How to change the background of QTableView header?
QtWS25 Last Chance

How to change the background of QTableView header?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 4.1k 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.
  • D Offline
    D Offline
    deleted385
    wrote on 7 Nov 2021, 09:34 last edited by
    #1

    First, I've done following in the constructor of QHeaderView sublass:

    TableHeader::TableHeader(QTableView * parent) : QHeaderView(Qt::Horizontal, parent){
        ...
        auto p = palette();
        p.setColor(QPalette::Base, Qt::darkGray);
        setPalette(p);
        ...
    }
    

    this palette thing works for QPlainTextEdit, QTreeView background and all cells of QTableView. It didn't work for the header:

    x1.gif

    Second, added this in QHeaderView sublass:

    void TableHeader::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const{
        QVariant bg = model()->headerData(logicalIndex, Qt::Horizontal, Qt::BackgroundRole);
        painter->save();
        QHeaderView::paintSection(painter, rect, logicalIndex);
        painter->restore();
        if(bg.isValid()) painter->fillRect(rect, Qt::darkGray);
    }
    

    didn't work either. Finally, tried this:

    void TableWidget::queryDatabase(){
        tableModel->setTable(tableName);
        tableHeader->resetBoxes(tableModel->columnCount());
        tableModel->select();
        while (tableModel->canFetchMore()) tableModel->fetchMore();
        for (int i = 0; i < tableModel->columnCount(); i++)
            tableModel->setHeaderData(i, Qt::Horizontal, QBrush(Qt::darkGray), Qt::BackgroundRole);
    }
    

    also doesn't work.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on 7 Nov 2021, 09:50 last edited by
      #2

      Qt is not always consistent with itself :(

      For one TableView i'm doing a mix of stylesheet and palette settings:

      BankTableView::BankTableView(QWidget* parent) : QTableView(parent)
      {
           QString style = R"(
                           BankTableView { background-color: transparent; }
                           BankTableView QHeaderView { background-color: transparent;}
                           )";
      
          setStyleSheet(style);
      
          QPalette p = palette();
          p.setColor(QPalette::Highlight, QColor(255,100,30,150));
          p.setColor(QPalette::Disabled, QPalette::Highlight, QColor(200,200,200,65));
          p.setColor(QPalette::HighlightedText, QColor(50,50,50));
          setPalette(p);
      
      C D 2 Replies Last reply 7 Nov 2021, 10:45
      2
      • M mpergand
        7 Nov 2021, 09:50

        Qt is not always consistent with itself :(

        For one TableView i'm doing a mix of stylesheet and palette settings:

        BankTableView::BankTableView(QWidget* parent) : QTableView(parent)
        {
             QString style = R"(
                             BankTableView { background-color: transparent; }
                             BankTableView QHeaderView { background-color: transparent;}
                             )";
        
            setStyleSheet(style);
        
            QPalette p = palette();
            p.setColor(QPalette::Highlight, QColor(255,100,30,150));
            p.setColor(QPalette::Disabled, QPalette::Highlight, QColor(200,200,200,65));
            p.setColor(QPalette::HighlightedText, QColor(50,50,50));
            setPalette(p);
        
        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 7 Nov 2021, 10:45 last edited by
        #3

        @mpergand said in How to change the background of QTableView header?:

        Qt is not always consistent with itself :(

        It is - as stated in the docs the stylesheet always wins: "Note: If Qt Style Sheets are used on the same widget as functions that set the appearance of widgets, such as QWidget::setFont() or QTreeWidgetItem::setBackground(), style sheets will take precedence if the settings conflict."

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

        D M 2 Replies Last reply 7 Nov 2021, 10:52
        1
        • C Christian Ehrlicher
          7 Nov 2021, 10:45

          @mpergand said in How to change the background of QTableView header?:

          Qt is not always consistent with itself :(

          It is - as stated in the docs the stylesheet always wins: "Note: If Qt Style Sheets are used on the same widget as functions that set the appearance of widgets, such as QWidget::setFont() or QTreeWidgetItem::setBackground(), style sheets will take precedence if the settings conflict."

          D Offline
          D Offline
          deleted385
          wrote on 7 Nov 2021, 10:52 last edited by
          #4

          @Christian-Ehrlicher, is there a way to do that without stylesheet? using css in desktop app, in and of itself, is inconsistent, it's not electron.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 7 Nov 2021, 11:37 last edited by
            #5

            @Emon-Haque said in How to change the background of QTableView header?:

            is there a way to do that without stylesheet?

            No, not on e.g. windows as described in the link I gave you: "For this kind of customization, style sheets are much more powerful than QPalette. For example, it might be tempting to set the QPalette::Button role to red for a QPushButton to obtain a red push button. However, this wasn't guaranteed to work for all styles, because style authors are restricted by the different platforms' guidelines and (on Windows and macOS) by the native theme engine."

            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 7 Nov 2021, 13:02
            1
            • C Christian Ehrlicher
              7 Nov 2021, 10:45

              @mpergand said in How to change the background of QTableView header?:

              Qt is not always consistent with itself :(

              It is - as stated in the docs the stylesheet always wins: "Note: If Qt Style Sheets are used on the same widget as functions that set the appearance of widgets, such as QWidget::setFont() or QTreeWidgetItem::setBackground(), style sheets will take precedence if the settings conflict."

              M Offline
              M Offline
              mpergand
              wrote on 7 Nov 2021, 12:00 last edited by
              #6

              @Christian-Ehrlicher
              Style sheets should not exist in the first place.
              If you want to change the appearance of a object, create a subclass, that's basic OOP.

              I don't understand why Qt doesn't use native widgets, there are always some woes, on Mac it's ugly !
              Just to illustrate a few:

              • text not centered
              • no margins for the line edit
              • wrong selection color for the menu

              and I'm not talking about Linux ...

              1 Reply Last reply
              0
              • D Offline
                D Offline
                deleted385
                wrote on 7 Nov 2021, 12:10 last edited by deleted385 11 Jul 2021, 12:11
                #7

                Halfway through. If I replace the painSection with these:

                void TableHeader::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const{
                //    painter->fillRect(this->contentsRect(), Qt::transparent);
                //    painter->fillRect(this->contentsRect(), Qt::transparent);
                    painter->fillRect(rect, Qt::transparent);
                    painter->drawText(rect, Qt::AlignHCenter, model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString());
                }
                

                it looks like this:

                cap1.PNG

                on the right side there's still some unexpected white area. Tried to fill the entire rect() in paintEvent like this:

                void TableHeader::paintEvent(QPaintEvent *event){
                    QPainter painter(this);
                    painter.fillRect(rect(), Qt::transparent);
                }
                

                it does fill the entire area including the header text and I get these in Qt Creator output:

                QWidget::paintEngine: Should no longer be called
                QPainter::begin: Paint device returned engine == 0, type: 1
                
                1 Reply Last reply
                0
                • C Christian Ehrlicher
                  7 Nov 2021, 11:37

                  @Emon-Haque said in How to change the background of QTableView header?:

                  is there a way to do that without stylesheet?

                  No, not on e.g. windows as described in the link I gave you: "For this kind of customization, style sheets are much more powerful than QPalette. For example, it might be tempting to set the QPalette::Button role to red for a QPushButton to obtain a red push button. However, this wasn't guaranteed to work for all styles, because style authors are restricted by the different platforms' guidelines and (on Windows and macOS) by the native theme engine."

                  D Offline
                  D Offline
                  deleted385
                  wrote on 7 Nov 2021, 13:02 last edited by deleted385 11 Jul 2021, 13:04
                  #8

                  @Christian-Ehrlicher, first of all it's not to bash Qt, I use a few Qt apps everyday and the small app through which I've started to explore Qt Widgets, which I post as screenshots/animations, in most of my posts has a WPF counterpart and sometime I compare the performance and features. Here's a few contrast:

                  1. I can zoom in a Datagrid with a 100k rows in WPF BUT I can't in QTableView. @SGaist, suggested for paging BUT I'd say improve UI/Data virtualization.

                  2. in WPF, I can write app without a single line of XAML, that's my preferred way and that's easier, Last time I checked QML, I couldn't even subclass a Rectangle for customization.

                  3. to some extent css can be powerful BUT obviously not for moderate/complex UI like this, never mind my screenshots:

                  cap2.png

                  that's the dashboard of an app I've written for myself and may be some other people also use that as I've shared the code/app in github. There's some fairly complex animation involved in that app and every control has been customized to make my life easier and there isn't any third party library other than WPF framework in that app.

                  I wish all the best for Qt and, hopefully, I'll keep exploring more of Qt.

                  JonBJ 1 Reply Last reply 7 Nov 2021, 13:28
                  0
                  • D deleted385
                    7 Nov 2021, 13:02

                    @Christian-Ehrlicher, first of all it's not to bash Qt, I use a few Qt apps everyday and the small app through which I've started to explore Qt Widgets, which I post as screenshots/animations, in most of my posts has a WPF counterpart and sometime I compare the performance and features. Here's a few contrast:

                    1. I can zoom in a Datagrid with a 100k rows in WPF BUT I can't in QTableView. @SGaist, suggested for paging BUT I'd say improve UI/Data virtualization.

                    2. in WPF, I can write app without a single line of XAML, that's my preferred way and that's easier, Last time I checked QML, I couldn't even subclass a Rectangle for customization.

                    3. to some extent css can be powerful BUT obviously not for moderate/complex UI like this, never mind my screenshots:

                    cap2.png

                    that's the dashboard of an app I've written for myself and may be some other people also use that as I've shared the code/app in github. There's some fairly complex animation involved in that app and every control has been customized to make my life easier and there isn't any third party library other than WPF framework in that app.

                    I wish all the best for Qt and, hopefully, I'll keep exploring more of Qt.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 7 Nov 2021, 13:28 last edited by
                    #9

                    @Emon-Haque
                    Yes, WPF has more powerful, out-of-the-box features for Windows Presentation.

                    OTOH, it doesn't work across platforms, nor for that matter does it provide, say, networking or bluetooth code like Qt does.

                    They are very different.

                    So you can either work within what Qt does provide, or go for a dedicated Windows-platform with "fancier" features, as you please.

                    D 1 Reply Last reply 7 Nov 2021, 13:33
                    0
                    • JonBJ JonB
                      7 Nov 2021, 13:28

                      @Emon-Haque
                      Yes, WPF has more powerful, out-of-the-box features for Windows Presentation.

                      OTOH, it doesn't work across platforms, nor for that matter does it provide, say, networking or bluetooth code like Qt does.

                      They are very different.

                      So you can either work within what Qt does provide, or go for a dedicated Windows-platform with "fancier" features, as you please.

                      D Offline
                      D Offline
                      deleted385
                      wrote on 7 Nov 2021, 13:33 last edited by deleted385 11 Jul 2021, 13:33
                      #10

                      @JonB, it's networking feature for sure! I've written a basic stock exchange in WPF (both server and client) and at that time I could make 120+ transaction per second AND in client side I've real time price/volume chart.

                      JonBJ 1 Reply Last reply 7 Nov 2021, 13:38
                      0
                      • D deleted385
                        7 Nov 2021, 13:33

                        @JonB, it's networking feature for sure! I've written a basic stock exchange in WPF (both server and client) and at that time I could make 120+ transaction per second AND in client side I've real time price/volume chart.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 7 Nov 2021, 13:38 last edited by JonB 11 Jul 2021, 13:43
                        #11

                        @Emon-Haque said in How to change the background of QTableView header?:

                        it's networking feature for sure!

                        I'm not sure what you are saying here. WPF is for UI presentation. All the other stuff you are talking about is from .NET. Which is a totally different topic. If you're now talking about .NET facilities compared to Qt that is a whole different matter. I merely picked some random aspect of Qt I could think of which is not UI. I could have mentioned something else, like QTextToSpeech or QPdfWriter or QXmlReader or whatever. Qt offers a lot more than just UI stuff.

                        D 1 Reply Last reply 7 Nov 2021, 13:39
                        0
                        • JonBJ JonB
                          7 Nov 2021, 13:38

                          @Emon-Haque said in How to change the background of QTableView header?:

                          it's networking feature for sure!

                          I'm not sure what you are saying here. WPF is for UI presentation. All the other stuff you are talking about is from .NET. Which is a totally different topic. If you're now talking about .NET facilities compared to Qt that is a whole different matter. I merely picked some random aspect of Qt I could think of which is not UI. I could have mentioned something else, like QTextToSpeech or QPdfWriter or QXmlReader or whatever. Qt offers a lot more than just UI stuff.

                          D Offline
                          D Offline
                          deleted385
                          wrote on 7 Nov 2021, 13:39 last edited by
                          #12

                          @JonB, grow up!

                          when you talk about winform/wpf, needless to say that it includes .net

                          JonBJ 1 Reply Last reply 7 Nov 2021, 13:46
                          0
                          • D deleted385
                            7 Nov 2021, 13:39

                            @JonB, grow up!

                            when you talk about winform/wpf, needless to say that it includes .net

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 7 Nov 2021, 13:46 last edited by JonB 11 Jul 2021, 14:05
                            #13

                            @Emon-Haque
                            LOL. So you don't mean just WPF. You mean compared against .NET as well? Perhaps you mean that .NET includes Windows as well, so that's all part of WPF, and Qt fails to provide its own OS?

                            I just don't get what point you are trying to make. Yes WPF has fancier stuff than Qt, I said that. And btw, what do you estimate the user base for .NET/WPF is compared to Qt, or the revenue of MS vs TQtC?

                            This is a user forum for Qt, so we work with what we get. We use Qt, not develop it. What would you like us to do about the various features you say (correctly) that WPF has which are not present in Qt?

                            D 1 Reply Last reply 7 Nov 2021, 14:38
                            0
                            • M mpergand
                              7 Nov 2021, 09:50

                              Qt is not always consistent with itself :(

                              For one TableView i'm doing a mix of stylesheet and palette settings:

                              BankTableView::BankTableView(QWidget* parent) : QTableView(parent)
                              {
                                   QString style = R"(
                                                   BankTableView { background-color: transparent; }
                                                   BankTableView QHeaderView { background-color: transparent;}
                                                   )";
                              
                                  setStyleSheet(style);
                              
                                  QPalette p = palette();
                                  p.setColor(QPalette::Highlight, QColor(255,100,30,150));
                                  p.setColor(QPalette::Disabled, QPalette::Highlight, QColor(200,200,200,65));
                                  p.setColor(QPalette::HighlightedText, QColor(50,50,50));
                                  setPalette(p);
                              
                              D Offline
                              D Offline
                              deleted385
                              wrote on 7 Nov 2021, 14:28 last edited by deleted385 11 Jul 2021, 14:28
                              #14

                              @mpergand, for the horizonalheader, Here's a way. First in the constructor of QHeaderView subclass add setStretchLastSection(true); and in the paintSection have only those two lines posted above. It'll look like this:

                              cap3.PNG

                              1 Reply Last reply
                              0
                              • JonBJ JonB
                                7 Nov 2021, 13:46

                                @Emon-Haque
                                LOL. So you don't mean just WPF. You mean compared against .NET as well? Perhaps you mean that .NET includes Windows as well, so that's all part of WPF, and Qt fails to provide its own OS?

                                I just don't get what point you are trying to make. Yes WPF has fancier stuff than Qt, I said that. And btw, what do you estimate the user base for .NET/WPF is compared to Qt, or the revenue of MS vs TQtC?

                                This is a user forum for Qt, so we work with what we get. We use Qt, not develop it. What would you like us to do about the various features you say (correctly) that WPF has which are not present in Qt?

                                D Offline
                                D Offline
                                deleted385
                                wrote on 7 Nov 2021, 14:38 last edited by deleted385 11 Jul 2021, 14:41
                                #15

                                @JonB, I believe QML team respects WPF masterminds a lot. I read, a long time ago, that QML was inspired by WPF XAML tech. They've one additional and important thing, you don't need XAML to write cool WPF apps. Hope that, someday, Qt users will also have that cool feature.

                                We can get into war on the pros and cons of different techs BUT that's not the way! Copy good things of others. C# teams haven't done anything wrong by copying Java and none, other than trolls, blame or humiliate them for that. Now, Java is far behind C# in terms of features.

                                Respect masterminds and keep contributing.

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mpergand
                                  wrote on 7 Nov 2021, 15:00 last edited by mpergand 11 Jul 2021, 15:01
                                  #16

                                  @Emon-Haque

                                  With subclasses you can do customizations easily;
                                  I'm using QPainterPath, QGradient a lot and it works well.

                                  alt text

                                  IMHO style sheets look more like a hack than anything else.

                                  D 1 Reply Last reply 7 Nov 2021, 16:21
                                  2
                                  • M mpergand
                                    7 Nov 2021, 15:00

                                    @Emon-Haque

                                    With subclasses you can do customizations easily;
                                    I'm using QPainterPath, QGradient a lot and it works well.

                                    alt text

                                    IMHO style sheets look more like a hack than anything else.

                                    D Offline
                                    D Offline
                                    deleted385
                                    wrote on 7 Nov 2021, 16:21 last edited by
                                    #17

                                    @mpergand, yes subclassing is the way to go in my opinion too. So far Qt Widgets isn't that bad as I though it'd be. It's fast and looks like it doesn't use GPU at all.

                                    1 Reply Last reply
                                    0
                                    • M mpergand referenced this topic on 18 Jun 2023, 18:55

                                    1/17

                                    7 Nov 2021, 09:34

                                    • Login

                                    • Login or register to search.
                                    1 out of 17
                                    • First post
                                      1/17
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved