[Resolved]About the Q_ENUMS in Qt Quick
-
http://doc.qt.nokia.com/latest/qobject.html#Q_ENUMS
bq. If you want to register an enum that is declared in another class, the enum must be fully qualified with the name of the class defining it. In addition, the class defining the enum has to inherit QObject as well as declare the enum using Q_ENUMS().
What this means that class defining enum has to derive from QObject, so global scope enums i guess might not work even just registering with Q_ENUMS, found one useful way to do it @ http://taschenorakel.de/michael/2011/07/14/using-c-enums-qml/
-
[quote author="manishsharma" date="1313394709"]http://doc.qt.nokia.com/latest/qobject.html#Q_ENUMS
bq. If you want to register an enum that is declared in another class, the enum must be fully qualified with the name of the class defining it. In addition, the class defining the enum has to inherit QObject as well as declare the enum using Q_ENUMS().
What this means that class defining enum has to derive from QObject, so global scope enums i guess might not work even just registering with Q_ENUMS, found one useful way to do it at http://taschenorakel.de/michael/2011/07/14/using-c-enums-qml/
[/quote]Yes, I have do in this.
But, how can i define a property of the enum type in my QML file?@
class Mynamespace :public QObject
{
Q_OBJECT
Q_ENUMS(DataType)public: enum DataType { type1, type2, type3 };
}
//in my qml file
//importItem {
property DataType//? I am not sure, it seems do not work.}
@
-
instead of creating enum property can't you just create an int property and assign value to it from exposed enum class from c++? Other thing which you have to do is call qmlRegisterType<Mynamespace>
Below is how i got it working,
my enum class
@
class EnumClass : public QObject
{
Q_OBJECT
Q_ENUMS(ClassType)
public:
enum ClassType { TypeOne, TypeTwo, TypeThree };
};@main.cpp
@int main(int argc, char **argv)
{
QApplication app(argc, argv);qmlRegisterType<EnumClass>("myEnum",1,0,"EnumClass"); QDeclarativeView *view = new QDeclarativeView; view->setSource(QUrl::fromLocalFile("./main.qml")); view->show(); app.exec();
}@
my main.qml file
@import Qt 4.7
import myEnum 1.0Rectangle {
id: page
width: 500; height: 200
color: "lightgray"
property int en : EnumClass.TypeThreeText { id: helloText text: "Hello world!" y: 30 anchors.horizontalCenter: page.horizontalCenter font.pointSize: 24; font.bold: true MouseArea { anchors.fill: parent onClicked: { console.log(en); } } }
}
@