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 31.0k 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 mzimmers
    3 Nov 2018, 15:43

    My model is defined like this:

        QAbstractItemModel *m_model;    // the main model
    

    But it's initialized like this:

    DeviceModel::DeviceModel(QObject *parent) : QObject(parent), m_model(new QStandardItemModel(this))
    {
        QString qs;
        QStringList qsl;
    
        m_model->insertColumns(0, TAG_NBRTAGS);
        m_model->setHeaderData(TAG_SERIALNUMBER, Qt::Horizontal, tr("Serial Number"), Qt::DisplayRole);
        m_model->setHeaderData(TAG_MACADDRESS, Qt::Horizontal, tr("MAC Address"), Qt::DisplayRole);
        m_model->setHeaderData(TAG_DEVICENAME, Qt::Horizontal, tr("Device Name"), Qt::DisplayRole);
        m_model->setHeaderData(TAG_SERIALNUMBER, Qt::Horizontal, QBrush(Qt::red), Qt::BackgroundRole);
    

    Note the use of the QStandardItemModel in the c'tor. (I think VRonin gave me this code, way back when.)

    Not sure if this is what you're asking for...

    K Offline
    K Offline
    kshegunov
    Moderators
    wrote on 3 Nov 2018, 15:59 last edited by kshegunov 11 Mar 2018, 16:00
    #14

    Yes, it is, but only partially. What does your DeviceModel::headerData contain? Does it just delegate to the QStandartItemModel's method?

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mzimmers
      wrote on 3 Nov 2018, 16:03 last edited by
      #15

      I'm not doing anything with the header except what I posted above. DeviceModel is my own class that has the actual Qt model as a member:

      class DeviceModel : public QObject
      {
          Q_OBJECT
      private:
          QAbstractItemModel *m_model;    // the main model
      
      K 1 Reply Last reply 3 Nov 2018, 16:05
      0
      • M mzimmers
        3 Nov 2018, 16:03

        I'm not doing anything with the header except what I posted above. DeviceModel is my own class that has the actual Qt model as a member:

        class DeviceModel : public QObject
        {
            Q_OBJECT
        private:
            QAbstractItemModel *m_model;    // the main model
        
        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 3 Nov 2018, 16:05 last edited by
        #16

        @mzimmers said in formatting a QTableView header:

        I'm not doing anything with the header except what I posted above. DeviceModel is my own class that has the actual Qt model as a member.

        Indeed. My bad, sorry. It's rather odd. As far as I can tell this should be enough to have your header have a red background. As a follow up question: Do you use style sheets?

        Read and abide by the Qt Code of Conduct

        M 1 Reply Last reply 3 Nov 2018, 16:06
        0
        • K kshegunov
          3 Nov 2018, 16:05

          @mzimmers said in formatting a QTableView header:

          I'm not doing anything with the header except what I posted above. DeviceModel is my own class that has the actual Qt model as a member.

          Indeed. My bad, sorry. It's rather odd. As far as I can tell this should be enough to have your header have a red background. As a follow up question: Do you use style sheets?

          M Offline
          M Offline
          mzimmers
          wrote on 3 Nov 2018, 16:06 last edited by
          #17

          @kshegunov said in formatting a QTableView header:

          As a follow up question: Do you use style sheets?

          No, not in this app.

          K 1 Reply Last reply 3 Nov 2018, 16:13
          0
          • M mzimmers
            3 Nov 2018, 16:06

            @kshegunov said in formatting a QTableView header:

            As a follow up question: Do you use style sheets?

            No, not in this app.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 3 Nov 2018, 16:13 last edited by kshegunov 11 Mar 2018, 16:14
            #18

            I just tested a project of mine, and it works correctly. Here's what I did:

            #include <QBrush>
            
            QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
            {
                if (/*role != Qt::DisplayRole || */orientation != Qt::Horizontal || section > columnCount())
                    return QVariant();
            
                if (role == Qt::DisplayRole)
                    return columns.byIndex(section).displayName();
                if (role == Qt::BackgroundRole)
                    return QBrush(Qt::red);
            
                return QVariant();
            }
            

            I observe the background of the table view's header to be red. It's really odd why it doesn't work for you. Can you retrieve the header data just after you've set it to the model to see if it's correctly stored? I.e.

            m_model->setHeaderData(TAG_SERIALNUMBER, Qt::Horizontal, QBrush(Qt::red), Qt::BackgroundRole);
            QBrush shouldBeRed = m_model->headerData(TAG_SERIALNUMBER, Qt::Horizontal, Qt::BackgroundRole);
            

            PS: The test was done with Qt 5.11.2 on Linux.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mzimmers
              wrote on 3 Nov 2018, 16:22 last edited by
              #19

              How do I convert a QVariant to a QBrush?
              0_1541262124374_wifiui.PNG

              K 1 Reply Last reply 3 Nov 2018, 16:51
              0
              • M mzimmers
                3 Nov 2018, 16:22

                How do I convert a QVariant to a QBrush?
                0_1541262124374_wifiui.PNG

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 3 Nov 2018, 16:51 last edited by kshegunov 11 Mar 2018, 16:51
                #20
                QBrush brush = variant.value<QBrush>();
                

                However you can inspect the data directly in the variant as well, as far as I can see in the watch.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 3 Nov 2018, 17:17 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

                  M 1 Reply Last reply 3 Nov 2018, 17:37
                  3
                  • C Christian Ehrlicher
                    3 Nov 2018, 17:17

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

                    M Offline
                    M Offline
                    mzimmers
                    wrote on 3 Nov 2018, 17:37 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?

                    K 1 Reply Last reply 3 Nov 2018, 22:17
                    0
                    • M mzimmers
                      3 Nov 2018, 17:37

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

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 3 Nov 2018, 22:17 last edited by kshegunov 11 Mar 2018, 22:17
                      #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
                      • M Offline
                        M Offline
                        mzimmers
                        wrote on 5 Nov 2018, 17:09 last edited by
                        #24

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

                        K 1 Reply Last reply 5 Nov 2018, 17:12
                        0
                        • M mzimmers
                          5 Nov 2018, 17:09

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

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 5 Nov 2018, 17:12 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
                          • M Offline
                            M Offline
                            mzimmers
                            wrote on 5 Nov 2018, 17:36 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
                            • M Offline
                              M Offline
                              mzimmers
                              wrote on 7 Nov 2018, 19:53 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
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 7 Nov 2018, 20:44 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
                                • M Offline
                                  M Offline
                                  mzimmers
                                  wrote on 7 Nov 2018, 20:46 last edited by
                                  #29

                                  Hi SGaist - like this:

                                      MyProxyStyle *mps = new MyProxyStyle;
                                      ui->tableView->setStyle(mps);
                                  
                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 7 Nov 2018, 20:54 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
                                    • 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

                                          23/41

                                          3 Nov 2018, 22:17

                                          • Login

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