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. Static Object Definition From QML

Static Object Definition From QML

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 5.8k 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.
  • carles.sole.grauC Offline
    carles.sole.grauC Offline
    carles.sole.grau
    wrote on last edited by
    #1

    Hi everybody,
    I would like to know if it's possible to create a new "static" QML type from QML code. And then use its properties without having to instantiate that object.
    For instance:
    EnumerationCustomType.qml:

    import QtQuick 2.0
    
    QtObject {
        
        readonly property int one: 1
        readonly property int two: 2
        readonly property int three: 3
    }
    
    

    And then in main.qml:

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        
        Label {
            anchors.centerIn: parent
            text: EnumerationCustomType.one
        }
    }
    

    This code does not work, this error is shown:

    ReferenceError: EnumerationCustomType is not defined
    

    P.S: I know that defining this object in main.qml it could be done:

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        property var enumerationCustomType: EnumerationCustomType{}
        Label {
            anchors.centerIn: parent
            text: enumerationCustomType.one
        }
    }
    

    But I would like to do it without having to defining it.

    Thank you very much

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! You can use a singleton for that:

      EnumerationCustomType.qml

      import QtQuick 2.7
      
      pragma Singleton
      
      QtObject {
          readonly property int one: 1
          readonly property int two: 2
          readonly property int three: 3
      }
      

      qmldir

      singleton EnumerationCustomType 1.0 EnumerationCustomType.qml
      
      

      main.qml

      import QtQuick 2.7
      import QtQuick.Controls 2.0
      import "."
      
      ApplicationWindow {
          visible: true
          width: 640
          height: 480
      
          Column {
              Text { text: EnumerationCustomType.one }
              Text { text: EnumerationCustomType.two }
              Text { text: EnumerationCustomType.three }
          }
      
      }
      
      carles.sole.grauC 1 Reply Last reply
      3
      • ? A Former User

        Hi! You can use a singleton for that:

        EnumerationCustomType.qml

        import QtQuick 2.7
        
        pragma Singleton
        
        QtObject {
            readonly property int one: 1
            readonly property int two: 2
            readonly property int three: 3
        }
        

        qmldir

        singleton EnumerationCustomType 1.0 EnumerationCustomType.qml
        
        

        main.qml

        import QtQuick 2.7
        import QtQuick.Controls 2.0
        import "."
        
        ApplicationWindow {
            visible: true
            width: 640
            height: 480
        
            Column {
                Text { text: EnumerationCustomType.one }
                Text { text: EnumerationCustomType.two }
                Text { text: EnumerationCustomType.three }
            }
        
        }
        
        carles.sole.grauC Offline
        carles.sole.grauC Offline
        carles.sole.grau
        wrote on last edited by carles.sole.grau
        #3

        @Wieland
        Hi, in relation with this answer I would like to replace the

        import "."
        

        with something like

        import MyComponents 1.0
        

        I've found this documentation: http://doc.qt.io/qt-5/qtqml-modules-qmldir.html
        Where it's said:

        //Style.qml with custom singleton type definition
        pragma Singleton
        import QtQuick 2.0
        
        QtObject {
            property int textSize: 20
            property color textColor: "green"
        }
        
        // qmldir declaring the singleton type
        module CustomStyles
        singleton Style 1.0 Style.qml
        
        // singleton type in use
        import QtQuick 2.0
        import CustomStyles 1.0
        
        Text {
            font.pixelSize: Style.textSize
            color: Style.textColor
            text: "Hello World"
        }
        

        So in my case I did:

        //EnumerationCustomType.qml with custom singleton type definition
        pragma Singleton
        import QtQuick 2.7
        
        QtObject {
            readonly property int one: 1
            readonly property int two: 2
            readonly property int three: 3
        }
        
        // qmldir declaring the singleton type
        module MyComponents
        singleton EnumerationCustomType 1.0 EnumerationCustomType.qml
        
        // singleton type in use
        import QtQuick 2.7
        import QtQuick.Controls 2.0
        import MyComponents 1.0
        
        ApplicationWindow {
            visible: true
            width: 640
            height: 480
        
            Column {
                Text { text: EnumerationCustomType.one }
                Text { text: EnumerationCustomType.two }
                Text { text: EnumerationCustomType.three }
            }
        }
        
        

        But when I try to run the app it is said:

        qrc:/main.qml:3 module "MyComponents" is not installed
        

        So, do you know how to solve this?
        I'm doing anything in a bad way?

        Thank you very much

        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