Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Passing Enum into C++ function like parameter from QML. How?
QtWS25 Last Chance

Passing Enum into C++ function like parameter from QML. How?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bogong
    wrote on 20 Nov 2019, 07:45 last edited by bogong
    #1

    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.

    J 1 Reply Last reply 20 Nov 2019, 07:58
    0
    • B bogong
      20 Nov 2019, 07:45

      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.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 20 Nov 2019, 07:58 last edited by
      #2

      @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)
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      B 1 Reply Last reply 20 Nov 2019, 08:01
      5
      • J J.Hilk
        20 Nov 2019, 07:58

        @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)
        
        B Offline
        B Offline
        bogong
        wrote on 20 Nov 2019, 08:01 last edited by bogong
        #3

        @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?

        K 1 Reply Last reply 20 Nov 2019, 08:08
        0
        • B bogong
          20 Nov 2019, 08:01

          @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?

          K Offline
          K Offline
          KroMignon
          wrote on 20 Nov 2019, 08:08 last edited by KroMignon
          #4

          @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

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          B 1 Reply Last reply 20 Nov 2019, 08:30
          1
          • K KroMignon
            20 Nov 2019, 08:08

            @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

            B Offline
            B Offline
            bogong
            wrote on 20 Nov 2019, 08:30 last edited by bogong
            #5

            @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.

            K jsulmJ 2 Replies Last reply 20 Nov 2019, 08:57
            0
            • B bogong
              20 Nov 2019, 08:30

              @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.

              K Offline
              K Offline
              KroMignon
              wrote on 20 Nov 2019, 08:57 last edited by KroMignon
              #6

              @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 ;)

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0
              • B bogong
                20 Nov 2019, 08:30

                @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.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 20 Nov 2019, 09:17 last edited by
                #7

                @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.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • B Offline
                  B Offline
                  bogong
                  wrote on 8 May 2021, 10:57 last edited by
                  #8

                  Published examples for this issue https://github.com/ArboreusSystems/arboreus_examples/tree/master/qt/Enum/Enum_v3

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved