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
Forum Updated to NodeBB v4.3 + New Features

qml type annotation with local enum

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 202 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 10 Mar 2025, 08:46 last edited by majorRonk 3 Oct 2025, 08:50
    #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 Online
      S Online
      Shrishashank
      wrote on 14 Mar 2025, 06:46 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 17 Mar 2025, 10:06
      1
      • S Shrishashank
        14 Mar 2025, 06:46

        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 17 Mar 2025, 10:06 last edited by
        #3

        @Shrishashank thank you very much.

        1 Reply Last reply
        0

        1/3

        10 Mar 2025, 08:46

        • Login

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