Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QTreeView: selecting full width of a row without text being truncated
I am seeking a way to make QTreeView (with one column) give the following behaviour:
- When a row is selected, the selection colour should fill the width of the visible part of the tree view, i.e. there should never be any white space to the right of selected items.
- Text of items in the tree view should never be truncated, and if the text is too wide to fit in the available space, a horizontal scroll bar should appear. When items are too wide, the selection colour of all entries should take the full width of the tree view, and this width should coincide with the longest visible entry's width.
I can make either of these things happen at the moment, but not both at once. I would prefer to avoid event filters and subclassing QTreeView if possible.
Please let me know if there is anything I need to clarify or if there are any further details that would be useful. If it's of any relevance, the tree view is in the left half of a horizontal splitter. The tree view has minimum dimensions set and the splitter's children are not collapsible.
ludde last edited by
Not sure I understand exactly what you want to achieve, but have you tried to set the view's header's resizeMode and stretchLastSection property?
[quote author="ludde" date="1328794218"]Not sure I understand exactly what you want to achieve, but have you tried to set the view's header's resizeMode and stretchLastSection property?[/quote]
Thanks for the suggestion. I have the scroll bar policies of the tree view as ScrollBarAsNeeded, and have tried the different combinations of the header's resizeMode and stretchLastSection properties. Most of these combinations result in one of the following:
- No scroll bar appears and text that exceeds the available space is truncated. This is problematic because when the tree gets very deep the user can't see items fully (or at all).
- The text is truncated at a fixed point (which is obviously not great, but I include it for the sake of completion).
The exception is when the header has resizeMode set to ResizeToContents and stretchLastSection set to false. In this case, when an item is selected, the background selection colour extends to the point that coincides with the furthest right point of widest visible item in the tree. This means that the selection colour doesn't fill the width of the tree view when all the items have short amount of text - but it does mean a scroll bar appears when entries become too wide.
I will try to get some images of the behaviour I'm looking for. Thanks again.
"Here":http://www.google.com/imgres?um=1&hl=en&client=ubuntu&sa=N&channel=fs&gl=uk&biw=1553&bih=798&tbm=isch&tbnid=TU8tJeieQP3JCM:&imgrefurl=http://msdn.microsoft.com/en-us/library/ms996437.aspx&docid=FGb115q9lXjvdM&imgurl=http://i.msdn.microsoft.com/dynimg/IC77718.gif&w=282&h=350&ei=qN0zT4vLDIal0QWqup2-Ag&zoom=1&iact=rc&dur=347&sig=113596841083879039400&page=2&tbnh=134&tbnw=108&start=32&ndsp=39&ved=1t:429,r:12,s:32&tx=64&ty=88 is an image of a tree view, with a wide entry that has necessitated a scroll bar. I would like my tree view to look like this, except I would like the blue selection background the 'Ringer' entry to always fill the whole width of the white area - the way items are highlighted in "this image.":http://www.google.com/imgres?um=1&hl=en&client=ubuntu&hs=zWW&sa=N&channel=fs&gl=uk&biw=1600&bih=827&tbm=isch&tbnid=Q8VVttKbSOONCM:&imgrefurl=http://www.palminfocenter.com/news/7805/splashid-4-for-mac-os-x-released/&docid=noft9BWfGJd_eM&imgurl=http://www.palminfocenter.com/images/splashid-mac-desktop-v4.jpg&w=550&h=310&ei=vd4zT9iGM8rK0QXysI3DAg&zoom=1&iact=rc&dur=431&sig=113596841083879039400&page=1&tbnh=107&tbnw=189&start=0&ndsp=28&ved=1t:429,r:2,s:0&tx=119&ty=72 I would also like the scroll bar to be visible only when there are overly wide items visible in the tree.
I hope that has helped to clarify a bit.