Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to load the XmlListModel successfully?



  • 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 the GridView. It's only working I remove the line in the Java code, where the label is loaded.

    Has someone an idea?



  • I think the reason of the missing data was an impact of an error in another component or in the simulator. I could successfully test my code with a physical device. I have updated the component, that has created the XML data.



  • I think the reason of the missing data was an impact of an error in another component or in the simulator. I could successfully test my code with a physical device. I have updated the component, that has created the XML data.


Log in to reply