[Solved] return QModelIndex from c++ to qml
-
Hi,
I wants to do a file explorer in qml. I use VisualDataModel, ListView, and I have a model derived from QFileSystemModel.
It works fine, I can go from folder to folder in qml. But now I wants to be able to set from qml the root folder to show in qml :I have add a function like this in my model derived from QfilesystemModel :
@
Q_INVOKABLE QModelIndex FileSystemModel::indexFromPath(const QString & path, int column) const
{
return index(path,column);
}
@but It don't works ...
1)- It seems that I can't send directly a QModelIndex to qml. Is it the reason why it don't works ? or is it a bug in VisualDataModel when you set rootitem ?
2)- I am trying to convert QModelIndex to Qvariant in order to send a QVariant but it don't compile : I have tried with return QVariant::fromValue<QModelIndex>(index(path,column));
-
Solved :)
I have add Q_DECLARE_METATYPE(QModelIndex) in my headerand I have now :
@
QVariant FileSystemModel::indexFromPath(const QString & path, int column)
{
setRootPath(path);
return QVariant::fromValue<QModelIndex>(index(path,column));
}
@[EDIT: code formatting fixed, Volker]
-
Hi,
If you haven't already seen it, you might also be interested in checking out the FolderListModel element in the Qt.labs namespace (http://doc.qt.nokia.com/4.7-snapshot/qml-folderlistmodel.html).
Regards,
Michael