Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] return QModelIndex from c++ to qml
Forum Updated to NodeBB v4.3 + New Features

[Solved] return QModelIndex from c++ to qml

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 6.6k 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.
  • Q Offline
    Q Offline
    qtnext
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qtnext
      wrote on last edited by
      #2

      Solved :)
      I have add Q_DECLARE_METATYPE(QModelIndex) in my header

      and 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]

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

        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

        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