How to load the XmlListModel successfully?
Solved
QML and Qt Quick
-
My app retrieves installed Android apps for a launcher in a Java class, that passes an xml string to a c++ class and the last one to a QML file. The XML string is correct, and the model seem to be loaded but no list entries are added.
Here is the code snipped of the Java class:
public static String getApplist(Activity a){ String list="<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>"; final PackageManager pm = a.getPackageManager(); Intent i = new Intent(Intent.ACTION_MAIN, null); i.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> availableActivities = pm.queryIntentActivities(i, 0); for (ResolveInfo ri:availableActivities) { list+="<item>"; list+="<name>"+ri.activityInfo.packageName+"</name>"; list+="<label>"+String.valueOf(ri.loadLabel(pm))+"</label>"; list+="</item>"; } list+="</root>"; return list; }
The value returned to the C++ class:
QString Launcher::getApplist(){ QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;"); QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("com/myapp/launcher/worker/AppWorker", "getApplist", "(Landroid/app/Activity;)Ljava/lang/String;", activity.object<jobject>()); return str.toString(); }
And here is the QML snippet:
XmlListModel { id: xmlModel xml: launcher.getApplist() query: "/root/item" XmlRole { name: "package" query: "name/string()" } XmlRole { name: "label" query: "label/string()" } onCountChanged: { console.log("Cound changed: " + count) } onStatusChanged: { switch (status) { case XmlListModel.Null: console.log("Status changed to null") break case XmlListModel.Ready: console.log("Status changed to ready") ... break case XmlListModel.Loading: console.log("Status changed to loading") break case XmlListModel.Error: console.log("Status changed to error: " + errorString()) break default: console.log("Status changed to anything else") } } }
In the log I can see, that the model is loading and then ready. But
onCountChange()
is never reached and the model is not working for theGridView
. It's only working I remove the line in the Java code, where the label is loaded.Has someone an idea?
-