Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to wait for QFileSystemModel so that rowCount() != 0
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 6.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yafes
    wrote on last edited by
    #1

    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);@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yafes
        wrote on last edited by
        #3

        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 &)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            yafes
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Can you show again your connect statement ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Y Offline
                Y Offline
                yafes
                wrote on last edited by
                #7

                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)));@

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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 ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • Y Offline
                      Y Offline
                      yafes
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved