PDA

View Full Version : QFileSystemModel and QML



enrico5th
5th June 2014, 15:08
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

wysota
5th June 2014, 15:26
Instantiate QApplication instead of QGuiApplication.

enrico5th
5th June 2014, 15:32
It works, but instead of showing the content of C:/Images it shows may laptop logical drives. Do you know why?

wysota
6th June 2014, 09:16
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.

enrico5th
18th June 2014, 09:55
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?

10428

enrico5th
18th June 2014, 15:23
Sorry I found the problem by myself. I made confusion with the use of x and y within child items.