PDA

View Full Version : QTreeView - force to show expand arrows on empty folders



cierpliwy
16th August 2012, 13:01
Hello there,

I have created custom file system model which displays folders and file. There is one requirement: I would like to always display expand arrow next to the empty folders. Therefore I implemented two methods:

hasChildren() - always return true if item is a folder
rowCount() - in empty folder, returns 0.

Right now I have an effect where arrow is sometimes visible and sometimes not (hasChildren return true, rowCount returns 0).
I don't know if I'm doing this right way. It looks like function get sometimes information about a children from rowCount instead of hasChildren.

Is there any way I can get it working? I'm using Qt 4.7.4

Thanks in advance.

maximebd
16th August 2012, 18:09
Unfortunately, this behaviour cannot be acheived with the stock QTreeItemView.

The QTreeItemView is a complex piece of code and to keep track of which branches are expended and what is currently on display, it caches as much data as it can in a tree structure. This means that the hasChildren() method will only be called on the initial rendering of the tree node. As soon as rowCount() is called, the data will be cached and QTreeItemView will then rely on the size of a vector containing pointers to the children QItemIndex in order to decide whether to draw the expend arrow icon or not.

If you absolutely want to get this behaviour, it is possible to re-implement a custom tree item view class. However, from having done so myself, it is not something I recommend, but I can provide some help if you persist on that solution.

cierpliwy
17th August 2012, 12:25
Thank you for replay, I thought that won't be easy to do. Fortunately I don't need to code it.

d_stranz
19th August 2012, 01:09
I thought that won't be easy to do. Fortunately I don't need to code it.

It would also have a different behavior and appearance from almost every other example of a tree that I can think of. Users know that if a tree item does not have an expand icon next to it, it has no children. If you put expand icons next to everything, then this will be very confusing, because a user will click it, and then what? Nothing happens? The expand icon stays the same? Or the tree expands and then what? There's nothing to show.

You can achieve the same effect by putting an actual dummy item as a child of the parents that have no real children; the tree will display the icon in the right state, and when clicked it will have the right behavior. You can set the text of these dummy items to empty or some string like "Null item" (or if the tree is editable, "Click to add" or something). This would be a lot better than changing a standard GUI behavior. You can always add some data value to the dummy children that tells you app that it isn't a real child if it needs to respond to user clicks on children.