5

Form setup:

QSplitter (highlighted on the image) that contains QTreeWidget (left) and QTableWidget (right):

Non-default properties of these two widgets within QSplitter:

  • QTreeWidget

    minimumSize / Width = 150

  • QTableWidget

    minimumSize / Width = 300
    sizePolicy / Horizontal Stretch = 1

Basically the above setup specifies the minimum width of these two components, and that QTableWidget is the one that will change it's width during form resize.
The buttons on the right side are placed in QVBoxLayout and it's size is fixed.

Question:

While resizing the form QTreeWidget's width goes from minimum 150 up to unknown 256, and only then QTableWidget starts to grow, while I expect QTreeWidget to not grow in width at all, since QTableWidget is the one, that has Horizontal Stretch set to 1.

Please note, this has nothing to do with QSplitter, since the same happens when I place these two widgets in QHBoxLayout instead.


UPDATE:

Setting QTreeWidget maximumSize / Width to 150 (same as minimumSize / Width) to avoid resizing of this widget gives following:

QTreeWidget is fixed now to 150, but when the window is resized there is an empty gap between the two widgets. This gap grows up to 256-150=106, and then QTableWidget starts to expand.
Basically the result is similar, but this time area that was taken by QTreeWidget now is dedicated to this empty gap.

I've started to feel this might be a Qt bug.

Technical info:

  • Ubuntu 10.04 (Lucid Lynx) x86_64
  • GCC 4.4.3
  • Qt 4.6.2
Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
  • And what is `QSizePolicy::Policy` for those widgets? What happens, if you set `QSizePolicy::Expanding` for `QTableWidget`? – Lol4t0 Dec 17 '11 at 19:46
  • `QSizePolicy` is default for both widgets, which is Expanding. Even if I set `QTreeWidget`'s Horizontal Policy to Minimum, the result is the same. – Andrejs Cainikovs Dec 18 '11 at 16:39
  • Try `QSizePolicy::Maximum` for `QTreeWidget`. And `QSizePolicy::Expanding` for `QTableWidget` Minimum is not actually 'widget should have minimum size', read this doc: http://developer.qt.nokia.com/doc/qt-4.8/qsizepolicy.html – Lol4t0 Dec 18 '11 at 20:26
  • Nope, setting `QTreeWidget`'s horizontal size policy to `Maximum` gives the same result. – Andrejs Cainikovs Dec 19 '11 at 00:54

2 Answers2

3

The size hint of 256 width is hard-coded into QAbstractScrollArea. (Why? I have no idea.) Here is the method, from qabstractscrollarea.cpp:

QSize QAbstractScrollArea::sizeHint() const
{
    return QSize(256, 192);
#if 0
    Q_D(const QAbstractScrollArea);
    int h = qMax(10, fontMetrics().height());
    int f = 2 * d->frameWidth;
    return QSize((6 * h) + f, (4 * h) + f);
#endif
}

I'm not sure what's going on with the 0 macro ... maybe someone started to implement a non-hardcoded width but didn't finish, and this is for testing?

Anyhow, you can work around it by subclassing QTreeView and implementing your own size hint, for example:

class MyTreeView : public QTreeView {
public:
  explicit MyTreeView(QWidget *parent = NULL) : QTreeView(parent) { }
  QSize sizeHint() const {
    return QSize(150, 192);
  }
};
Dave Mateer
  • 17,608
  • 15
  • 96
  • 149
0

Have you tried setting sizePolicy / HorizontalPolicy to Fixed ?

TonyK
  • 16,761
  • 4
  • 37
  • 72