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. [SOLVED]Returning a QList<QHash<QString, QString> > to Qml ???
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Returning a QList<QHash<QString, QString> > to Qml ???

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 6.2k 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.
  • T Offline
    T Offline
    Tory Gaurnier
    wrote on last edited by
    #1

    I have a Q_INVOKABLE method that returns a QList<QHash<QString, QString> > when called from Qml, however when I run the code it throws "Error: Unknown method return type: QList<QHash<QString,QString> >", I'm assuming this is because Qml doesn't have support for QHash? Because I was able to return a QList<QString> or QStringList. So, how would I do this?

    The reason I want to do this is I want to pass a struct I created, the best way I've found is to convert it to the QList in question, if anyone can think of a better way of passing a whole struct please do tell. The struct is in this format:
    @
    struct mySubStruct {
    char *name;
    char *value;
    struct mySubStruct *next;
    struct mySubStruct *prev;
    }
    struct myStruct {
    mySubStruct data1;
    mySubStruct data2;
    mySubStruct data3;
    etc.;
    etc.;
    }
    @

    Now, keep in mind I use this struct all throughout my program, now what I'm doing is just passing the data from the struct to my Qml gui to be displayed, now from Qml I want to be able to access it sort of like this:
    @
    // This is in a "Component.onCompleted"
    var data_list = myImportedClass.getDataList();
    for(var i = 0; i < data_list.length; i++) {
    myModel.append({
    item1: data_list[i]["name1"],
    item2: data_list[i]["name2"],
    etc.,
    etc.
    });
    }
    @

    or like this:

    @
    // This is in a "Component.onCompleted"
    var data_list = myImportedClass.getDataList();
    for(var i = 0; i < data_list.length; i++) {
    myModel.append({
    item1: data_list[i].name1,
    item2: data_list[i].name2,
    etc.,
    etc.
    });
    }
    @

    I hope I provided enough info, and thanks in advance for any help.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      blam
      wrote on last edited by
      #2

      What if you try a QList<QVariantMap> instead? QML has built-in support for QVariantMap.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chrisadams
        wrote on last edited by
        #3

        Unfortunately, this won't work. You should be able to get something similar working, by returning a QVariantList where each element is a QVariantMap containing just a single key/value pair - in this case, the QVariantList is converted to a JS array and each element will be a JS object. You could alternatively define QObject types to expose the members of your struct, and use a QDeclarativeListProperty - but I don't recommend that, obviously, as performance would be hideous and it's likely overkill for what you want to do.

        Cheers,
        Chris.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tory Gaurnier
          wrote on last edited by
          #4

          Thank you, it works wonderfully! QVariantList with QVariantMap is exactly what I was looking for :D I just wish I could mark a question as answered, and vote up answers and stuff...

          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