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

How to change the background of QTableView header?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 4.3k 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
    deleted385
    wrote on last edited by deleted385
    #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
    • Christian EhrlicherC Christian Ehrlicher

      @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 last edited by deleted385
      #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
      0
      • D deleted385

        @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 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
        0
        • JonBJ JonB

          @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 last edited by deleted385
          #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
          0
          • D deleted385

            @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 last edited by JonB
            #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
            0
            • JonBJ JonB

              @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 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
              0
              • D deleted385

                @JonB, grow up!

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #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
                0
                • M mpergand

                  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 last edited by deleted385
                  #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

                    @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 last edited by deleted385
                    #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 last edited by mpergand
                      #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
                      2
                      • M mpergand

                        @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 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

                        • Login

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