gpt4 book ai didi

c++ - 将 C++ 自定义类型公开给 QML

转载 作者:行者123 更新时间:2023-11-27 22:56:28 25 4
gpt4 key购买 nike

我有以下 C++ 自定义对象,希望在 QML 中用作 enum 类型:

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

#include "database/uepeoplemodel.h"
#include "core/uestatus.h"
#include "core/uedatabaseconnectionstatus.h"

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

UeStatus* ueApplicationStatus=new UeStatus(qApp);
UePeopleModel* uePeopleModel=new UePeopleModel(qApp);

QObject::connect(uePeopleModel,
SIGNAL(ueSignalDatabaseConnectionChanged(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus)),
ueApplicationStatus,
SLOT(ueSignalDatabaseConnectionChanged(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus)));

uePeopleModel->ueConnectToDatabase();

engine.rootContext()->setContextProperty("uePeopleModel",
uePeopleModel);
engine.rootContext()->setContextProperty("ueApplicationStatus",
ueApplicationStatus);
engine.addImageProvider(QLatin1String("uePeopleModel"),
uePeopleModel);

qmlRegisterUncreatableType<UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus>("si.mikroelektronika",
1,
0,
"UeTypeDatabaseConnectionStatus",
"Database Connection Status");

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

return app.exec();
}

我收到以下编译错误(在 Linux work002 3.19.0-28-generic#30-Ubuntu SMP Mon Aug 31 15:52:51 UTC 2015 上使用 gcc version 4.9.2 x86_64 x86_64 x86_64 GNU/Linux:

In file included from /opt/QtOpenSource55/5.5/gcc_64/include/QtCore/qobject.h:40:0, from /opt/QtOpenSource55/5.5/gcc_64/include/QtCore/qabstractanimation.h:37, from /opt/QtOpenSource55/5.5/gcc_64/include/QtCore/QtCore:4, from /opt/QtOpenSource55/5.5/gcc_64/include/QtQml/QtQmlDepends:3, from /opt/QtOpenSource55/5.5/gcc_64/include/QtQml/QtQml:3, from ../ueBlagajnaClient/main.cpp:1: /opt/QtOpenSource55/5.5/gcc_64/include/QtCore/qobjectdefs.h:88:5: error: class definition may not be declared a friend friend Q_DECL_CONSTEXPR const QMetaObject *qt_getEnumMetaObject(ENUM) Q_DECL_NOEXCEPT { return &staticMetaObject; } \ ^ ../ueBlagajnaClient/database/../core/uedatabaseconnectionstatus.h:17:5: note: in expansion of macro 'Q_ENUM' Q_ENUM(UeTypeDatabaseConnectionStatus) ^ /opt/QtOpenSource55/5.5/gcc_64/include/QtCore/qobjectdefs.h:88:35: error: 'QMetaObject' is neither function nor member function; cannot be declared friend friend Q_DECL_CONSTEXPR const QMetaObject *qt_getEnumMetaObject(ENUM) Q_DECL_NOEXCEPT { return &staticMetaObject; } \ ^ ../ueBlagajnaClient/database/../core/uedatabaseconnectionstatus.h:17:5: note: in expansion of macro 'Q_ENUM' Q_ENUM(UeTypeDatabaseConnectionStatus) ^ /opt/QtOpenSource55/5.5/gcc_64/include/QtCore/qobjectdefs.h:88:35: error: expected ';' at end of member declaration friend Q_DECL_CONSTEXPR const QMetaObject *qt_getEnumMetaObject(ENUM) Q_DECL_NOEXCEPT { return &staticMetaObject; } \ ^ ../ueBlagajnaClient/database/../core/uedatabaseconnectionstatus.h:17:5: note: in expansion of macro 'Q_ENUM' Q_ENUM(UeTypeDatabaseConnectionStatus) ^ /opt/QtOpenSource55/5.5/gcc_64/include/QtCore/qobjectdefs.h:88:73: error: ISO C++ forbids declaration of 'qt_getEnumMetaObject' with no type [-fpermissive] friend Q_DECL_CONSTEXPR const QMetaObject qt_getEnumMetaObject(ENUM) Q_DECL_NOEXCEPT { return &staticMetaObject; } \ ^ ../ueBlagajnaClient/database/../core/uedatabaseconnectionstatus.h:17:5: note: in expansion of macro 'Q_ENUM' Q_ENUM(UeTypeDatabaseConnectionStatus) ^ ../ueBlagajnaClient/database/../core/uedatabaseconnectionstatus.h: In member function 'int UeDatabaseConnectionStatus::qt_getEnumMetaObject(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus)': /opt/QtOpenSource55/5.5/gcc_64/include/QtCore/qobjectdefs.h:88:101: error: cannot convert 'const QMetaObject*' to 'int*' in return friend Q_DECL_CONSTEXPR const QMetaObject qt_getEnumMetaObject(ENUM) Q_DECL_NOEXCEPT { return &staticMetaObject; } \ ^ ../ueBlagajnaClient/database/../core/uedatabaseconnectionstatus.h:17:5: note: in expansion of macro 'Q_ENUM' Q_ENUM(UeTypeDatabaseConnectionStatus) ^ In file included from /opt/QtOpenSource55/5.5/gcc_64/include/QtQml/QtQml:9:0, from ../ueBlagajnaClient/main.cpp:1: /opt/QtOpenSource55/5.5/gcc_64/include/QtQml/qqml.h: In instantiation of 'int qmlRegisterUncreatableType(const char, int, int, const char*, const QString&) [with T = UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus]': ../ueBlagajnaClient/main.cpp:35:120: required from here /opt/QtOpenSource55/5.5/gcc_64/include/QtQml/qqml.h:83:59: error: 'staticMetaObject' is not a member of 'UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus' const char *className = T::staticMetaObject.className(); \ ^ /opt/QtOpenSource55/5.5/gcc_64/include/QtQml/qqml.h:134:5: note: in expansion of macro 'QML_GETTYPENAMES' QML_GETTYPENAMES ^ In file included from /opt/QtOpenSource55/5.5/gcc_64/include/QtQml/QtQml:9:0, from ../ueBlagajnaClient/main.cpp:1: /opt/QtOpenSource55/5.5/gcc_64/include/QtQml/qqml.h:144:51: error: 'staticMetaObject' is not a member of 'UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus' uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject, ^ Makefile:1319: recipe for target 'main.o' failed make: *** [main.o] Error 1 08:04:15: The process "/usr/bin/make" exited with code 2. Error while building/deploying project ueBlagajnaClient (kit: Desktop Qt 5.5.0 GCC 64bit) When executing step "Make"

为什么以及如何摆脱这个错误?我如何滥用方法 qmlRegisterUncreatableType

最佳答案

如果要使用qmlRegisterType,使用的类必须是继承QObject并且有Q_OBJECT宏的类:

class UeTypeDatabaseConnectionStatus: public QObject
{
Q_OBJECT

public:
enum StatusEnum
{
NOT_CONNECTED=false,
CONNECTED=true
}
Q_ENUM(StatusEnum)
...
}

在 QML 中,您可以创建它的一个实例,就像您创建任何其他 QML 类一样。

编辑:但是,由于您只想使用枚举值,因此请改用 qmlRegisterUncreatableType。通过这种方式注册类型,但您无法创建它的实例。

要访问枚举值,导入类型并像使用任何其他枚举一样使用它:

connectionProperty: UeTypeDatabaseConnectionStatus.CONNECTED;

关于c++ - 将 C++ 自定义类型公开给 QML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32564045/

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