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. enums (pure QML) in main.qml not accessable?
Forum Updated to NodeBB v4.3 + New Features

enums (pure QML) in main.qml not accessable?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
enumqmlenumeration
5 Posts 3 Posters 974 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    SeDi
    wrote on 3 Jul 2023, 00:20 last edited by
    #1

    I want to use "pure qml" enums in my main.qml file. According to the docs, "Values are referred to via <Type>.<EnumerationType>.<Value> or <Type>.<Value>."

    I have created my enum set in the main.qml, but I cannot access it:

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    
    ApplicationWindow {
        id: mainWin
        enum PageType {
            FACE_PAGE,
            SHOW_FOLDER_PAGE,
            SETTINGS_PAGE
        }
        property int test1: Main.PageType.FACE_PAGE
        property int test2: mainWin.PageType.FACE_PAGE
        property int test3: ApplicationWindow.PageType.FACE_PAGE
        (...)
    

    The test1 property gives a ReferenceError: Main is not defined error
    The other properties give a TypeError: Cannot read property 'FACE_PAGE' of undefined error

    How can I reference my enums?

    S 1 Reply Last reply 3 Jul 2023, 05:09
    0
    • S SeDi
      3 Jul 2023, 00:20

      I want to use "pure qml" enums in my main.qml file. According to the docs, "Values are referred to via <Type>.<EnumerationType>.<Value> or <Type>.<Value>."

      I have created my enum set in the main.qml, but I cannot access it:

      import QtQuick 2.15
      import QtQuick.Controls 2.15
      
      ApplicationWindow {
          id: mainWin
          enum PageType {
              FACE_PAGE,
              SHOW_FOLDER_PAGE,
              SETTINGS_PAGE
          }
          property int test1: Main.PageType.FACE_PAGE
          property int test2: mainWin.PageType.FACE_PAGE
          property int test3: ApplicationWindow.PageType.FACE_PAGE
          (...)
      

      The test1 property gives a ReferenceError: Main is not defined error
      The other properties give a TypeError: Cannot read property 'FACE_PAGE' of undefined error

      How can I reference my enums?

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 3 Jul 2023, 05:09 last edited by
      #2

      @SeDi I think this might be unsupported, since usually the first <type> is the same as filename. In your case it's main.qml which starts with a lowercase letter so QML engine won't accept it I guess. But just in case, try this:

      property int test4: mian.PageType.FACE_PAGE
      property int test5: mian.FACE_PAGE
      

      If all fails - you'll have to declare your enum in a different QML file, sorry.

      (Z(:^

      S 1 Reply Last reply 3 Jul 2023, 16:30
      1
      • K Offline
        K Offline
        Kamichanw
        wrote on 3 Jul 2023, 11:56 last edited by
        #3

        As what @sierdzio said, you can access your enum as follwing way

        // main.qml
        property int test1: main.FACE_PAGE // ok 
        property int test2: main.PageType.FACE_PAGE // ok
        
        S 1 Reply Last reply 3 Jul 2023, 16:32
        0
        • S sierdzio
          3 Jul 2023, 05:09

          @SeDi I think this might be unsupported, since usually the first <type> is the same as filename. In your case it's main.qml which starts with a lowercase letter so QML engine won't accept it I guess. But just in case, try this:

          property int test4: mian.PageType.FACE_PAGE
          property int test5: mian.FACE_PAGE
          

          If all fails - you'll have to declare your enum in a different QML file, sorry.

          S Offline
          S Offline
          SeDi
          wrote on 3 Jul 2023, 16:30 last edited by
          #4

          @sierdzio Many thanks for answering. Unfortunately you seem to be right with this being unsupported. But actually it was much easier to re-structure my project than I had thought. Used in the proper way (== not in main.qml) enum works well by now. Would be interesting to know, if internally main.qml goes by any TypeName, thus making it referenceable. Perhaps I'll ask this in the #interest list, but for now I am settled.

          1 Reply Last reply
          2
          • S SeDi has marked this topic as solved on 3 Jul 2023, 16:30
          • K Kamichanw
            3 Jul 2023, 11:56

            As what @sierdzio said, you can access your enum as follwing way

            // main.qml
            property int test1: main.FACE_PAGE // ok 
            property int test2: main.PageType.FACE_PAGE // ok
            
            S Offline
            S Offline
            SeDi
            wrote on 3 Jul 2023, 16:32 last edited by
            #5

            @Kamichanw Unfortunately, you can't. "ReferenceError: main is not defined"

            1 Reply Last reply
            0

            5/5

            3 Jul 2023, 16:32

            • Login

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