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



  • 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.



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



  • 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.



  • 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...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.