Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. qml type annotation with local enum

qml type annotation with local enum

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 501 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.
  • M Offline
    M Offline
    majorRonk
    wrote on last edited by majorRonk
    #1

    QtCreator (qt 6.5.2) compiler is complaing:
    Could not compile function getSomething: Cannot resolve the argument type States. [compiler]

    enum States { State0, State1, State2}
    
    function getSomething(id: States): string{
    }
    

    How should i use an local enum type correctly?

    edit: should i use
    id: int instead?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Shrishashank
      wrote on last edited by
      #2

      I see the issue with your QML enum usage. In QML, you can't directly use an enum as a parameter type the way you're trying to do. Here's how to fix it:

      Solution 1: Use int as the parameter type while keeping the enum:
      qml
      enum States { State0, State1, State2 }

      function getSomething(id: int): string {
      if (id === States.State0) return "This is State0";
      if (id === States.State1) return "This is State1";
      if (id === States.State2) return "This is State2";
      return "Unknown state";
      }

      Solution 2: Define the enum in a more QML-friendly way:
      // In a separate file, e.g., States.qml
      pragma Singleton
      import QtQml 2.15

      QtObject {
      readonly property int State0: 0
      readonly property int State1: 1
      readonly property int State2: 2
      }

      // Then in your main file:
      import "path/to/States.qml" as States

      function getSomething(id: int): string {
      if (id === States.State0) return "This is State0";
      // etc.
      }

      The key point is that in QML, enums are essentially integers under the hood, so you need to use id: int as the parameter type. The enum values themselves can still be used for comparison inside the function.
      Hope this helps!

      SHRISHASHANK S K

      M 1 Reply Last reply
      1
      • S Shrishashank

        I see the issue with your QML enum usage. In QML, you can't directly use an enum as a parameter type the way you're trying to do. Here's how to fix it:

        Solution 1: Use int as the parameter type while keeping the enum:
        qml
        enum States { State0, State1, State2 }

        function getSomething(id: int): string {
        if (id === States.State0) return "This is State0";
        if (id === States.State1) return "This is State1";
        if (id === States.State2) return "This is State2";
        return "Unknown state";
        }

        Solution 2: Define the enum in a more QML-friendly way:
        // In a separate file, e.g., States.qml
        pragma Singleton
        import QtQml 2.15

        QtObject {
        readonly property int State0: 0
        readonly property int State1: 1
        readonly property int State2: 2
        }

        // Then in your main file:
        import "path/to/States.qml" as States

        function getSomething(id: int): string {
        if (id === States.State0) return "This is State0";
        // etc.
        }

        The key point is that in QML, enums are essentially integers under the hood, so you need to use id: int as the parameter type. The enum values themselves can still be used for comparison inside the function.
        Hope this helps!

        M Offline
        M Offline
        majorRonk
        wrote on last edited by
        #3

        @Shrishashank thank you very much.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RoachLin
          wrote last edited by
          #4

          https://doc.qt.io/qt-6/qtqml-javascript-hostenvironment.html#type-annotations-and-assertions
          It says:
          Note: In QML, enumerations are not types and can therefore not be used as type annotations. Their underlying numeric type, int or double, should be used instead.

          1 Reply Last reply
          3

          • Login

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