Create an enumerator in QML by C++

Asked

Viewed 49 times

1

I’m trying to use an enumerator that was created in C++ and I’m using the QT 5.6 website itself to orient myself, Data Type Conversion Between QML and C++. But when compiling I get the following exception from the compiler:

error: Undefined Reference to `Typesexclass::staticMetaObject'

Erro na compilação no Qt 5.6

Enum

#include <QObject>
class TypeSexClass : public QObject
{
    Q_OBJECT
    Q_ENUMS(TypeSex)
public:
    enum TypeSex{
        NONE, MEN, WOMAN};
    TypeSex typesex() const;
};

Main

#include <QGuiApplication>
#include <QDebug>
#include <QtQml>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    TypeSexClass typesexclass();

    qmlRegisterUncreatableType<TypeSexClass>("teste.typesex", 1, 0, "TypeSexClass","It`s not do create");

    QQmlApplicationEngine engine;

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

    return app.exec();
}

The solved

Enum

#ifndef TYPESEX_H
#define TYPESEX_H

#include <QObject>

class TypeSexClass : public QObject
{
    Q_OBJECT
public:    
    enum TypeSex{
        NONE, MAN, WOMAN};
    Q_ENUM(TypeSex)
};

#endif // TYPESEX_H

Main

#include <QGuiApplication>
#include <QDebug>
#include <QtQml>
#include "typesex.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    qmlRegisterUncreatableType<TypeSexClass>("teste.typesex", 1, 0, "TypeSex","It's not instantiable. It's a enumeration");

    QQmlApplicationEngine engine;

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

    return app.exec();
}

QML


Window {
    id: window1
    title: "Redi"
    visible: true
    visibility : "Maximized"
    Button {
            id: button1
            x: 457
            width: 100
            height: 50
            text: qsTr("Gerar")
            anchors.top: parent.top
            anchors.topMargin: 33
            onClicked: {                
                console.log(TypeSex.MAN)
                console.log(TypeSex.WOMAN)
                console.log(TypeSex.NONE)
            }
}

1 answer

2


Since Qt 5.5 you must use a new macro called Q_ENUM immediately after the definition of the enumeration. In your case:

enum TypeSex{NONE, MEN, WOMAN};
Q_ENUM(TypeSex)

Note that you no longer need to manually record in the meta class, just use the macro. Much easier! :)

More information in this post (in English).

P.S.: Silly preciousness, but use plural or singular in everything in your definition of enumeration. Men is in the plural, and Woman in the singular (use Men and Women, or Man and Woman - which seems to be the most suitable for the enumeration you are making).

  • I changed how you suggested and another error occurred **error: 'Friend' used Outside of class Friend Q_DECL_CONSTEXPR const Qmetaobject qt_getEnumMetaObject(ENUM) Q_DECL_NOEXCEPT { Return &staticMetaObject; } *

  • By chance the enumeration (and consequently the call of Q_ENUM) is not in a . cpp, is it? It should be in a . h.

  • is in a header (.h)

  • I saw that you edited the question. Well, I shouldn’t have edited it, because a future user loses the reference to the original problem. Then please go back to the previous version. Anyway, I saw that you now declared Enum outside the scope of a class inherited from Qobject (and no longer using the macro Q_OBJECT). But I believe that Enum accurate be done in this scope and the macro is required. IE, leave your code as it was before, just remove the macro Q_ENUMS and add the Q_ENUM.

  • 1

    Right, I edited it because the stack doesn’t let you put the changes and suggests that you edit the question, but I can come back. I’ll test it this way you passed me.

  • 1

    Good was just what you said, I needed to declare my enumerator within a class with the macro of Q_OBJECT and used the Q_ENUM Thks

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.