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. formatting a QTableView header
Forum Updated to NodeBB v4.3 + New Features

formatting a QTableView header

Scheduled Pinned Locked Moved Unsolved General and Desktop
41 Posts 5 Posters 30.2k Views 4 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 7 Nov 2018, 20:56 last edited by
    #31

    I can try that. What I was asking for, though, was a style hint that would be readily apparent in my app. The one in the example might work perfectly, but I'd never see it without implementing any shortcuts (at least I think not).

    K 1 Reply Last reply 8 Nov 2018, 07:33
    0
    • M Offline
      M Offline
      mzimmers
      wrote on 7 Nov 2018, 23:32 last edited by
      #32

      For what it's worth, this line of code produces expected results:

      ui->tableView->setStyleSheet("background-color: red");
      

      But this one does not:

          ui->tableView->horizontalHeader()->setStyleSheet("background-color: red");
      

      So the problem seems to be in the header object, doesn't it?

      1 Reply Last reply
      0
      • M mzimmers
        7 Nov 2018, 20:56

        I can try that. What I was asking for, though, was a style hint that would be readily apparent in my app. The one in the example might work perfectly, but I'd never see it without implementing any shortcuts (at least I think not).

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 8 Nov 2018, 07:33 last edited by kshegunov 11 Aug 2018, 07:34
        #33

        You don't need a style hint if you're trying to override the painting. You'd have to reimplement QStyle::drawControl and handle the QStyle::CE_Header control. Something like this:

        class MyProxyStyle : public QProxyStyle
        {
        public:
            void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override
            {
                if (element != QStyle::CE_Header)  {
                     baseStyle()->drawControl(element, option, painter, widget);
                     return;
                }
        
                // Paint here ...
            }
        };
        

        It's been a long time since I last played with the styles, but I hope this is of help.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • V Offline
          V Offline
          VRonin
          wrote on 8 Nov 2018, 08:33 last edited by VRonin 11 Aug 2018, 15:13
          #34

          You might want to try just adding QApplication::setStyle(QStyleFactory::create("Fusion")); to your main(). The fusion style supports the styling of QHeaderViews even on windows

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          M 1 Reply Last reply 8 Nov 2018, 15:24
          3
          • V VRonin
            8 Nov 2018, 08:33

            You might want to try just adding QApplication::setStyle(QStyleFactory::create("Fusion")); to your main(). The fusion style supports the styling of QHeaderViews even on windows

            M Offline
            M Offline
            mzimmers
            wrote on 8 Nov 2018, 15:24 last edited by
            #35

            @VRonin using that style, along with the style sheet, did in fact paint my header background red. Thank you!

            Now, for creating a separation line under the header...is it recommended that I try to use a style sheet, or QStyles?

            1 Reply Last reply
            0
            • V Offline
              V Offline
              VRonin
              wrote on 8 Nov 2018, 16:13 last edited by
              #36

              Try the fast way, use stylesheet

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • M Offline
                M Offline
                mzimmers
                wrote on 8 Nov 2018, 17:55 last edited by
                #37

                Interesting results...I tried this:

                    ui->tableView->horizontalHeader()->setStyleSheet("border-bottom: 5px solid red");
                

                And got this:
                0_1541699574292_header.PNG

                I must be malforming the stylesheet (I pulled the contents directly from a CSS example I found online), but the curious (to me) part is I'm now getting an underline (actually a bottom border, I guess) across the entire header, but only the middle column is painted red (which I don't understand either).

                Thoughts?

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  VRonin
                  wrote on 8 Nov 2018, 18:05 last edited by VRonin 11 Aug 2018, 19:15
                  #38

                  http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qheaderview

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    mzimmers
                    wrote on 8 Nov 2018, 18:16 last edited by
                    #39

                    Disregard my image above: the setting of the red background was due to a separate experiment I'd been performing on the model. It was disguised when I set the entire header view to red, then reappeared when I removed that CSS in favor of the bottom border.

                    I'd actually read that page you referenced, but I don't fully understand the syntax. I can see that everything in the braces is Qt style information, but what is the context in which they're used? For example, in this:

                    QHeaderView::section
                    

                    What is "section?"

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 8 Nov 2018, 19:17 last edited by
                      #40

                      it's the sub element inside the headerview. That style applies to every single row/column header cell

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      1
                      • M Offline
                        M Offline
                        mzimmers
                        wrote on 9 Nov 2018, 15:59 last edited by
                        #41

                        Ah, I see (I think). Well, the upshot is that I can remove all of my stylesheet settings. The use of the Fusion style gave me the desired look:

                        0_1541779015718_wifiui.PNG

                        Which is fortunate, because this attempt at stylesheet setting didn't work:

                            ui->tableView->horizontalHeader()->setStyleSheet("border-bottom: 5px solid red");
                        

                        Not sure why (the red background worked).

                        1 Reply Last reply
                        1

                        40/41

                        8 Nov 2018, 19:17

                        • Login

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