I'm trying to use an enumerator that was created in C ++ and I'm using the QT 5.6 website itself to guide me, Data Type Conversion Between QML and C ++ . However when compiling I get the following compiler exception:
error: undefined reference to 'TypeSexClass :: staticMetaObject'
Enum
#include<QObject>classTypeSexClass:publicQObject{Q_OBJECTQ_ENUMS(TypeSex)public:enumTypeSex{NONE,MEN,WOMAN};TypeSextypesex()const;};
Main
#include<QGuiApplication>#include<QDebug>#include<QtQml>intmain(intargc,char*argv[]){QGuiApplicationapp(argc,argv);TypeSexClasstypesexclass();qmlRegisterUncreatableType<TypeSexClass>("teste.typesex", 1, 0, "TypeSexClass","It's not do create");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
Resolved
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)
}
}