How to wait for QFileSystemModel so that rowCount() != 0



  • Hi,

    From the Documentation:
    Unlike QDirModel, QFileSystemModel uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Calls to rowCount() will return 0 until the model populates a directory.

    Question
    What is the right way to wait for QFileSystemModel to finish it's procedure?

    @ QFileSystemModel *fileModel;
    QModelIndex parentIndex = fileModel->index(path);
    int numRows = fileModel->rowCount(parentIndex);@


  • Lifetime Qt Champion

    Hi,

    The "directoryLoaded":http://doc.qt.io/qt-5/qfilesystemmodel.html#directoryLoaded signal might be a good starting point

    Hope it helps



  • Hi SGaist,

    i think i am accessing the signal in a wrong way,

    In Widget.cpp:

    @
    dirModel = new MyQFileSystemModel;
    connect(dirModel, SIGNAL(​directoryLoaded(const QString &)), this, SLOT(toggled(const QString&)));@

    in Widget.h:
    @signals:
    void directoryLoaded(const QString &path);

    public slots:
    void toggled(const QString &path);@

    Am i doing something wrong?
    Error: No such signal QFileSystemModel::?directoryLoaded(const QString &)


  • Lifetime Qt Champion

    Yes you do, your SIGNAL and SLOT statements are wrong.

    @
    connect(dirModel, SIGNAL(​directoryLoaded(QString)), this, SLOT(toggled(QString)));
    @

    Out of curiosity, why the directoryLoaded in Widget.h ?



  • Hi SGaist, still does not work
    QObject::connect: No such signal QFileSystemModel::?directoryLoaded(QString) in ..\AssetBrowser_02\widget.cpp:126

    Out of curiosity, why the directoryLoaded in Widget.h ?

    Do you mean in terms of Programm-Design? I am prototyping in widget.cpp and encapsulating it later in the appropriate classes.


  • Lifetime Qt Champion

    Looks like you implement a custom MyQFileSystemModel, how does it look like ?

    Can you show again your connect statement ?



  • SGaist,

    i just have overwritten the data in my custom MyQFileSystemModel:
    @class MyQFileSystemModel : public QFileSystemModel {
    public:
    QVariant data( const QModelIndex& index, int role ) const {

        if( role == Qt::DecorationRole ) {
            return QVariant(QIcon(QPixmap(":/myresources/images/folder-icon.png")));
        }
    
        return QFileSystemModel::data(index, role);
    }
    

    };@

    My Connect Statement:
    @connect(dirModel, SIGNAL(​directoryLoaded(QString)), this, SLOT(toggled(QString)));@


  • Lifetime Qt Champion

    You can also use a QIdentityProxyModel for such customization. That allows to reuse the same QFileSystemModel with several views (if you need such a thing)

    What version of Qt are you using ?



  • I think SGaist's solution is the better one. Furtheremore, your subclass is not complete: no Q_OBJECT macro, no constructor, etc.



  • I am using version 5.4. The constructor should be derived from the base-class. Can the macro be derived as well? I don't know whats happening deep inside QT.

    Ok now with the proxy model i get a runtime debug message:
    QAbstractItemView::setRootIndex failed : index must be from the currently set model

    I was setting the root Index like this:
    ui->treeView->setRootIndex(dirModel->setRootPath(Globals::filePath));

    I don't know if the error is coming from this part of the code, but seems to be, as i am only setting the root Index here. So how can i set now the index? The Proxy Model has no methods for that.

    Thanks in advance


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.