Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-52446

QTreeView header forgets setStretchLastSection(true)

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.7.1
    • 5.7.0 Alpha, 5.7.0, 5.8.0 Alpha
    • Widgets: Itemviews
    • None
    • fa95eb055401f5264cbc6aca761cb9b5feb4affc

    Description

      Consider the following example:

      #include <QApplication>
      #include <QTreeView>
      #include <QStandardItemModel>
      #include <QHeaderView>
      #include <QTimer>
      
      class TableModel : public QStandardItemModel
      {
          int size;
      public:
          TableModel() : size(0) {}
      
          void update()
          {
              ++size;
              clear();
              setColumnCount(size);
              for (int x = 0; x < size; ++x)
                  setHeaderData(x, Qt::Horizontal, QString::fromLatin1("column %1").arg(x));
          }
      };
      
      class TreeView : public QTreeView
      {
          Q_OBJECT
      
      public:
          QTimer t;
      
          void update()
          {
              static_cast<TableModel *>(model())->update();
              resizeColumnToContents(0);
      //        header()->setStretchLastSection(true); <--- fixes it
          }
      
          TreeView(QWidget *parent = 0) : QTreeView(parent)
          {
              setGeometry(0,0,500,100);
              TableModel *m = new TableModel();
              t.setInterval(1000);
              connect(&t, &QTimer::timeout, this, &TreeView::update);
              setModel(m);
              header()->setStretchLastSection(true);
              t.start();
          }
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          TreeView t;
          t.show();
          return a.exec();
      }
      
      #include "main.moc"
      

      With Qt 5.5 the last column is always stretched to the end of the window. With Qt 5.7, when "column 2" is added, it stops stretching the last column. This can be fixed by "reminding" the header after rebuilding the model and resizing the first column, but such a workaround should not be necessary.

      Attachments

        For Gerrit Dashboard: QTBUG-52446
        # Subject Branch Project Status CR V

        Activity

          People

            ulherman Ulf Hermann
            ulherman Ulf Hermann
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes