Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 33
Registered: ‎08-28-2013
My Device: non-BlackBerry user
My Carrier: TURKCELL
Accepted Solution

QList<QString> values from cpp return as "undefined" in QML

Hi there. I have a quite simple question. In my qml file I am calling an invokable function from applicationui.cpp file which is below:

QList<QString> ApplicationUI::getList() {
    qDebug() << myMap.keys(); // prints ("key1", "key2")
    return myMap.keys();
}

Within my QML(sheet) file I write the code below:

    onOpened: {
        var myKeys = app.getList();
        console.log(myKeys); // prints undefined
        for (var item in myKeys){
            myDataModel.append(myKeys[item]);
        }
    }

 As you can see in the code, when program comes to qDebug expression it prints ("key1", "key2") but it comes to console.log(myKeys) expression, it prints "undefined". I am definitely doing something wrong. Could you please help???

Developer
Posts: 47
Registered: ‎05-31-2013
My Device: Z10, Passport

Re: QList<QString> values from cpp return as "undefined" in QML

Try to return QList<QVariant> instead. That should make qml happy Smiley Happy

Developer
Posts: 33
Registered: ‎08-28-2013
My Device: non-BlackBerry user
My Carrier: TURKCELL

Re: QList<QString> values from cpp return as "undefined" in QML

Man I love you...

Quick question (bonus Smiley Very Happy), do you know a faster way to convert QList<QString> to QList<QVariant> instead of using a for loop to add values one by one?

Developer
Posts: 47
Registered: ‎05-31-2013
My Device: Z10, Passport

Re: QList<QString> values from cpp return as "undefined" in QML

If you are going to actualy measure operations performance mark significant events with 

QDateTime::currentMSecsSinceEpoch()

 and compare performance.

However I think this is not actually what you need. Try below.

QVariant ApplicationUI::getList() {
    QStringList myKeysQsl;
    QMap<QString, QVariant> myMap;
    //myMap ...
    myMap.insert("key1", 12312);
    myMap.insert("key2", "dgh shd");
    myMap.insert("key3", 10);

    myKeysQsl = myMap.keys();
    return myKeysQsl;
}

 

Q_INVOKABLE QVariant getList();

 

var myStringList = cpp.getList();
console.log(myStringList, typeof myStringList, myStringList.length, myStringList[0], myStringList[1]);

 You can then use the result as a regular javascript array (object). Hope it helps.