将QMultiMap <qstring,qstring =“”>导出为QML

时间:2015-10-30 09:38:59

标签: c++ qt qml qmultimap

我关注QObject

#ifndef UETYPES
#define UETYPES

#include <QHash>
#include <QByteArray>
#include <QMultiMap>
#include <QString>
#include <QObject>

#include "../database/ueuserrecord.h"
#include "../database/ueorderrecord.h"

class UeTypes : public QObject
{
    Q_OBJECT

public:
    typedef QHash<int, QByteArray> UeTypeRoles;

    /*
     * QString  first parameter     userId
     * QString  second parameter    placeId
     */
    typedef QMultiMap<QString, QString> UeTypeLoggedUsers;
};

#endif // UETYPES

我正试图通过 main.cpp 中的typedef QMultiMap<QString, QString> UeTypeLoggedUsers向{QML}公开/导出qmlRegisterType

#include <QtQml>
#include <QApplication>
#include <QQmlApplicationEngine>

#include "core/uetypes.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QQmlApplicationEngine engine;

    qmlRegisterType<UeTypes::UeTypeLoggedUsers>("si.test",
                                                1,
                                                0,
                                                "UeTypeLoggedUsers");

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

但是,当我尝试编译此代码时,我收到以下错误:

  

错误:&#39; staticMetaObject&#39;不是QMultiMap&#39;

的成员

为什么我会收到此错误,如何摆脱它?

1 个答案:

答案 0 :(得分:1)

您可以直接在QMLQVariantMap中使用Qt property system。它被声明为typedef QMap<QString, QVariant>

class UeTypes : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QVariantMap map READ map WRITE setMap NOTIFY mapChanged)

public:
    QVariantMap map() const { return mMap; }
    void setMap(QVariantMap map) {
       if(mMap != map) {
           mMap = map;
           emit mapChanged();
       }
    }

signals:
    void mapChanged();

private:
    QVariantMap mMap;

};

您可以使用QMap::insertMulti多个相同的密钥插入QMap。它相当于QMultiMap::insert