I just started learning Qt. To be honest, some of confuses my a lot. I'm trying to load a predefined .csv table into a listview. I created a qml file with the simple layout of a textfield, a listview and a button to load a file. The file looks like this:
import QtQuick 2.7
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 1.2
ApplicationWindow {
id: applicationWindow
objectName: "App"
visible: true
width: 640
height: 480
title: qsTr("Rosinenpicker")
ColumnLayout {
id: columnLayout
anchors.rightMargin: 40
anchors.leftMargin: 40
anchors.bottomMargin: 40
anchors.topMargin: 40
anchors.fill: parent
TextField {
id: name
text: qsTr("Text Field")
Layout.preferredHeight: 50
Layout.fillWidth: true
}
ListView {
id: list
x: 0
y: 0
width: 110
height: 160
spacing: 0
boundsBehavior: Flickable.DragAndOvershootBounds
Layout.fillHeight: true
Layout.fillWidth: true
objectName: "listView"
model: guestModel
delegate: Item {
x: 5
width: 80
height: 40
Text {
text: model.modeldata.name
font.bold: true
anchors.verticalCenter: parent.verticalCenter
}
}
}
Button {
id: button
width: 50
text: qsTr("Open File")
Layout.preferredWidth: 100
Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
onClicked: fileDialog.visible = true;
}
FileDialog {
id: fileDialog
title: "Please choose a valid guestlist file"
objectName: "fileDialog"
nameFilters: ["Valid guestlist files (*.csv)"]
selectMultiple: false
signal fileSelected(url path)
onAccepted: fileSelected(fileDialog.fileUrl)
}
}
}
In my main file I create a CsvParser class a connect it to the fileSelected signal of the file dialog. I can parse the csv-File. But when I try to connect it to the listview, I'm lost.
The main file:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtDebug>
#include <QUrl>
#include <QQuickView>
#include "csvparser.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
auto root = engine.rootObjects().first();
auto fileDialog = root->findChild<QObject*>("fileDialog");
CsvParser parser(engine.rootContext());
QObject::connect(fileDialog, SIGNAL(fileSelected(QUrl)), &parser, SLOT(loadData(QUrl)));
return app.exec();
}
My parser looks like this:
#include "csvparser.h"
#include <QtDebug>
#include <QFile>
#include "guest.h"
CsvParser::CsvParser(QQmlContext *context)
{
this->context = context;
}
void CsvParser::loadData(QUrl path)
{
friendList.clear();
guestList.clear();
QFile file(path.toLocalFile());
file.open(QIODevice::ReadOnly);
// ignore first lines
for(int i=0; i<3; i++)
file.readLine();
while(!file.atEnd()) {
auto rowCells = file.readLine().split(',');
if(rowCells.size() != 6)
continue;
checkedAdd(friendList, rowCells[0], rowCells[1]);
checkedAdd(guestList, rowCells[3], rowCells[4]);
}
qDebug() << guestList.size();
auto qv = QVariant::fromValue(guestList);
context->setContextProperty("guestModel", qv);
}
void CsvParser::checkedAdd(QList<QObject*> &list, QString name, QString familyName)
{
if(name == "" && familyName == "")
return;
list.append(new Guest(name, familyName));
}
And the guest, which has only a name and a familyname looks like this:
#ifndef GUEST_H
#define GUEST_H
#include <QObject>
class Guest : public QObject
{
public:
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString fName READ fName WRITE setfName NOTIFY fNameChanged)
public:
Guest(QString name, QString fName);
QString name();
QString fName();
void setName(QString name);
void setfName(QString fName);
signals:
void nameChanged();
void fNameChanged();
private:
QString m_name, m_fName;
};
#endif // GUEST_H
I get the following output:
qrc:/main.qml:41: ReferenceError: guestModel is not defined
QFileInfo::absolutePath: Constructed with empty filename
2
qrc:/main.qml:49: TypeError: Cannot read property 'name' of undefined
qrc:/main.qml:49: TypeError: Cannot read property 'name' of undefined
What am I doing wrong? Any help is appreciated. Thanks!
guestModelis not defined, you need to define it. I would recommend usingQAbstractListModelfor that.