View Full Version : QTreeWidget - setting an absolute height

2nd November 2011, 14:26
Hi all,

I've searched for this, but don't see anyone saying it can be done. I have seen people saying it can't. I just wanted to make sure that this is the case.

In software that I am now beginning to support, I have created a new dockable panel in an existing dock area. I have a QVBoxLayout with two QTreeWidgets in it, each having a QLabel just before (so it's QLabel over QTreeWidget over QLabel over QTreeWidget). I want a known amount of rows in each of these, and chose QTreeWidget because it is easy to add items that have columns, and to add headers for those columns.

The first tree widget has 3 rows in it, and the second has 6, but the QVBoxLayout is giving them the same amount of space each. I would like to have an absolute height, where each widget shows only the rows it has, with no whitespace below. I tried setting the stretch factor for the two labels and the first tree widget to 0, and then tried 1 for the second tree widget. I even tried up to 3 for the second, but all that does is to create greater height for the second tree widget and it doesn't reduce the height of the first.

I also looked into styling the widget, but the Qt page describing styling doesn't show any ability for styling to limit widget height/width.

I know that I can set the size policy to fixed for each, but I don't see how I can set the size hint for a tree widget. This is slightly frustrating. I'm not sure why it should be difficult to constrain tree widgets to the size I need them to be.

Can anyone tell me where I am going wrong here? Is there something that I can do to get these tree widgets to be sized exactly to show their 3 and 6 rows respectively? If not, it must be possible to maybe choose another widget to get the effect I am looking for. Any ideas?


3rd November 2011, 22:07
Ok, so there isn't a way to do this. I ended up creating a QWidget per QTreeWidget, putting a layout into each QWidget, and putting my QTreeWidgets into those layouts. Then, setting the geometry of the outer widgets worked spankingly.