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'
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)
}
}
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; } *
– Jhonny Rm
By chance the enumeration (and consequently the call of
Q_ENUM
) is not in a . cpp, is it? It should be in a . h.– Luiz Vieira
is in a header (.h)
– Jhonny Rm
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.
– Luiz Vieira
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.
– Jhonny Rm
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
– Jhonny Rm