Results 1 to 6 of 6

Thread: QFileSystemModel and QML

  1. #1
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default QFileSystemModel and QML

    Hi,

    I'm writing a QML application in which I would like to use a ListView to show a list of files. I tried to use as model for my ListView the QFileSystemModel as it is shown in the following code samples:

    main.cpp
    #include <QQmlContext>
    #include <QtGui/QGuiApplication>
    #include <QDir>
    #include <QFileSystemModel>
    #include "qtquick2applicationviewer.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QFileSystemModel* model = new QFileSystemModel();
    model->setFilter(QDir::NoDotAndDotDot | QDir::Files);
    model->setRootPath("C:/Images");

    QtQuick2ApplicationViewer viewer;

    viewer.rootContext()->setContextProperty("myFileModel", model);

    viewer.setMainQmlFile(QStringLiteral("qml/ProvaQML/main.qml"));
    viewer.showExpanded();

    return app.exec();
    }
    main.qml
    import QtQuick 2.0

    Item
    {
    id: main
    width: 1024
    height: 768

    FileList
    {
    x_start: parent.x
    y_start: parent.y
    }
    }
    FileList.qml
    import QtQuick 2.2

    ListView
    {
    property int x_start: 0
    property int y_start: 0

    x: x_start + 20
    y: y_start + 100

    width: 210
    height: 290

    model: myFileModel

    delegate: Rectangle{
    width: 210; height: 20; radius: 5; border.width: 2; border.color: "orange"; color: "yellow";
    Text { text: fileName; anchors.centerIn: parent }
    }
    }
    Unfortunatly when I try to run my application I receive a Segmentation Fault error and in my QML/JS Console i read:
    ASSERT: "!"No style available without QApplication!"" in file kernel\qapplication.cpp, line 962 global\qglobal.cpp: 2096
    Does anyone know which is the problem and how to fix it?

    Thanks

    Enrico

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFileSystemModel and QML

    Instantiate QApplication instead of QGuiApplication.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QFileSystemModel and QML

    It works, but instead of showing the content of C:/Images it shows may laptop logical drives. Do you know why?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFileSystemModel and QML

    Most likely the root path is not set correctly or you are using the model too early -- before it has a chance to update itself with new data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QFileSystemModel and QML

    Hi. I found a way to use a C++ model for my ListView object and I started developing a little application that shows two different file browsers at the same time, but I'm getting a graphical error when I display the second file browser. In fact both the file browsers should be displayed inside a BorderImage, but the second one falls outside of it's BorderImage and I just cannot understand why. I'm adding the project to this post, can anybody tell me what's wrong with it?

    Prova_QML.zip

  6. #6
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QFileSystemModel and QML

    Sorry I found the problem by myself. I made confusion with the use of x and y within child items.

Similar Threads

  1. QFileSystemModel and QTableView
    By jesse_mark in forum Qt Programming
    Replies: 0
    Last Post: 22nd March 2013, 18:01
  2. Failing to use QFileSystemModel in Qt
    By owais_blore in forum Newbie
    Replies: 1
    Last Post: 16th November 2012, 14:50
  3. Proxy for the QFileSystemModel
    By stereoMatching in forum Newbie
    Replies: 0
    Last Post: 26th July 2012, 12:26
  4. Help with QFileSystemModel
    By TheShow in forum Qt Programming
    Replies: 4
    Last Post: 5th January 2010, 20:11
  5. QFileSystemModel has no sorting
    By mpi in forum Qt Programming
    Replies: 3
    Last Post: 28th May 2009, 08:14

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.