You need to write a subclass of QDirModel/QFileSystemModel, and rewrite the flags (), data (), and setData () Methods of QDirModel/QFileSystemModel.
Define a QSet <QPersistentModelIndex> named checkedIndexes to record the Checked path name.
In flags (), ItemIsUserCheckable is returned.
Check whether the given item is in checkedIndexes in data (). If yes, the Qt: Checked is returned, and no
Returns Qt: Unchecked.
SetData () adds the given item with the value Qt: Checked and all child nodes of the given item to checkedIndexes.
RecursiveCheck () is used to add subnodes to a rule.
The following code is written based on the QDirModel. If QFileSystemModel is used, it is not good to automatically select a sub-node when selecting a folder, because QFileSystemModel is asynchronously loaded, and rowCount () always returns 0 before loading. QDirIterator can be used to traverse all sub-nodes. Although this function can be implemented, it has a disadvantage that it will disrupt the arrangement of files. I have not found a good solution, so if there are few files, we should use QDirModel.
CFileSystemModel. h
# Ifndef CFileSystemModel_H <br/> # define CFileSystemModel_H <br/> # include <QDirModel> <br/> # include <QSet> <br/> # include <QPersistentModelIndex> <br/> class CFileSystemModel: public QDirModel <br/>{< br/> public: <br/> CFileSystemModel (); <br/> QSet <QPersistentModelIndex> checkedIndexes; <br/> Qt :: itemFlags flags (const QModelIndex & index) const; <br/> QVariant data (const QModelIndex & index, int role) const; <br/> bool setData (const QModelIndex & index, const QVariant & value, int role); <br/> private: <br/> bool recursiveCheck (const QModelIndex & index, const QVariant & value); <br/> }; <br/> # endif // CFileSystemModel_H
CFileSystemModel. cpp
# Include "cfilesystemmodel. H "<br/> # include <qdebug> <br/> cfilesystemmodel: cfilesystemmodel () <br/>{< br/>}< br/> QT :: itemflags cfilesystemmodel: Flags (const qmodelindex & Index) const <br/>{< br/> return qdirmodel: Flags (INDEX) | QT: itemisusercheckable; <br/>}< br/> qvariant cfilesystemmodel: Data (const qmodelindex & Index, int role) const <br/>{< br/> If (role = QT:: checkstaterole) <br/>{< br/> re Turn checkedindexes. Contains (INDEX )? Qt: checked: QT: unchecked; <br/>}< br/> else <br/>{< br/> return qdirmodel: Data (index, role ); <br/>}< br/> bool cfilesystemmodel: setdata (const qmodelindex & Index, const qvariant & Value, int role) <br/>{< br/> If (role = QT: checkstaterole) <br/>{< br/> If (value = QT: checked) <br/>{< br/> checkedindexes. insert (INDEX); <br/> If (haschildren (INDEX) = true) <br/>{< br/> recursivecheck (index, value ); <br/>}< br/> else <br/> {<br/> checkedindexes. remove (INDEX); <br/> If (haschildren (INDEX) = true) <br/>{< br/> recursivecheck (index, value ); <br/>}< br/> emit datachanged (index, index); <br/> return true; <br/>}< br/> return qdirmodel: setdata (index, value, role); <br/>}< br/> bool cfilesystemmodel :: recursivecheck (const qmodelindex & Index, const qvariant & Value) <br/>{< br/> If (haschildren (INDEX) <br/>{< br/> int I; <br/> int childrencount = rowcount (INDEX); <br/> qmodelindex child; <br/> for (I = 0; I <childrencount; I ++) <br/>{< br/> child = qdirmodel: index (I, 0, index); <br/> setdata (child, value, QT: checkstaterole ); <br/>}< br/>