Use QDirModel/QFileSystemModel + QTreeView in Qt to display the Checkbox check box

Source: Internet
Author: User

 

 

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/> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.