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?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
enumqmlenumeration
5 Posts 3 Posters 940 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.
  • SeDiS Offline
    SeDiS Offline
    SeDi
    wrote on 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?

    sierdzioS 1 Reply Last reply
    0
    • SeDiS SeDi

      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?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on 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(:^

      SeDiS 1 Reply Last reply
      1
      • K Offline
        K Offline
        Kamichanw
        wrote on 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
        
        SeDiS 1 Reply Last reply
        0
        • sierdzioS sierdzio

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

          SeDiS Offline
          SeDiS Offline
          SeDi
          wrote on 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
          • SeDiS SeDi has marked this topic as solved on
          • K Kamichanw

            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
            
            SeDiS Offline
            SeDiS Offline
            SeDi
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

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