Results 1 to 3 of 3

Thread: Exposing QModelIndex in C++ to QML VisualDataModel

  1. #1
    Join Date
    Apr 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Symbian S60

    Default Exposing QModelIndex in C++ to QML VisualDataModel

    I'm trying to implement a very basic file browser that only shows files in a set directory. Using the QFileSystemModel, I've set the root path but the connected QML VisualDataModel still shows the entire path. I have to keep clicking to get the directory I want ("/testfolder/test"). I tried using the Symbian FolderModel QML element but it's a bit buggy, it doesn't refresh properly when a file is added or deleted.

    I tried exporting the index of the root path to the VisualDataModel using a QModelIndex but I keep getting the following compile errors:
    ../../QtSDK/Simulator/Qt/gcc/include/QtCore/qvariant.h:433: error: 'QVariant::QVariant(void*)' is private
    ../qfiledemo/main.cpp:46: error: within this context
    I want to use the QModelIndex to set the VisualDataModel to jump directly to the path I want, rather than starting out at the top of the filesystem. Any way to expose a QVariant to QML without making a custom class? Thanks!

    Here is my main.cpp:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QDeclarativeView view;
    6.  
    7. QFileSystemModel model;
    8. model.setRootPath(QString("/testfolder/test"));
    9. QModelIndex rootidx = model.index("/testfolder/test"); // find the correct index to pass to VisualDataModel
    10.  
    11. view.rootContext()->setContextProperty("dirModel", &model);
    12. view.rootContext()->setContextProperty("rootIndex", &rootidx); // this always fails!
    13. view.setSource(QUrl::fromLocalFile("qml/qfiledemo/main.qml"));
    14. view.show();
    15.  
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    My main.qml:
    Qt Code:
    1. ListView {
    2. id: view
    3. width: 300
    4. height: 400
    5.  
    6. model: VisualDataModel {
    7. model: dirModel
    8.  
    9. delegate: Rectangle {
    10. width: 200; height: 25
    11. Text { text: filePath }
    12.  
    13. MouseArea {
    14. anchors.fill: parent
    15. onClicked: {
    16. if (model.hasModelChildren) {
    17. view.model.rootIndex = view.model.modelIndex(index)
    18. // Would be great if I could set the rootIndex in here to jump straight to the directory I want
    19.  
    20. }
    21. }
    22. }
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by hurmatt; 29th April 2012 at 10:28. Reason: updated contents

  2. #2
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Exposing QModelIndex in C++ to QML VisualDataModel

    I didn't test it but maybe you can subclass QFileSystemModel and add someting like Q_INVOKABLE QModelIndex root_index(const QString& path) const;
    and in qml do: view.model.rootIndex = dirmodel.root_index("/testfolder/test"); in a Component.onCompleted event

  3. #3
    Join Date
    Apr 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Symbian S60

    Default Re: Exposing QModelIndex in C++ to QML VisualDataModel

    I found something similar at http://qt-project.org/forums/viewthread/3306 where the poster used a custom subclass from QFileSystemModel and managed to get QModelIndex exposed to QML.

    I can't make heads or tails of the solution since my C++ is really rusty and I haven't used subclasses from Qt classes before Any documentation on doing this with Qt?

Similar Threads

  1. custom widget: exposing properties of subwidgets
    By snydesc in forum Qt Programming
    Replies: 0
    Last Post: 13th February 2012, 18:24
  2. Exposing a Qt container property to QtScript, READ and WRITE
    By SingleMalt in forum Qt Programming
    Replies: 6
    Last Post: 12th May 2011, 07:31
  3. Exposing a Q_ENUM to QML
    By aidanok in forum Qt Quick
    Replies: 1
    Last Post: 17th December 2010, 13:18
  4. Exposing custom widget properties
    By Plixil in forum Newbie
    Replies: 1
    Last Post: 25th July 2010, 16:38
  5. QModelIndex question
    By waynew in forum Qt Programming
    Replies: 2
    Last Post: 10th February 2010, 01:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.