[SOLVED] receiving list from c++ to qml listview
how to take a list from c++ to qml? in this case for example, i need to obtain the list from file.cpp:
- Component { id: delegate_grafica; Rectangle { width: 300; height: 30; color: "orange"; border.width: 1; border.color: "yellow"; Text { color: "black"; text: deutschesWort; font.pixelSize: 15; anchors.centerIn: parent } } }
- ListView { id: list_parolaSalvate; x: 0; y: 0; width: 300; height: 300; delegate: delegate_grafica; }
e il “model”???
12 replies
Please add some new lines to your code.
Expose a QAbstractListModel subclass as a context property or by using a Q_INVOAKABLE method.
Other options are using a QStringList or a QList<QObject> as the model.
Remember to register your QAbstractListModel subclass if you’ll not be exposing it via context property.
qmlRegisterUncreatableType/qmlRegisterType
i will use “Other options are using a QStringList or a QList<QObject> as the model.”, but the problem is: how to pass this qlist to this QML file?
main.qml
- Rectangle {
- width: 500; height: 500; signal emitsignal();
- Component { id: delegate_grafica; Rectangle { width: 300; height: 30; color: "orange"; border.width: 1; border.color: "yellow"; Text { color: "black"; text: deutschesWort; font.pixelSize: 15; anchors.centerIn: parent } } }
- ListView { id: list_parolaSalvate; x: 0; y: 0; width: 300; height: 300; delegate: delegate_grafica; model: emitsignal() }
- }
here i need a model, that i want to obtain from emitsignal, by which i would to obtain a qlist that is then the container of the model of my listview {}. my qlist will contain qlist<biglietto>
where biglietto is:
- int number = newnumber;
- bool isImportant = important;
- etc.
- }
however, thank yoU!
- QDeclarativeContext* context = declarativeView.rootContext();
- context->setContextProperty("mymodel",(QObject*)&myModel); // here myModel is the QObject that exposes your model
- ...
- }
In Qml
- ListView {
- ...
- model: myModel.dataModel();
- }
You need to write in the delegate the name of a public variable contained in the model, which you associate with QDeclarativeView.contextProperty.setContextProperty(nameOfTheModel, listThatyouhavemade). Note the you have to use first that I have just written and then set the source of the qml file where the model is needed.
I found that a simple
- modelData
You need to write in the delegate the name of a public variable contained in the model, which you associate with QDeclarativeView.contextProperty.setContextProperty(nameOfTheModel, listThatyouhavemade). Note the you have to use first that I have just written and then set the source of the qml file where the model is needed.
You must log in to post a reply. Not a member yet? Register here!



