populate() not working Repeater
-
Hallo Everybody,
I want to visualize a hard sphere simulation. There for I work with Singleton and QAbstractListmodel. I got the spheres to be be drawn in a window, but I can't move them per mouseclick. The mouseclick is recognized by the QAbstractListmodel, but the populate() function does not give the new coordinates of the spheres and ellipses to the QML. The spheres and ellipses are drwan by a Repeater and DelegateChooser. The DelagateChooser must be, because I also want to draw squares later. How can I get the populate function to work?
This is my main function:
int main(int argc, char *argv[]) { tw_hardcoresystem::Hardcore_InitClass initClass(argc, argv); tw_hardcoresystem::System system; initClass.return_Hardcoresystem_single(&system); QGuiApplication app(argc, argv); tw_hardcoresystem::Hardcore_Entrymodel entryModel; entryModel.setSystem(&system); qmlRegisterSingletonInstance("hardcoresystem", 1, 0, "Hardcoreentrymodel", &entryModel); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/qt/qml/Main.qml"))); return app.exec(); }
And my QML file looks like this:
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.12 import QtQuick.Shapes 1.7 //import QtQuick.Layouts 2.2 import QtQml.Models 2.12 import Qt.labs.qmlmodels 1.0 import hardcoresystem 1.0 Window { id: appwindow visible: true width: Hardcoreentrymodel.returnWindowLx() height: Hardcoreentrymodel.returnWindowLy() color: "grey" Rectangle{ id: hardcoresystemrectangle anchors.left: appwindow.left anchors.top: appwindow.top width: appwindow.width height: appwindow.height color: appwindow.color MouseArea { anchors.fill: parent onClicked: Hardcoreentrymodel.doNewStep() } } Connections{ id: hardcoreentrymodel target: Hardcoreentrymodel function onNewStepDoing(){console.log("New Path doing ...\n")} function onNewStepReady(){console.log("New Path ready ...\n")} function onDataChanged(){console.log("DataChanged received")} } Item{ id: partilcesitem width: hardcoresystemrectangle.width height: hardcoresystemrectangle.height anchors.left: hardcoresystemrectangle.left anchors.top: hardcoresystemrectangle.top Repeater { id: partilcesreapeter model: Hardcoreentrymodel delegate: chooser DelegateChooser{ id: chooser role: "type" DelegateChoice{ roleValue: "sphere"; delegate: Item{ id: sphereimage width: 2.0*Number(model.semimajoraxis) height: 2.0*Number(model.semiminoraxis) x: Number(model.xcoord - model.semimajoraxis) y: Number(model.ycoord - model.semiminoraxis) Canvas { anchors.fill: parent onPaint: { var ctx = getContext('2d'); ctx.beginPath(); ctx.lineWidth = 1 ctx.strokeStyle = "black" ctx.fillStyle = model.color ctx.ellipse(x,y,width,height) ctx.stroke(); ctx.fill(); } } rotation: Number(model.angle) Component.onCompleted: console.log(model.iparticle,", sphere ",x,", ",y,", ",width,", ",height,", ", model.color) } } DelegateChoice{ roleValue: "ellipse"; delegate: Item{ id: ellipseimage width: 2.0*Number(model.semimajoraxis) height: 2.0*Number(model.semiminoraxis) x: Number(model.xcoord - model.semimajoraxis) y: Number(model.ycoord - model.semiminoraxis) Canvas { anchors.fill: parent onPaint: { var ctx = getContext('2d'); ctx.beginPath(); ctx.lineWidth = 1 ctx.strokeStyle = "black" ctx.fillStyle = model.color ctx.ellipse(x,y,width,height) ctx.stroke(); ctx.fill(); } } rotation: Number(model.angle) Component.onCompleted: console.log(model.iparticle,", ellipse ",x,", ",y,", ",width,", ",height,", ", model.color) } } } } } } }
Kind regards,
Tobias -
According to Repeater Documentation you should use
modelData
if model is string list or object list. Try something like this.... DelegateChooser{ id: chooser role: "type" required property string modelData ... y: Number(modelData.ycoord - modelData.semiminoraxis) ...
-
What does your
poppulate
function do? Is it called byHardcoreentrymodel.doNewStep()
? -
@MIlan-Radosavljevic said in populate() not working Repeater:
required property string modelData
Dear Mllan,
this leads to this errors:
qrc:/qt/qml/Main.qml:79:25: TypeError: Cannot read property 'ycoord' of undefined
qrc:/qt/qml/Main.qml:78:25: TypeError: Cannot read property 'xcoord' of undefined
qrc:/qt/qml/Main.qml:77:25: TypeError: Cannot read property 'semiminoraxis' of undefined
qrc:/qt/qml/Main.qml:76:25: TypeError: Cannot read property 'semimajoraxis' of undefined
qml: 0 , sphere 0 , 0 , 0 , 0 , blueand finally to
QQmlApplicationEngine failed to create component
qrc:/qt/qml/Main.qml:71:17: Required property modelData was not initializedHow can I iniatialize the property modelData?
Tobias
-
@GrecKo Hello GrecKo,
This is my populate function of Entrymodel. It is called by doNewStep():
void tw_hardcoresystem::Hardcore_Entrymodel::populate() { int ipart; for(ipart=0; ipart<nparticles; ipart++){ mDatas[ipart]= initqtview.SystemToWindow(hardcoresystem->returnIParticle(ipart)); } emit dataChanged(index(0),index(nparticles)); return; }
The function should be OK, because the particles are drawn at first.
Tobias
-
This Weekend I made a linux distribution update with updated Qt library and updated QtCreator. Now I get a new error when building the sources - I had to change the cmake file:
Failed to import hardcoresystem. Are your import paths set up properly? [import]
and
Warnings occurred while importing module "hardcoresystem": [import]
in cmake I have now (Main2.qml is another testing file:
qt_add_qml_module(simHardcoreQt6 URI qt/qml VERSION 1.0 QML_FILES Main.qml Main2.qml )
and in the main.cpp it is still:
[...] tw_hardcoresystem::Hardcore_Entrymodel entryModel; entryModel.setSystem(&system); qmlRegisterSingletonInstance("hardcoresystem", 1, 0, "Hardcoreentrymodel", &entryModel); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/qt/qml/Main.qml"))); return app.exec();
Is there a problem where the qml files are situated? The Programm still compiles and the spheres are drawn at the right places at the beginning. Just the emit dataChanged(index(0),index(nparticles)) statement has no effect ...
Tobias
-
-
OK, the model has only one instance - I checked their this pointer. So that the emit dataChanged(index(0),index(nparticles)); will not update the particle coordinates may have another reason.
The model is not loaded well by the qml file. How can I avoid these Unqualified access [unqualified] errors:
Failed to import hardcoresystem. Are your import paths set up properly? [import]
In the cmake file:
qt_add_qml_module(simHardcoreQt6 URI qt/qml VERSION 1.0 QML_FILES Main.qml Main2.qml )
In the main.cpp:
tw_hardcoresystem::Hardcore_Entrymodel entryModel; entryModel.setSystem(&system); qmlRegisterSingletonInstance("hardcoresystem", 1, 0, "Hardcoreentrymodel", &entryModel); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/qt/qml/Main.qml")));
In the qml file:
[...] import hardcoresystem 1.0 [...]
-