Search code examples
c++qtqmlqt5qvariant

How to return QVariant type array


I am recently creating a model for qml with c++, but I face a problem when returning a QVariant type empty array. How should I define my return statement?

switch (role) {
case NameRole:
    return QVariant(QStringLiteral("AAAAA"));
case LevelRole:
    return QVariant(QStringLiteral("1"));
case ParentRole:
    return QVariant(QStringLiteral("null"));
case SublevelRole:
    return ???// I would like to return an empty array
}

Solution

  • Use QVariantList:

    switch (role) {
    case NameRole:
        return QVariant(QStringLiteral("AAAAA"));
    case LevelRole:
        return QVariant(QStringLiteral("1"));
    case ParentRole:
        return QVariant(QStringLiteral("null"));
    case SublevelRole:
        return QVariantList();
    }