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 (column width)
Forum Updated to NodeBB v4.3 + New Features

formatting a QTableView (column width)

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 10.4k 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 2 Jul 2018, 17:15 last edited by
    #1

    Hi all -

    I need to make some formatting changes to a QTableView. The table displays 2 columns of data; I'd like to have these columns equally fill the space allocated for the table.

    I ran across this:

    By default, the cells in a table do not expand to fill the available space.
    
    You can make the cells fill the available space by stretching the last header section. Access the relevant header using horizontalHeader() or verticalHeader() and set the header's stretchLastSection property.
    
    To distribute the available space according to the space requirement of each column or row, call the view's resizeColumnsToContents() or resizeRowsToContents() functions.
    

    I don't want to resize to content, so that section doesn't apply. If I use the setStretchLastSection() method, I do fill the space, but not equally. What's the best way to do this?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KillerSmath
      wrote on 2 Jul 2018, 18:50 last edited by
      #2

      @mzimmers

      Have you tried to set the ResizeMode of horizontalHeader to Stretch mode ?

      https://doc.qt.io/qt-5.11/qheaderview.html#ResizeMode-enum

      tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
      

      this mode will prevent the resize of columns and correct fill the space for each column.

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mzimmers
        wrote on 2 Jul 2018, 18:56 last edited by
        #3

        Beautiful...thanks.

        Slightly off topic, but I also need to format the cells (not the header). There appears to be a bunch of ways to do that, but I want to keep it simple.

        I thought this (just an example) might work, but it doesn't:

            m_model->setData(m_model->index(1, 1), QColor(Qt::red), Qt::BackgroundRole);
        

        Any suggestions?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KillerSmath
          wrote on 2 Jul 2018, 19:17 last edited by KillerSmath 7 Feb 2018, 19:31
          #4

          @mzimmers said in formatting a QTableView (column width):

          m_model->setData(m_model->index(1, 1), QColor(Qt::red), Qt::BackgroundRole);

          • Do you need to set this data just for one specific element or for every data of a specific column or row ?
          • Which Type of Model are you using ? (QAbstractItemModel, QSqlQueryModel, QSqlTableModel)

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mzimmers
            wrote on 2 Jul 2018, 19:35 last edited by
            #5

            @KillerSmath said in formatting a QTableView (column width):

            Some of the formatting will apply to all cells; others, perhaps alternating rows. I just thought I'd get it working on one cell and then extrapolate.

            I'm using QAbstractItemModel, constructed from QStandardItemModel.

            Thanks...

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KillerSmath
              wrote on 2 Jul 2018, 19:47 last edited by KillerSmath 7 Feb 2018, 19:48
              #6

              This setting is working well:

                     // from http://doc.qt.io/archives/qt-4.8/qstandarditemmodel.html
                     QStandardItemModel *m_model = new QStandardItemModel(4, 4, this);
                      
                      for (int row = 0; row < 4; ++row) {
                          for (int column = 0; column < 4; ++column) {
                              QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
                              m_model->setItem(row, column, item);
                          }
                      }
                      // set background color
                      m_model->setData(m_model->index(1, 1), QColor(Qt::red), Qt::BackgroundRole);
              

              But you could reimplement the data function of Model to return the Red Color by specific row like row%2 == 0 (0, 2, 4 , 6)

              @Computer Science Student - Brazil
              Web Developer and Researcher
              “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

              1 Reply Last reply
              1
              • M Offline
                M Offline
                mzimmers
                wrote on 2 Jul 2018, 20:23 last edited by
                #7

                The only difference I see between your example, and what I'm doing, is the use of the QStandardItem. Is it in fact necessary to do that?

                My model is going to have rows added (and deleted) periodically; I can't pre-allocate them as in your example.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 2 Jul 2018, 20:23 last edited by
                  #8

                  Hi,

                  Depending on how you want to format your data a QIdentityProxyModel might be interesting.

                  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
                  2
                  • K Offline
                    K Offline
                    KillerSmath
                    wrote on 2 Jul 2018, 20:50 last edited by KillerSmath 7 Feb 2018, 20:51
                    #9

                    @mzimmers
                    To avoid these disnecessary calls of setData, you can return the custom color directly of model using a proxy like @SGaist said or creating a custom model derived of QStandardItemModel and just reimplement the data function to return the color how you want :)

                    Example:

                     QVariant CustomModel::data(const QModelIndex &index, int role) const
                      {
                        if ((index.row()%2 == 0) && role == Qt::BackgroundRole) // return red background for pair rows
                          return QColor(Qt::red);
                    
                        return QStandardItemModel::data(index, role);
                      }
                    

                    @Computer Science Student - Brazil
                    Web Developer and Researcher
                    “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mzimmers
                      wrote on 2 Jul 2018, 21:02 last edited by
                      #10

                      OK, I think I'm starting to get this. Something that doesn't make sense yet, though -- my understanding of the model/view paradigm was that the model contained the data, and the view controlled the presentation of the data. Why, then, am I setting a background color on the model and not on the view?

                      K 1 Reply Last reply 2 Jul 2018, 21:07
                      0
                      • M mzimmers
                        2 Jul 2018, 21:02

                        OK, I think I'm starting to get this. Something that doesn't make sense yet, though -- my understanding of the model/view paradigm was that the model contained the data, and the view controlled the presentation of the data. Why, then, am I setting a background color on the model and not on the view?

                        K Offline
                        K Offline
                        KillerSmath
                        wrote on 2 Jul 2018, 21:07 last edited by
                        #11

                        @mzimmers said in formatting a QTableView (column width):

                        OK, I think I'm starting to get this. Something that doesn't make sense yet, though -- my understanding of the model/view paradigm was that the model contained the data, and the view controlled the presentation of the data. Why, then, am I setting a background color on the model and not on the view?

                        This works as the below image:

                        Model View Delegate
                        from Model/View Programming:

                        Delegate controls how the data is showed to user but the delegate receives the information from model.

                        @Computer Science Student - Brazil
                        Web Developer and Researcher
                        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                        1 Reply Last reply
                        2

                        1/11

                        2 Jul 2018, 17:15

                        • Login

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