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. Resize QTableView / QSqlQueryModel
Forum Updated to NodeBB v4.3 + New Features

Resize QTableView / QSqlQueryModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 2.2k Views
  • 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.
  • G Offline
    G Offline
    Gaalee
    wrote on last edited by Gaalee
    #1

    Hi, i have 2 problems (sorry my english is bad, let me know if you don't understand).

    1. Is their a way to use resizeColumnsToContents() and resizeSections(QHeaderView::ResizeMode::Stretch) at the same time ? Because if i used only resizeColumnsToContents it's like this (didn't feet the white space) : example
      And if i used resizeSections(QHeaderView::ResizeMode::Stretch) it's like this (some columns are unnecessarily large and some need to be large to display their content) : example

    2. At initialization the table is not affected by the resize methodes, i tried to setVisible(0 and 1) between resize but it doesn't work. Did you have an idea ?

    Thanks for helping me.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2
      1. http://doc.qt.io/qt-5/qheaderview.html#stretchLastSection-prop
      2. could you try forcing a call to update() on the QTableView?

      "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
      2
      • G Offline
        G Offline
        Gaalee
        wrote on last edited by Gaalee
        #3
        1. Result :
          example
          It's still looking bad. Is their a way to set an equal stretch between each columns ? Like that : example

        2. Apparantly not.

        VRoninV 1 Reply Last reply
        0
        • G Gaalee
          1. Result :
            example
            It's still looking bad. Is their a way to set an equal stretch between each columns ? Like that : example

          2. Apparantly not.

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @Gaalee said in Resize QTableView / QSqlQueryModel:

          It's still looking bad. Is their a way to set an equal stretch between each columns ? Like that :

          setSectionResizeMode(QHeaderView::Stretch)

          "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
          0
          • G Offline
            G Offline
            Gaalee
            wrote on last edited by
            #5

            Doesn't work with resizeColumnsToContents().

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              Ok, now I understood, you want to equally distribute the extra space among the remaining columns.

              protected:
              void resizeEvent(QResizeEvent *ev)
              {
                  QWidget::resizeEvent(ev);
                  resizeHeaders();
              }
              Q_SLOT void resizeHeaders()
              {
                  QHeaderView* const tableHead = view->horizontalHeader();
                  const int secCount = tableHead->count();
                  if (view->model()->rowCount() == 0 || secCount<=1) {
                      tableHead->resizeSections(QHeaderView::Stretch);
                      return;
                  }
                  tableHead->resizeSections(QHeaderView::ResizeToContents);
                  QVector<int> allHints(secCount);
                  for (int i = 0; i < secCount; ++i)
                      allHints[i]= tableHead->sectionSize(i);
                  const int extraSpace = tableHead->width() - std::accumulate(allHints.constBegin(), allHints.constEnd(), 0);
                  for (int i = 0; i < secCount; ++i)
                      tableHead->resizeSection(i, allHints.at(i) + static_cast<int>(static_cast<double>(extraSpace) / static_cast<double>(secCount)));
              }
              

              then in the constructor add

              view->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
              view->horizontalHeader()->setStretchLastSection(true);
              connect(model, &QAbstractItemModel::rowsRemoved, this, &QuoteViewer::resizeHeaders);
                  connect(model, &QAbstractItemModel::dataChanged, this, &QuoteViewer::resizeHeaders);
                  resizeHeaders();
              

              This will distribute the extra space evenly, not based on the original size, it's trivial to modify the math to achieve this other result btw

              "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