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. Returning QFileSystemModel
Qt 6.11 is out! See what's new in the release blog

Returning QFileSystemModel

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 897 Views
  • 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.
  • NineswissN Offline
    NineswissN Offline
    Nineswiss
    wrote on last edited by
    #1

    I am trying to return a QAbstractItemModel from QFileSystemModel to use in my QML tree view. I'm not sure how to return anything. Ps I am new to C++. I don't even know if this is correct.

    QAbstractItemModel listFiles(const QString &filepath){
        QFileSystemModel model;
        model.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
        model.setRootPath(filepath);
        return model; // WHAT DO I RETURN HERE?
    }
    
    jsulmJ 1 Reply Last reply
    0
    • NineswissN Nineswiss

      I am trying to return a QAbstractItemModel from QFileSystemModel to use in my QML tree view. I'm not sure how to return anything. Ps I am new to C++. I don't even know if this is correct.

      QAbstractItemModel listFiles(const QString &filepath){
          QFileSystemModel model;
          model.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
          model.setRootPath(filepath);
          return model; // WHAT DO I RETURN HERE?
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Nineswiss said in Returning QFileSystemModel:

      // WHAT DO I RETURN HERE?

      In this code a copy of model.
      You can allocate your model on the heap (using new) and return pointer to it.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • NineswissN Offline
        NineswissN Offline
        Nineswiss
        wrote on last edited by
        #3

        Thanks @jsulm! I'm not really sure what that means but I will research.

        1 Reply Last reply
        0
        • NineswissN Offline
          NineswissN Offline
          Nineswiss
          wrote on last edited by
          #4

          Hmmm, I still can't seem to work it out :/

          jsulmJ 1 Reply Last reply
          0
          • NineswissN Nineswiss

            Hmmm, I still can't seem to work it out :/

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #5

            @Nineswiss said in Returning QFileSystemModel:

            I still can't seem to work it out :/

            What exactly? Please be more clear about what you are asking.
            In the code you posted you return model and return type of listFiles is QAbstractItemModel (by value) - that means that your model is copied. This is basic C++.
            If you do not want this copying then allocate on the heap using new (again: basic C++):

            QAbstractItemModel* listFiles(const QString &filepath){
                QFileSystemModel *model = new  QFileSystemModel();
                model->setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
                model->setRootPath(filepath);
                return model;
            }
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

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