Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to load the XmlListModel successfully?
Qt 6.11 is out! See what's new in the release blog

How to load the XmlListModel successfully?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 438 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • a.burksA Offline
    a.burksA Offline
    a.burks
    wrote on last edited by aha_1980
    #1

    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?

    a.burksA 1 Reply Last reply
    0
    • a.burksA a.burks

      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?

      a.burksA Offline
      a.burksA Offline
      a.burks
      wrote on last edited by a.burks
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved