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

resizeToContents sometimes slightly to small

    XMLWordPrintable

Details

    Description

      Sometimes QHeaderView::ResizeToContents and resizeRowsToContents() lead to a slightly too small widget size in combination with word wrap:
      It seems to appear more often when having a decoration data but also without it appears from time to time. It can be easily reproduced when resizing the window and watching the text wrap.

      I created a minimal working example where the problem occurs:

      main.cpp
      #include <QtGui/QApplication>
      #include <QMainWindow>
      #include <QAbstractTableModel>
      #include <QList>
      #include <QString>
      #include <QResizeEvent>
      #include "ui_mainwindow.h"
      
      class MyModel : public QAbstractTableModel {
          Q_OBJECT
          QList<QString> items;
      public:
          MyModel(QWidget* parent = 0) : QAbstractTableModel(parent) {
              items.append("The less you know about computers the more you want Microsoft! -- Microsoft ad campaign, circa 1996");
          }
          int rowCount(const QModelIndex& /*parent = QModelIndex()*/) const { return 1; }
          int columnCount(const QModelIndex& /*parent = QModelIndex()*/) const { return items.size(); }
          QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const {
              if (role == Qt::DisplayRole) return items[index.column()];
              if (role == Qt::DecorationRole) return QIcon::fromTheme(QLatin1String("dialog-information"));
              else return QVariant();
          }
      };
      
      namespace Ui { class MainWindow; }
      class MainWindow : public QMainWindow {
          Q_OBJECT
          Ui::MainWindow* ui;
      public:
          MainWindow(QWidget* parent = 0) : QMainWindow(parent), ui(new Ui::MainWindow) {
              ui->setupUi(this);
              ui->tableView->setModel(new MyModel(this));
              ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
              ui->tableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
              ui->tableView->resizeRowsToContents();
              ui->tableView->verticalHeader()->setVisible(false);
          }
          ~MainWindow() { delete ui; }
      };
      
      #include "main.moc" // required due to single-file
      
      int main(int argc, char *argv[]) {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
      }
      
      mainwindow.ui
      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class&gt;MainWindow</class&gt;
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>264</width>
          <height>200</height>
         </rect>
        </property>
        <widget class="QWidget" name="centralWidget">
         <layout class="QVBoxLayout" name="verticalLayout">
          <item>
           <widget class="QTableView" name="tableView"/>
          </item>
         </layout>
        </widget>
       </widget>
       <layoutdefault spacing="6" margin="11"/>
       <resources/>
       <connections/>
      </ui>
      
      QtTest.pro
      QT       += core gui
      TARGET    = QtTest
      TEMPLATE  = app
      SOURCES  += main.cpp
      FORMS    += mainwindow.ui
      

      The example is also attached as an archive.

      Systems tested:

      • Ubuntu 12.04.2 LTS, Qt 4.8.1-0ubuntu4.4
      • Windows 7, Qt 4.8.0

      Attachments

        Issue Links

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

          Activity

            People

              stephen.kelly Stephen Kelly (Unused account) (Inactive)
              scai Alexander Heinlein
              Votes:
              2 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes