View Full Version : Display Data from C++ to QML
Binary01
19th April 2016, 11:34
Hi EveryOne,
I have a C++ class that does some operations
The method : getDataFromServer() exports some Data from server, it works well
for (int i = 0; i < projects.size(); ++i)
{
qDebug() << "result : " << projects[i];
}
I want to display data in Listview QML
QML code
Rectangle
{
Component.onCompleted: {
ClassTest.getDataFromServer();
}
ClassTest{
id: classTest
}
Item {
width: 800
height: 600
ListView {
id: listView
anchors.left: parent.left
anchors.top: parent.top
anchors.bottom: parent.bottom
width: 0.25 * parent.width
focus: true
model: myModel
delegate: Item {
width: parent.width
height: 22
Text {
anchors.centerIn: parent
text: "data"
}
MouseArea {
anchors.fill: parent
onClicked: {
console.log(model.index)
}
}
}
}
}
getDataFromServer() code
...
// getData OK
for (int i = 0; i < projects.size(); ++i)
{
qDebug() << "result : " << projects[i];
}
QQuickView *view= new QQuickView;
QQmlContext *c = view->engine()->rootContext();
c->setContextProperty("myModel", QVariant::fromValue(projects));
Any help plz ;)
Cheers,
anda_skoa
19th April 2016, 11:43
Your getDataFromServer() creates a new view?
Cheers,
_
Binary01
19th April 2016, 11:59
The object is to display Data in QML
I created a new QML file code above , I would like to communicate C++ class and This QML file, so I tried to use :
QQuickView *view= new QQuickView;
QQmlContext *c = view->engine()->rootContext();
c->setContextProperty("myModel", QVariant::fromValue(projects));
Is this an other way simple to do that ??
anda_skoa
19th April 2016, 12:53
The object is to display Data in QML
If you create another view, it is another view.
In C++ two instance of a class are two different objects, with their owns state/data.
Is this an other way simple to do that ??
Either a property on an object exposed to QML or a model exposed to QML.
What's the element type of "projects"?
Cheers,
_
Binary01
19th April 2016, 14:44
projects is a QStringList : QStringList projects
Yeah I used a model exposed to QML : myModel
I tried to use the example : http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html
Cheers,
anda_skoa
19th April 2016, 15:30
projects is a QStringList : QStringList projects
Ah, good, easy :)
Yeah I used a model exposed to QML : myModel
Currently you are not, your non-working code exports the QStringList as a context property.
I assume "ClassTest" is a custom C++ type that you have registered with the QML type system?
Just add a QStringList property to it and bind the ListView's model to that instead of myModel.
ClassTest {
id: classTest
}
ListView {
model: classTest.projects // assuming you give your property the name "projects"
}
Cheers,
_
Binary01
19th April 2016, 16:41
Yeah, "ClassTest" is a custom C++ type registered with the QML type system
Is it necessary to add a new property ??
In ClassTest, I added projects
Q_PROPERTY(QStringList projects READ projects)
In QML
ListView {
model: classTest.projects
}
In C++
// getDataFromServer() == Get data From server
for (int i = 0; i < projects.size(); ++i)
{
qDebug() << "result : " << projects[i];
}
QQuickView *view= new QQuickView;
QQmlContext *ctxt = view->engine()->rootContext();
ctxt->setContextProperty("myModel", QVariant::fromValue(projects));
Still not work
C++ code should remain as such it is ??
Cheers,
anda_skoa
19th April 2016, 17:37
Yeah, "ClassTest" is a custom C++ type registered with the QML type system
Great, that makes everything easy.
Is it necessary to add a new property ??
That's the easiest way.
Do you need a more complicated approach?
In ClassTest, I added projects
Q_PROPERTY(QStringList projects READ projects)
Needs a NOTIFY signal, since the value is changing after object creation.
In QML
ListView {
model: classTest.projects
}
Exactly.
In C++
// getDataFromServer() == Get data From server
for (int i = 0; i < projects.size(); ++i)
{
qDebug() << "result : " << projects[i];
}
QQuickView *view= new QQuickView;
QQmlContext *ctxt = view->engine()->rootContext();
ctxt->setContextProperty("myModel", QVariant::fromValue(projects));
We've already determined that this is very wrong, no?
Once the value of the property has changed, you emit the change notifcation signal.
Cheers,
_
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.