Populating a listView in qml from c++?
-
wrote on 3 Feb 2014, 21:00 last edited by
I have a main.qml file that I want to be able to control what is in that list from c++
I don't even know where to begin but I do have a static list with a few things in it in my qml code.
My c++ looks like this
@
#include <QApplication>
#include "qmlapplicationviewer.h"Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));QmlApplicationViewer viewer; viewer.addImportPath(QLatin1String("modules")); viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); viewer.setMainQmlFile(QLatin1String("qml/Testing_List_Stuff/main.qml")); viewer.showExpanded(); return app->exec();
}
@
and my qml list stuff looks like this
@import QtQuick 1.1Rectangle {
width: 360
height: 360ListModel { id: fruitModel ListElement { name: "Apple" cost: 2.45 } ListElement { name: "Orange" cost: 3.25 } ListElement { name: "Banana" cost: 1.95 } } ListView { anchors.fill: parent model: fruitModel delegate: Row { Text { text: "Fruit: " + name } Text { text: "Cost: $" + cost } } }
}
@as you can see my c++ stuff only loads the qml and then my qml contains the static list.
How can I make a list from c++? -
wrote on 4 Feb 2014, 07:06 last edited by
You should use C++ model (doc.
https://qt-project.org/doc/qt-5.1/qtquick/qtquick-modelviewsdata-cppmodels.html )
1/2