简体   繁体   English

使用QML导出QFlags(Qt 5) - 使用staticMetaObject出错

[英]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.

相关问题 我需要整合我的 c++ 代码和 qml。 但是我在使用 qmlRegisterType 时遇到了许多错误“未定义对‘Middlemen::staticMetaObject’的引用” - I need to Intergate my c++ code and qml. But I get errors "undefined reference to `Middlemen::staticMetaObject'" among many on using qmlRegisterType QT5在staticMetaObject函数外部未解决 - Qt5 unresolved external to staticMetaObject function Qt无法加载插件:未定义符号iPlugin :: staticMetaObject - Qt Cannot load plugin : undefined symbol iPlugin::staticMetaObject QT5.5中用于qDebug()的新QFlags助手的问题 - Problems with new QFlags helpers for qDebug() in QT5.5 qt QFlags:有符号值超出枚举常量的范围 - qt QFlags: signed value is out of range for enum constant 未定义的对&#39;vtable for DigitalClock&#39;的引用 - 对&#39;DigitalClock :: staticMetaObject&#39;的未定义引用 - Qt - undefined reference to `vtable for DigitalClock' - undefined reference to `DigitalClock::staticMetaObject' - Qt QT QLibrary“无法加载库* .so:(* .so:undefined symbol:staticMetaObject)” - QT QLibrary “Cannot load library *.so: (*.so: undefined symbol: staticMetaObject)” 如何以编程方式无错误地关闭 Qt qml 应用程序? - How to close a Qt qml application programmatically without errors? 普通Qt应用程序中的Qt Qml - Qt Qml in a normal Qt application 在 Qt 5 中更新 QT/QML 映射 - Recenter QT/QML Map in Qt 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM