2

我有以下内容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> UeTypeLoggedUsersQML :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();
}

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

错误:“staticMetaObject”不是“QMultiMap”的成员

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

4

1 回答 1

2

您可以通过Qt 属性系统直接使用QVariantMap。它被声明为。QMLtypedef 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

于 2015-10-30T11:25:43.687 回答