gpt4 book ai didi

c++ - 将 QMultiMap 导出到 QML

转载 作者:行者123 更新时间:2023-11-30 05:36:50 25 4
gpt4 key购买 nike

我关注 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

我正在尝试公开/导出 typedef QMultiMap<QString, QString> UeTypeLoggedUsers通过 qmlRegisterType 到 QML在 ma​​in.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();
}

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

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

为什么会出现此错误以及如何消除它?

最佳答案

你可以使用 QVariantMap直接在QML通过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 .

关于c++ - 将 QMultiMap<QString, QString> 导出到 QML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33432816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com