Estou tentando utilizar um enumerador que foi criado no C++ e estou utilizando o próprio site do QT 5.6 para me orientar, Data Type Conversion Between QML and C++. Porém ao compilar eu recebe a seguinte exceção do compilador:
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();
}
O resolvido
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)
}
}