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 anenum
member that says if theBookContent
class is aFile
orArchive
.BookContent
can have multiple book children, meaning that if aBook
is anArchive
it will have a collection ofBook
asFile
inside.I have a collection of
BookContent
so I was wondering how can I list that in a tree view, but the thing is, ifBookContent
is aArchive
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 ofFile
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.
-
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 ;)