exporting QMultiMap<QString, QString> to QML

2.3k Views Asked by At

I have following 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

and I am trying to expose/export typedef QMultiMap<QString, QString> UeTypeLoggedUsers to QML via qmlRegisterType in main.cpp:

#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();
}

However, when I try to compile this code, I get following error(s):

error: 'staticMetaObject' is not a member of 'QMultiMap'

Why am I getting this error and how do I get rid of it?

1

There are 1 best solutions below

0
On BEST ANSWER

You could use QVariantMap directly in QML through Qt property system. It is declared as 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;

};

You can insert multiple identical keys to QMap with QMap::insertMulti. It's equivalent to QMultiMap::insert.