How to access C++ enum from QML?
You can wrap the enum in a class which derives from QObject (and that you expose to QML):
style.hpp :
#ifndef STYLE_HPP
#define STYLE_HPP
#include <QtGlobal>
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
// Qt 4
#include <QDeclarativeEngine>
#else
// Qt 5
#include <QQmlEngine>
#endif
// Required derivation from QObject
class StyleClass : public QObject
{
Q_OBJECT
public:
// Default constructor, required for classes you expose to QML.
StyleClass() : QObject() {}
enum EnStyle
{
STYLE_RADIAL,
STYLE_ENVELOPE,
STYLE_FILLED
};
Q_ENUMS(EnStyle)
// Do not forget to declare your class to the QML system.
static void declareQML() {
qmlRegisterType<StyleClass>("MyQMLEnums", 13, 37, "Style");
}
};
#endif // STYLE_HPP
main.cpp:
#include <QApplication>
#include "style.hpp"
int main (int argc, char ** argv) {
QApplication a(argc, argv);
//...
StyleClass::declareQML();
//...
return a.exec();
}
QML Code:
import MyQMLEnums 13.37
import QtQuick 2.0 // Or 1.1 depending on your Qt version
Item {
id: myitem
//...
property int item_style: Style.STYLE_RADIAL
//...
}
How to access a Q_ENUM declared in C++ class from QML?
Your class needs two adjustments.
as pointed out by JarMan, it needs a metaObject, which can be obtained by deriving from QObject and adding Q_OBJECT:
class MyClass : public QObject
{
Q_OBJECT
...
};Actually, it would also be possible to use
Q_GADGET
but you already seem to lean towardsQ_OBJECT
. But, as requested, here we go:class MyClass
{
Q_GADGET
public:
enum Enum_Test {
ETestA,
ETestB
};
Q_ENUM(Enum_Test)
};Keep in mind that Q_GADGET cannot have signals, so I left out the property and only have this class as "enum-placeholder".
The enum value names need to be capitalized:
enum Enum_Test {
ETestA,
ETestB
};
Q_ENUM(Enum_Test)
Then you can use it in QML as:
QtObject {
property int myEnumVal: MyClass.ETestA
}
Note that support for enums is somewhat limited because the mix with JavaScript. The values will be converted to integers. Also, when used in a JavaScript switch-statement, typo's will not be warned about by QtCreator (assuming version 4.14)
Using c++ enum in qml
You register the type as SettingManager
but use it as BookManager
. The correct code is:
settingManager.bookKind = SettingManager.BookKind1;
You should also use Q_ENUM
instead of Q_ENUMS
.
How to access enum from QML?
You're enum is inside the class Heatercooler so you should write Heatercooler::HEAT_GO_AMBIENT_FOR_HOT
QML component enum class property
According to the documentation,
To use a custom enumeration as a data type, its class must be
registered and the enumeration must also be declared with Q_ENUM() to
register it with Qt's meta object system.
So you need to register your class Card
instead of the enum InGameState
:
qmlRegisterType<Card>("com.memorygame.card", 1, 0, "Card");
Additionally:
The enumeration type is a representation of a C++ enum type. It is not
possible to refer to the enumeration type in QML itself; instead, the
int or var types can be used when referring to enumeration values from
QML code.
For example, in your case, the enum should be use as follows:
import QtQuick 2.0
import com.memorygame.card 1.0
Item {
property int state: Card.FLIPPED
Rectangle {
id: dummy
width: 10
}
}
How to pass enum class to QML?
The problem was in Helper
class (thanks to Qt community).
class Helper {
...
};
Q_DECLARE_METATYPE(Helper::Requester)
Q_DECLARE_METATYPE(Helper::JANSWER)
QML signal with a C++ enum type
Ok, it does not seem like this can be done with enums i.e. you have to use ints
. I made the following change and it works:
class MyType : public QObject
{
Q_OBJECT
Q_ENUMS(TestEnum)
Q_PROPERTY(TestEnum foo READ foo WRITE setFoo NOTIFY fooChanged)
public:
enum TestEnum
{
State1 = 1,
State2 = 2
};
MyType(QObject *parent = nullptr) :
QObject(parent),
mFoo(TestEnum::State1)
{
}
TestEnum & foo()
{
return mFoo;
}
void setFoo(TestEnum foo)
{
if (foo == mFoo)
return;
mFoo = foo;
emit fooChanged(static_cast<int>(mFoo));
}
signals:
void fooChanged(int blah);
private:
TestEnum mFoo;
};
Q_DECLARE_METATYPE(MyType::TestEnum)
The slots need to be changed as well:
class HandleTextField : public QObject
{
Q_OBJECT
public:
explicit HandleTextField(QObject *parent = 0);
public slots:
void handleSubmitTextField(int in);
void handleFooChanged(int in);
public:
MyType myType;
};
We need to change the connections for int
type:
QObject::connect(topLevel, SIGNAL(submitTextField(int)),
&handleTextField, SLOT(handleSubmitTextField(int)));
and finally the signal is declared as:
signal submitTextField(int text)
How to use C++ enum in Qt .js file?
When you call qmlRegisterType<FileSystemModel>("FileSystemModel", 1, 0, "FileSystemModel")
you're registering a FileSystemModel
QML module that contains a FileSystemModel
type, so in your js when you write .import FileSystemModel 1.0 as FileSystemModel
you're not actually importing your type but the QML module, that's why it's not working.
In your js file try changing FileSystemModel.TYPE_DIR
to FileSystemModel.FileSystemModel.TYPE_DIR
, that should do the trick.
Related Topics
Will My iPhone App Take a Performance Hit If I Use Objective-C for Low Level Code
Why Is There No Piecewise Tuple Construction
Error Redeclaring a for Loop Variable Within the Loop
Inheriting Constructors and Brace-Or-Equal Initializers
Overriding Return Type in Function Template Specialization
The Difference Between C and C++ Regarding the ++ Operator
What Are Practical Uses of a Protected Constructor
Calculating Execution Time in C++
Why Are Pointers to a Reference Illegal in C++
How to Design Proper Release of a Boost::Asio Socket or Wrapper Thereof
What Are Some Tricks I Can Use with MACros
Generate All Sequences of Bits Within Hamming Distance T
Convert Hexadecimal String with Leading "0X" to Signed Short in C++
How to Print Out the Size of a C++ Class at Compile-Time