[英]Export QFlags with QML (Qt 5) - errors with staticMetaObject
I've been trying to export some QFlags for use in QML (mainly, to read them from properties of an object exposed in QML) but even following this SO question I keep on getting compiler errors: 我一直在尝试导出一些QFlags用于QML(主要是从QML中公开的对象的属性中读取它们),但即使遵循这个SO问题,我仍然会遇到编译器错误:
#ifndef HOLIDAYACTIVITIES_H
#define HOLIDAYACTIVITIES_H
#include <QObject>
#include <QMetaType>
#include <QPair>
#include <QObject>
#include <QDateTime>
#include <QGeoLocation>
namespace HolidayPlanner
{
class Holiday: public QObject {
Q_OBJECT
Q_ENUMS(ActivityType)
Q_ENUMS(TransportType)
public:
explicit Holiday(QObject* parent = 0);
virtual ~Holiday();
enum ActivityType {
Transportation = 1,
Lodging = 2,
Sights = 4,
Note = 8,
Food = 16
};
enum TransportType {
Flight = 1,
Train = 2,
Subway = 4,
Bus = 8,
Foot = 16,
Other = 32,
None = 64
};
enum ItemRole {
TypeRole = Qt::UserRole + 1000,
StartDateRole = Qt::UserRole + 1001,
EndDateRole = Qt::UserRole + 1002,
StartPositionRole = Qt::UserRole + 1003,
EndPositionRole = Qt::UserRole + 1004,
TaskRole = Qt::UserRole + 1005,
};
Q_DECLARE_FLAGS(ActivityTypes, ActivityType)
Q_FLAGS(ActivityTypes)
Q_DECLARE_FLAGS(TransportTypes, TransportType)
Q_FLAGS(ActivityTypes)
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Holiday::ActivityTypes)
Q_DECLARE_OPERATORS_FOR_FLAGS(Holiday::TransportTypes)
}; // namespace HolidayPlanner
Q_DECLARE_METATYPE(HolidayPlanner::Holiday::ActivityType)
Q_DECLARE_METATYPE(HolidayPlanner::Holiday::TransportType)
But when I issue 但是当我发出
qmlRegisterType<HolidayPlanner::Holiday::ActivityType>();
anywhere in the code, I get 我得到了代码中的任何地方
In file included from /usr/include/qt5/QtQml/QtQml:9:0,
from /home/lb/Coding/cpp/holiday-planner/src/holidayactivities.cpp:21:
/usr/include/qt5/QtQml/qqml.h: In instantiation of ‘int qmlRegisterType() [with T =
HolidayPlanner::Holiday::ActivityType]’:
/home/lb/Coding/cpp/holiday-planner/src/holidayactivities.cpp:28:60: required from here
/usr/include/qt5/QtQml/qqml.h:109:5: error: ‘staticMetaObject’ is not a member of
‘HolidayPlanner::Holiday::ActivityType’
QML_GETTYPENAMES
^
/usr/include/qt5/QtQml/qqml.h:119:21: error: ‘staticMetaObject’ is not a member of
‘HolidayPlanner::Holiday::ActivityType’
0, 0, 0, 0, &T::staticMetaObject,
^
What could be possibly wrong here? 这可能有什么问题?
For reference, other classes like QLocale can export their enums to QML (not done by default, but it works), but I couldn't find any clue to why it does not work in my case. 作为参考,像QLocale这样的其他类可以将它们的枚举导出到QML(默认情况下没有完成,但它可以工作),但我找不到任何线索,为什么它在我的情况下不起作用。
You can't register enums as a type of qml. 您不能将枚举注册为qml类型。 The
qmlRegisterType<T>()
function is only for classes that are derived from QObject. qmlRegisterType<T>()
函数仅适用于从QObject派生的类。 AFAIK registering enumus is not required. AFAIK注册enumus不是必需的。 They are treated as normal ints.
他们被视为正常的整体。
To be able to pass all others types that do not derive from QObject
within a QVariant
to qml, use qRegisterMetaType<T>
. 为了能够将
QVariant
未从QObject
派生的所有其他类型传递给qml,请使用qRegisterMetaType<T>
。 You can't use them directly in qml, but you can pass to C++ slots, properties and invokable methods. 您不能直接在qml中使用它们,但可以传递给C ++槽,属性和可调用方法。
You are probably not doing this correctly. 你可能没有正确地做到这一点。 You need to register the class contaning the enumeration, an then it will be available automatically.
您需要注册包含枚举的类,然后它将自动可用。 Here is an example of how it usually goes:
这是一个通常如何的例子:
class MyClass : public QObject
{
Q_OBJECT
Q_ENUMS(MyEnum)
Q_PROPERTY(MyEnum myEnum READ myEnum)
public:
enum MyEnum {
Value1 = 0,
Value2
};
explicit MyObject(QObject *parent = 0);
MyEnum myEnum() const;
Q_INVOKABLE MyEnum getMyEnum() const;
};
qmlRegisterType<MyClass>("myclass", 1, 0, "MyClass");
To interpret this for your particular case, you would be writing something like this: 要根据您的特定情况对此进行解释,您将编写如下内容:
qmlRegisterType<Holiday>("holiday", 1, 0, "Holiday");
Then, you will access the enumeration values from QML like this: 然后,您将从QML访问枚举值,如下所示:
holiday.Transportation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.