Inspecting a qvariantlist in qml



  • Hi,

    I'm invoking a Q_INVOKABLE function from QML which returns a QVariantList. The QVariantList contains QVariants of QSizes, and I'm attempting to retrieve the QSize values from the list this way
    @
    var values = object.values();//a QVariantList is assigned to values
    var width = values[0].width;
    var height = values[0].height;
    @
    however the variables width and height are undefined. Please advise as to what I'm doing wrong and how I can rectify the problem.

    Thanks,
    Jasi

    [Edit: Added @ code tags; mlong]



  • Do you mean a QVariantList or a QVariantMap?

    A QVariantList is like this:
    @
    QVariantList list;
    list << 10 << Qt::green << "bottles";
    @
    In which case, you have not defined a 'width' or 'height'.

    In either case, the QVariants in QML work like JavaScript arrays.

    So, for example, to look through a QVariantMap you could do something like this:
    @
    for (var prop in anObject) {

             console.log("Object item:", prop, "=", anObject[prop])
    

    }
    @

    [Edit: Added @ tags. mlong]



  • values() returns a QVariantlist which was built by wrapping QSize objects in QVariant like this
    @
    Q_INVOKABLE QVariantList values() {
    QVariantList list;

    list.append(QVariant(QSize(100, 20)));
    list.append(QVariant(QSize(80, 25)));
    ....

    return list;
    }
    @
    This is why I'm using width and height in qml to get the size values. When I do the following
    @
    console.log(values[0]);
    @
    this is the output

    "QVariant(QSize)"

    and when I do this
    @
    console.log(values[0].width);
    @
    the output is

    "undefined"

    although the values are there when printed inside the values() function.

    [Again, please wrap your code in @ tags. mlong]



  • The documentation doesn't seem exactly clear on this, but I'm not sure you can do a values[ 0].width because width() is a method of QSize. In order to do what you want, QSize would have to have a Q_PROPERTY() associated with the getter and setter methods (i.e. width() and setWidth()) of QSize. I do not see anywhere in the documentation where there are any properties defined for QSize and width.

    However, there is hope. In terms of "Automatic Type Conversion from Strings". The docs say this is true of QSize:

    Type String Format Example
    QSize "WidthxHeight" "100x200"

    I think what you might want to try is not wrapping your QSize as a QVariant:
    @
    ...
    QVariantList list;
    list << QSize(100,200) << QSize(80,25);
    ..
    @
    Then you should get something like this, according to the documentation:
    @
    console.log(values[0]);
    @
    ouput should be:

    "100x200"

    [Edit: Added @ tags; mlong]



  • Hi,

    If we are wrapping an item with QVariant, then how can we access the correct values.

    Ansif



  • I have same problem. I have declared a custom struct (c type) and I want access trought QVariant:

    @
    typedef struct
    {
    int test;
    } CUSTOM_C_TYPE;
    @

    in qml, obtain undefined when try to access "test" variable...


Log in to reply
 

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