PDA

View Full Version : QTreeWidget Hide Column Headers



tvj4218
26th April 2017, 01:34
Hello.

I've got a QTreeWidget with column headers. When I scroll down to see more data, eventually the column header disappear and I don't know what each column stands for.

How do I lock the top row so the column headers are always visible?

Thanks.

t

Santosh Reddy
5th May 2017, 08:23
QTreeWidget's column headers don't scroll, they are always visible. Not sure how you was set it up. My guess is that QTreeWidget is in a QScrollArea.

tvj4218
18th September 2017, 03:17
Thank you for your reply.

Why would having it in a scroll area create this problem? I will have other items below this table so I may need a scroll area.

n

d_stranz
18th September 2017, 04:00
Why would having it in a scroll area create this problem?

I think you are confusing the scroll bars of a scroll area that contains a tree widget vs. the scroll bars of a standalone tree widget. A scroll area will scroll everything inside it without respect what that is, so if you have a tree widget which reports its height as being greater than the height of the scroll area, then as you scroll down using the scroll area's scroll bar eventually the column headers will scroll out of view. Remember that the scroll area will set its vertical scroll bar to match the height reported by the widget inside it, so if you have a very tall tree, the tree will report its height, the scroll area will say OK, you got it and set its vertical scroll bar appropriately. Because the tree widget thinks it has as much height as it needs, it doesn't turn its scroll bar on. The scroll bar you see belongs to the scroll area, not the tree widget.

On the other hand, if the tree widget is standalone (eg. in a layout that controls its maximum size), then it will turn on its own scroll bars if the contents are larger than the available space. And when you scroll a tree widget using its own scroll bars, the column headers don't scroll up when you move the vertical scroll bar and will scroll left and right when you move the horizontal scroll bar.

So you can't have it both ways. You can't put a tree widget inside a scroll area and prevent it from being scrolled off the top if the size of everything in the scroll area is larger than the physical window in which the scroll area lives. That's the way scroll areas work. If you really need the behavior you describe, then a simple scroll area as the main widget in a window won't do it. Probably what you will need to do is make a plain QWidget as the main widget, add a vertical layout to that, then put the tree widget at the top of the layout and add a scroll area under that. That way the tree and the scroll area will have independent scroll bars. You can scroll the contents of the scroll area without affecting the tree.