Passing Enum into C++ function like parameter from QML. How?
-
Hello all!
I need to pass Enum value like parameter in C++ function called from QML. In this documentation described only using C++ Enum in QML and passing it into C++ like int but not Enum from JavaScript into C++ when using signals. The question is how to make it automatically working without casting it in C++ or defining it like int in QML?If I am using something like this:
public slots: void someFunction(EnumType inValue);
it's not working for calling from QML, but if I am using something like this:
public slots: void someFunction(int inValue) { EnumType oValue = static_cast<EnumType>(inValue); }
it's OK. Is there any way to exclude casting when I am using it? Or make it automatically happening?
I mean being looking for QML like Int, but inside function like EnumType. -
Hello all!
I need to pass Enum value like parameter in C++ function called from QML. In this documentation described only using C++ Enum in QML and passing it into C++ like int but not Enum from JavaScript into C++ when using signals. The question is how to make it automatically working without casting it in C++ or defining it like int in QML?If I am using something like this:
public slots: void someFunction(EnumType inValue);
it's not working for calling from QML, but if I am using something like this:
public slots: void someFunction(int inValue) { EnumType oValue = static_cast<EnumType>(inValue); }
it's OK. Is there any way to exclude casting when I am using it? Or make it automatically happening?
I mean being looking for QML like Int, but inside function like EnumType.@bogong
hi, couple of things,first, make sure to use the Q_ENUM() macro on your enum definition
Make sure your Enums start with a capital letter
enum Languages { NoLanguage = 0, De = 1, En, It, Fr };Q_ENUM(Languages)
register it to the meta system:
qRegisterMetaType<Languages>("Languages");
register the class that has the enum definition to your QML engine:
qmlRegisterUncreatableType<MyEnums>("CppEnums",1,0,"Enums","Enum is not a type");
than you have to import it in QML
import CppEnums 1.0
Than you can use them inside your QML file and pass it as valid argument to c++ functions
onSomePropertyChanged: backend.someFunction(Enums.En)
-
@bogong
hi, couple of things,first, make sure to use the Q_ENUM() macro on your enum definition
Make sure your Enums start with a capital letter
enum Languages { NoLanguage = 0, De = 1, En, It, Fr };Q_ENUM(Languages)
register it to the meta system:
qRegisterMetaType<Languages>("Languages");
register the class that has the enum definition to your QML engine:
qmlRegisterUncreatableType<MyEnums>("CppEnums",1,0,"Enums","Enum is not a type");
than you have to import it in QML
import CppEnums 1.0
Than you can use them inside your QML file and pass it as valid argument to c++ functions
onSomePropertyChanged: backend.someFunction(Enums.En)
@J-Hilk said in Passing Enum into C++ function like parameter from QML. How?:
Make sure your Enums start with a capital letter
I've been doing everything that mentioned by you but not naming Enum class from capital letter. Should I be naming Enum name class from Capital Letter? Or it's only for enum values?
-
@J-Hilk said in Passing Enum into C++ function like parameter from QML. How?:
Make sure your Enums start with a capital letter
I've been doing everything that mentioned by you but not naming Enum class from capital letter. Should I be naming Enum name class from Capital Letter? Or it's only for enum values?
-
@bogong C++ classes and enum should always starts with a capital letter to be "visible" on QML side... I don't remember where I found this information, but I known this is mandatory!
==> see bug report QTBUG-46758
@KroMignon Next portion of Qt things ... Sometimes it's making me crazy because of classes should be started from capital letter BUT!!! the file name of the class should be lower case if you using it in cross-platform application ... And the amount of this "only letter" things is enormous. Issue closed.
-
@KroMignon Next portion of Qt things ... Sometimes it's making me crazy because of classes should be started from capital letter BUT!!! the file name of the class should be lower case if you using it in cross-platform application ... And the amount of this "only letter" things is enormous. Issue closed.
@bogong said in Passing Enum into C++ function like parameter from QML. How?:
... And the amount of this "only letter" things is enormous
Hmm, C/C++ are case sensitive languages, and a common "good practice", is to use PascalCase for classe names or enums... And for QML, it seems to be a requirement from the JavaScript Engine (according to the bug report QTBUG-4758).
The file naming convention is to avoid having problem when you want to build your project on Windows or Unix based systems. This is only a common "good practice". You don't need to follow it if you don't want. It is up to you.
In case of QML, the file name should be in PascalCase ;) -
@KroMignon Next portion of Qt things ... Sometimes it's making me crazy because of classes should be started from capital letter BUT!!! the file name of the class should be lower case if you using it in cross-platform application ... And the amount of this "only letter" things is enormous. Issue closed.
@bogong said in Passing Enum into C++ function like parameter from QML. How?:
the file name of the class should be lower case if you using it in cross-platform application
The reason for this is that some file systems are case sensitive (Linux/UNIX) and some other not (Windows). So, it is a good idea to always use one naming convention for file names (like lower case). But as @KroMignon mentioned it is just a convention.
-
Published examples for this issue https://github.com/ArboreusSystems/arboreus_examples/tree/master/qt/Enum/Enum_v3