如何将QMap从C ++公开到QML? 使用QList我可以使用QDeclarativeListProperty。
答案 0 :(得分:6)
您可以将其转换或更改为QVariantMap。在QML中,您可以将QVariantMap实例用作普通的Javascript对象。
class MyClass : public QObject {
Q_OBJECT
public:
Q_INVOKABLE QVariantMap getIntMap() {
QVariantMap rval;
foreach (QString key, m_intMap.keys()) {
// int has an implicit conversion to QVariant
rval[key] = m_intMap[key];
}
return rval;
}
Q_INVOKABLE QVariantMap getObjMap() {
QVariantMap rval;
foreach (QString key, m_objMap.keys()) {
// TODO: make sure all QObject subclasses are exported to QML
rval[key] = QVariant::fromValue<QObject*>(m_objMap[key]);
}
return rval;
}
private:
QMap<QString, int> m_intMap;
QMap<QString, QObject*> m_objMap;
}