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.7k 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.
  • KillerSmathK Offline
    KillerSmathK Offline
    KillerSmath
    wrote on 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
    • mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on 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
      • KillerSmathK Offline
        KillerSmathK Offline
        KillerSmath
        wrote on last edited by KillerSmath
        #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
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on 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
          • KillerSmathK Offline
            KillerSmathK Offline
            KillerSmath
            wrote on last edited by KillerSmath
            #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
            • mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on 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
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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
                • KillerSmathK Offline
                  KillerSmathK Offline
                  KillerSmath
                  wrote on last edited by KillerSmath
                  #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
                  • mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on 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?

                    KillerSmathK 1 Reply Last reply
                    0
                    • mzimmersM mzimmers

                      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?

                      KillerSmathK Offline
                      KillerSmathK Offline
                      KillerSmath
                      wrote on 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

                      • Login

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