Qt6 topics merged with General

Model, view and delegate help

  • I have a class that is called BookContent, this book can be a file or a zip archive with files in it. In that class I have the authors name, and an enum member that says if the BookContent class is a File or Archive. BookContent can have multiple book children, meaning that if a Book is an Archive it will have a collection of Book as File inside.

    I have a collection of BookContent so I was wondering how can I list that in a tree view, but the thing is, if BookContent is a Archive I want to put a button (See) in the tree view along side that item and when the user clicks on that button, calls a method.

    That method that the user clicked will make an operation to get the zip archive contents and then create another BookContent for each one with the type of File and put as children for the clicked item on the tree view.


    Something like that.

  • Hi @Mr-Gisa

    Why not use a treeView the standard way.

    In your case, the "see" button serves nothing because it's the exact function of the triangle at the beginning of a row in a treeWiew.
    When the user click on the triangle, the tree expands to show the item contents.

    All you have to do is to listen to the expand() signal and act accordingly.

  • But the triangle appears only if it has children, right?

  • I think your can return true here:

    bool QAbstractItemModel::hasChildren(const QModelIndex &parent = QModelIndex()) const

    even if there is no children yet.

    When you receive the expend() signal, you unzip the file if it's not already done, and then update the model

  • The thing is that I wanted the expand triangle to appear after the extraction, because if I do the way you pointed out I will have to check if it has been extracted already and put more code in the overridden model class method and I think that adding a custom delegate to draw a button with a signal with clicked is easier, idk. What you think?

  • At first think, i would say you need to memorize the state unzipped or not.
    But in fact, when you receive the expand signal, if the parent is a zip file and it has no children, then it means an unzip operation is needed.

    I never did this kind of thing, but i don't see any issue - may be i'm wrong, nasty bug always lurking around ;)

  • I will try something with the tree list example that comes with Qt and also reading a few things on the internet. Anything I will be back here.

Log in to reply