将 QMultiMap<QString, QString> 导出到 QML
exporting QMultiMap<QString, QString> to QML
我关注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:
中的 qmlRegisterType
expose/export typedef QMultiMap<QString, QString> UeTypeLoggedUsers
到 QML
#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();
}
但是,当我尝试编译这段代码时,出现以下错误:
error: 'staticMetaObject' is not a member of 'QMultiMap'
为什么会出现此错误以及如何消除它?
您可以使用 QVariantMap directly in QML
through 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. It's equivalent to QMultiMap::insert.
向 QMap 插入 多个相同的键
我关注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:
中的qmlRegisterType
expose/export typedef QMultiMap<QString, QString> UeTypeLoggedUsers
到 QML
#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();
}
但是,当我尝试编译这段代码时,出现以下错误:
error: 'staticMetaObject' is not a member of 'QMultiMap'
为什么会出现此错误以及如何消除它?
您可以使用 QVariantMap directly in QML
through 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. It's equivalent to QMultiMap::insert.
向 QMap 插入 多个相同的键