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.4k 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 2 Nov 2018, 21:11 last edited by
    #1

    Hi all -

    My main UI looks like this:

    0_1541193064683_wifiui.PNG

    The QTableView is the white box near the top. The first row of text (the "MAC Address Serial Number Device Name" is the header data (set in the model). The line that begins "b4:e6" is the first row of data in the table.

    I'd like to improve this display, and the first thing that comes to mind is to visually set off the header from the data. I've looked through the pages on models and tableviews, but I can't find how to go about formatting this. Could someone point me in the right direction?

    Thanks...

    K 1 Reply Last reply 2 Nov 2018, 21:18
    0
    • M mzimmers
      2 Nov 2018, 21:11

      Hi all -

      My main UI looks like this:

      0_1541193064683_wifiui.PNG

      The QTableView is the white box near the top. The first row of text (the "MAC Address Serial Number Device Name" is the header data (set in the model). The line that begins "b4:e6" is the first row of data in the table.

      I'd like to improve this display, and the first thing that comes to mind is to visually set off the header from the data. I've looked through the pages on models and tableviews, but I can't find how to go about formatting this. Could someone point me in the right direction?

      Thanks...

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 2 Nov 2018, 21:18 last edited by
      #2

      You can customize the header through returning the appropriate values for the roles from the model. But(!), it'd depend on how your program's organized. How do you populate the header?

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mzimmers
        wrote on 2 Nov 2018, 21:25 last edited by
        #3

        Like so:

            m_model->insertColumns(0, TAG_NBRTAGS);
            m_model->setHeaderData(TAG_SERIALNUMBER, Qt::Horizontal, tr("Serial Number"));
            m_model->setHeaderData(TAG_MACADDRESS, Qt::Horizontal, tr("MAC Address"));
            m_model->setHeaderData(TAG_DEVICENAME, Qt::Horizontal, tr("Device Name"));
        
        K 1 Reply Last reply 2 Nov 2018, 21:29
        0
        • M mzimmers
          2 Nov 2018, 21:25

          Like so:

              m_model->insertColumns(0, TAG_NBRTAGS);
              m_model->setHeaderData(TAG_SERIALNUMBER, Qt::Horizontal, tr("Serial Number"));
              m_model->setHeaderData(TAG_MACADDRESS, Qt::Horizontal, tr("MAC Address"));
              m_model->setHeaderData(TAG_DEVICENAME, Qt::Horizontal, tr("Device Name"));
          
          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 2 Nov 2018, 21:29 last edited by
          #4

          Okay. Firstly you should use Qt::DisplayRole, not the default Qt::EditRole. Then you can modify further[1]:

          1. Icons - Qt::DecorationRole
          2. Font - Qt::FontRole
          3. Alignment - Qt::TextAlignmentRole

          and so on.

          [1] http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum

          Read and abide by the Qt Code of Conduct

          M 1 Reply Last reply 2 Nov 2018, 21:43
          4
          • K kshegunov
            2 Nov 2018, 21:29

            Okay. Firstly you should use Qt::DisplayRole, not the default Qt::EditRole. Then you can modify further[1]:

            1. Icons - Qt::DecorationRole
            2. Font - Qt::FontRole
            3. Alignment - Qt::TextAlignmentRole

            and so on.

            [1] http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum

            M Offline
            M Offline
            mzimmers
            wrote on 2 Nov 2018, 21:43 last edited by
            #5

            @kshegunov: wow -- this is fairly involved stuff. So, do I need to create a bitmap image of a line to use as an icon that would be my delimiter? (Not sure why font or alignment matter, as I'm trying to modify a data row, not the format of the text in that row.)

            Thanks.

            K 1 Reply Last reply 2 Nov 2018, 21:55
            0
            • M mzimmers
              2 Nov 2018, 21:43

              @kshegunov: wow -- this is fairly involved stuff. So, do I need to create a bitmap image of a line to use as an icon that would be my delimiter? (Not sure why font or alignment matter, as I'm trying to modify a data row, not the format of the text in that row.)

              Thanks.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 2 Nov 2018, 21:55 last edited by
              #6

              @mzimmers said in formatting a QTableView header:

              wow -- this is fairly involved stuff. So, do I need to create a bitmap image of a line to use as an icon that would be my delimiter? (Not sure why font or alignment matter, as I'm trying to modify a data row, not the format of the text in that row.)

              Not really, no. What you should start with is to use Qt::DisplayRole so not to get the weird header (currently it thinks it's an editor of sorts). As a second step you can set any of the mentioned roles to get more customized looks.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mzimmers
                wrote on 2 Nov 2018, 22:03 last edited by
                #7

                I made the change to DisplayRole, but I don't see a difference in the header. What is it supposed to look like?
                0_1541196185564_wifiui.PNG

                K 1 Reply Last reply 2 Nov 2018, 22:21
                0
                • M mzimmers
                  2 Nov 2018, 22:03

                  I made the change to DisplayRole, but I don't see a difference in the header. What is it supposed to look like?
                  0_1541196185564_wifiui.PNG

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 2 Nov 2018, 22:21 last edited by
                  #8

                  @mzimmers said in formatting a QTableView header:

                  What is it supposed to look like?

                  I imagined it would be gray after changing the role. Maybe it's just the style you're using. Try setting Qt::BackgroundRole to something to make it distinguishable. (assuming that's what you wanted to do in the first place).

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mzimmers
                    wrote on 2 Nov 2018, 22:33 last edited by
                    #9

                    OK, I clearly have no idea what I'm doing. The whole concept of these "roles" eludes me.

                    I tried this, and it didn't work:

                        ui->tableView->horizontalHeader()->setBackgroundRole(QPalette::Window);
                    
                    
                    K 1 Reply Last reply 2 Nov 2018, 23:20
                    0
                    • M mzimmers
                      2 Nov 2018, 22:33

                      OK, I clearly have no idea what I'm doing. The whole concept of these "roles" eludes me.

                      I tried this, and it didn't work:

                          ui->tableView->horizontalHeader()->setBackgroundRole(QPalette::Window);
                      
                      
                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 2 Nov 2018, 23:20 last edited by
                      #10

                      Try something like this:

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

                      Roles give information of what kind of data you are keeping (think of it as the item's properties).

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      1
                      • M Offline
                        M Offline
                        mzimmers
                        wrote on 3 Nov 2018, 14:04 last edited by
                        #11

                        Still no visible change. Do I need to do something to the table view to reflect the change to the model object?

                        K 1 Reply Last reply 3 Nov 2018, 15:04
                        0
                        • M mzimmers
                          3 Nov 2018, 14:04

                          Still no visible change. Do I need to do something to the table view to reflect the change to the model object?

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 3 Nov 2018, 15:04 last edited by
                          #12

                          That's very odd. Do you mind sharing the headerData method of your model? Or is the model QStandardItemModel?

                          Read and abide by the Qt Code of Conduct

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

                            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 1 Reply Last reply 3 Nov 2018, 15:59
                            0
                            • 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

                                          1/41

                                          2 Nov 2018, 21:11

                                          • Login

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