Results 1 to 4 of 4

Thread: QTreeView - force to show expand arrows on empty folders

  1. #1
    Join Date
    Jul 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTreeView - force to show expand arrows on empty folders

    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.

  2. #2
    Join Date
    Aug 2012
    Location
    Montreal
    Posts
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView - force to show expand arrows on empty folders

    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.

  3. #3
    Join Date
    Jul 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView - force to show expand arrows on empty folders

    Thank you for replay, I thought that won't be easy to do. Fortunately I don't need to code it.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeView - force to show expand arrows on empty folders

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Q3ListView expand arrows incorrect on Mac in Qt 4.8
    By AndyBrice in forum Qt Programming
    Replies: 6
    Last Post: 4th April 2012, 10:11
  2. Replies: 4
    Last Post: 8th January 2010, 08:40
  3. How to remove empty folders?
    By merry in forum Qt Programming
    Replies: 19
    Last Post: 16th December 2008, 09:14
  4. QTreeView expand signals
    By Vidar Bøe in forum Qt Programming
    Replies: 2
    Last Post: 26th May 2008, 09:47
  5. Replies: 11
    Last Post: 2nd August 2007, 20:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.