PDA

View Full Version : QTreeView and unwanted scrollbar



theprobe
11th January 2011, 16:05
Hi,
I have a QTreeView that is populated by setModel:


model = new MyModel();
myTreeView->setModel(model);

When I do this the first time, my model is empty, but is subsequently filled.
The tree view appears as expected.

However, when I do this with a model that is not empty, I find that unwanted
and unneccesary horizontal scrollbars are appearing - the row is not wide
enough to warrant one.

If I do:

myTreeView->resizeColumnToContents(0);

the scrollbars go away, but I don't want to have to force my larger items to be
fully open. Any pointers?
Thanks in advance

bothorsen
12th January 2011, 06:50
This is pretty difficult to get right. Qt doesn't do automatic sizing of the column widths on open, so you have to do it yourself. Basically, you have to do the resizeColumnToContents on all the columns, and then distribute the remaining space to the columns you want. You can call setStretchLastSection on your horizontal header, for something that does a remotely reasonable thing.

I would also suggest to you to save the state of the headers so the user doesn't have to resize the views to his liking every time.

theprobe
12th January 2011, 08:19
Hi, thanks for your answer. One question though, how come it does what I want for the case where model is empty upon setModel, but something different for when the model is subsequently populated?
Thanks,