Returning QFileSystemModel
-
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? }
-
@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. -
@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; }