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 29.3k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #21

    @mzimmers: are you working on windows? https://bugreports.qt.io/browse/QTBUG-31804

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

    mzimmersM 1 Reply Last reply
    3
    • Christian EhrlicherC Christian Ehrlicher

      @mzimmers: are you working on windows? https://bugreports.qt.io/browse/QTBUG-31804

      mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #22

      @Christian-Ehrlicher yes, I am, and that would seem to be the problem here. Plus, there's this:

      Widget::Widget(DeviceModel *d, QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
      {
      ...
          ui->tableView->horizontalHeader()->setBackgroundRole(QPalette::Window);
      

      So, do I need to create a QStyle for the underlining, and set that on my header view?

      kshegunovK 1 Reply Last reply
      0
      • mzimmersM mzimmers

        @Christian-Ehrlicher yes, I am, and that would seem to be the problem here. Plus, there's this:

        Widget::Widget(DeviceModel *d, QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
        {
        ...
            ui->tableView->horizontalHeader()->setBackgroundRole(QPalette::Window);
        

        So, do I need to create a QStyle for the underlining, and set that on my header view?

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #23

        @mzimmers said in formatting a QTableView header:

        So, do I need to create a QStyle for the underlining, and set that on my header view?

        I'd advise at least trying if that will fix the issue.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #24

          Do I need to derive a subclass from QStyle to do this?

          kshegunovK 1 Reply Last reply
          0
          • mzimmersM mzimmers

            Do I need to derive a subclass from QStyle to do this?

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #25

            @mzimmers said in formatting a QTableView header:

            Do I need to derive a subclass from QStyle to do this?

            Yes. But maybe try QProxyStyle instead, so you have less work to do.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #26

              I'm afraid that I'm going to have to temporarily abandon this thread. I just don't know enough about styles/hints/etc. and I don't have time to give it the attention it deserves.

              Thanks for the assistance; I'll get back to this as soon as I can.

              1 Reply Last reply
              0
              • mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #27

                OK, I have a little spare time now. I've copied this code (from the page):

                class MyProxyStyle : public QProxyStyle
                {
                  public:
                    int styleHint(StyleHint hint, const QStyleOption *option = 0,
                                  const QWidget *widget = 0, QStyleHintReturn *returnData = 0) const override
                    {
                        if (hint == QStyle::SH_UnderlineShortcut)
                            return 0;
                        return QProxyStyle::styleHint(hint, option, widget, returnData);
                    }
                };
                

                What would you suggest I use instead of SH_UnderlineShortcut to test whether my styling is taking effect? (Given the trouble we were having earlier.)

                Thanks.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #28

                  Hi,

                  How did you set your custom style ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #29

                    Hi SGaist - like this:

                        MyProxyStyle *mps = new MyProxyStyle;
                        ui->tableView->setStyle(mps);
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #30

                      What if you set it application wide ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • mzimmersM Offline
                        mzimmersM Offline
                        mzimmers
                        wrote on 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).

                        kshegunovK 1 Reply Last reply
                        0
                        • mzimmersM Offline
                          mzimmersM Offline
                          mzimmers
                          wrote on 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
                          • mzimmersM mzimmers

                            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).

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by kshegunov
                            #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
                            • VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by VRonin
                              #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

                              mzimmersM 1 Reply Last reply
                              3
                              • VRoninV VRonin

                                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

                                mzimmersM Offline
                                mzimmersM Offline
                                mzimmers
                                wrote on 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
                                • VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on 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
                                  • mzimmersM Offline
                                    mzimmersM Offline
                                    mzimmers
                                    wrote on 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
                                    • VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by VRonin
                                      #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
                                      • mzimmersM Offline
                                        mzimmersM Offline
                                        mzimmers
                                        wrote on 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
                                        • VRoninV Offline
                                          VRoninV Offline
                                          VRonin
                                          wrote on 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

                                          • Login

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