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

QHeaderView: ResizeMode suggestion

    XMLWordPrintable

Details

    Description

      It seems that achieving the following behavior is a little harder than it should be with a QHeaderView.

      Have the last column stretch but not to a smaller size than enough to display all its contents.

      Essentially something like:

      enum ResizeMode

      { Interactive, Stretch, Fixed, ResizeToContents, Custom = Fixed, ResizeToContentsButStretchToRightEdgeOfViewport }

      ;

      To achieve this today one has to do stuff like this:

      #include <QtGui>
      
      class TreeWidget : public QTreeWidget
      {
          Q_OBJECT
      public:
          TreeWidget(QWidget *parent = 0)
              : QTreeWidget(parent)
          {
              setColumnCount(1);
              setRootIsDecorated(true);
              QTreeWidgetItem *item;
              for (int i=0; i<10; ++i) {
                  item = new QTreeWidgetItem;
                  item->setText(0, QString("Item number %1").arg(i));
                  addTopLevelItem(item);
                  for (int j=0; j<10; ++j) {
                      QTreeWidgetItem *child = new QTreeWidgetItem(item);
                      child->setText(0, QString("Child Item number %1").arg(j));
      
                  }
              }
              connect(model(), SIGNAL(dataChanged(QModelIndex, QModelIndex)),
                      this, SLOT(fixHeader()));
              connect(model(), SIGNAL(rowsInserted(QModelIndex, int, int)),
                      this, SLOT(fixHeader()));
              connect(model(), SIGNAL(rowsRemoved(QModelIndex, int, int)),
                      this, SLOT(fixHeader()));
              connect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(fixHeader()));
              connect(this, SIGNAL(collapsed(QModelIndex)), this, SLOT(fixHeader()));
      
      
          }
          void resizeEvent(QResizeEvent *e)
          {
              QTreeWidget::resizeEvent(e);
              fixHeader();
          }
          void timerEvent(QTimerEvent *e)
          {
              if (e->timerId() == timer.timerId()) {
                  timer.stop();
                  const int s = qMax(viewport()->width(), sizeHintForColumn(0));
                  header()->resizeSection(0, s);
              } else {
                  QTreeWidget::timerEvent(e);
              }
          }
      
      public slots:
          void fixHeader()
          {
              timer.start(10, this);
          }
      private:
          QBasicTimer timer;
      };
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      public:
          MainWindow(QWidget *parent = 0)
              : QMainWindow(parent)
          {
              QSplitter *splitter = new QSplitter(Qt::Horizontal, this);
              setCentralWidget(splitter);
              TreeWidget *tw = new TreeWidget(splitter);
              splitter->addWidget(tw);
              splitter->addWidget(new QTextEdit(this));
          }
      public slots:
      };
      
      #include "main.moc"
      
      int main(int argc, char **argv)
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            rve Anders Bakken
            Votes:
            2 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes