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. Pragma singleton inside QML doesnt works for me
Forum Updated to NodeBB v4.3 + New Features

Pragma singleton inside QML doesnt works for me

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 4 Posters 16.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.
  • V Offline
    V Offline
    vladstelmahovsky
    wrote on last edited by
    #2

    ok, digging git commits of qtdeclarative, I've found missed part:
    Usage:
    // First, define your QML singleton type which provides the functionality.
    @pragma Singleton
    import QtQuick 2.0
    Item {
    property int testProp1: 125
    }
    @
    // Second, register the QML singleton type by calling this function in an initialization function.
    @#include <QtQml>
    ...
    qmlRegisterSingletonType(QUrl("file:///absolute/path/SingletonType.qml"), "Qt.example.qobjectSingleton", 1, 0, "RegisteredSingleton");
    @
    In order to use the registered singleton type in QML, you must import the singleton type.
    @
    import QtQuick 2.0
    import Qt.example.qobjectSingleton 1.0
    Item {
    id: root
    property int someValue: RegisteredSingleton.testProp1
    }@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Maheshdev305
      wrote on last edited by
      #3

      Hi, I tried your example,
      for me it gives the following error during compilation.!

      bq. error: C2664: 'int qmlRegisterSingletonType(const char *,int,int,const char *,QJSValue (__cdecl *)(QQmlEngine *,QJSEngine *))' : cannot convert parameter 2 from 'const char [7]' to 'int'
      There is no context in which this conversion is possible

      am using Qt 5.1. please help.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vladstelmahovsky
        wrote on last edited by
        #4

        Hi

        Probably you have to use Qt 5.2 and later

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SteveG
          wrote on last edited by
          #5

          Try the following. The order is unimportant.

          1. Create QML file (e.g., Test.qml) that uses singleton and add to your .qrc file
          2. Create MySingletonDir subdirectory
          3. Create singleton QML file (e.g., MySingleton.qml) in MySingletonDir
          4. Specify
            @
            pragma Singleton
            @

          at top of MySingleton.qml
          5. Add qmldir file in same directory as MySingleton.qml with content
          @
          singleton MySingleton 1.0 MySingleton.qml
          @

          1. Add MySingleton.qml and qmldir to your .qrc file
          2. Add
            @
            import "MySingletonDir" 1.0
            @

          to Test.qml
          8. Reference singleton in Test.qml (e.g., MySingleton.myProperty)

          Nothing is required in any C++ file.

          1 Reply Last reply
          2
          • M Offline
            M Offline
            Mahaboobsab
            wrote on last edited by
            #6

            Hi SteveG

            I tried the same method you said. the singleton is working fine all the places except inside the states.

            example
            states:[
            State
            {
            name: "HitState"

                        PropertyChanges
                        {
                            target: gradientMainColor
                            color: single.button_hitstate_color
                        }
            
                        PropertyChanges
                        {
                            target: gradientMainColor1
                            color: single.button_hitstate_color
                    }
                ]
            

            the error message is single undefined

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SteveG
              wrote on last edited by
              #7

              Mahaboobsab,

              Is single your singleton and does it have a button_hitstate_color property? How do you reference the singleton in other parts of your code?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mahaboobsab
                wrote on last edited by
                #8

                Hi SteveG

                Yes single is my singleton and button_hitstate_color is one of the property in MySingleton.qml

                Inisde qmldir i have created the module names Styles with version 1.0
                singleton single 1.0 MySingleton.qml

                where ever i need this i include module import Styles 1.0
                and start using the single.property ... it works fine for all the places in the any qml file except inisde the states i,e property changes

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SteveG
                  wrote on last edited by
                  #9

                  Mahaboobsab,

                  As a possible workaround (i.e., hack), does it work if you define
                  @
                  property color my_button_hitstate_color: single.button_hitstate_color
                  @

                  and reference my_button_hitstate_color in PropertyChanges?

                  Regardless,you may want to file a bug report. It would be helpful to attach zipped sample files with a singleton, a qmldir file and a QML component that shows the singleton not working in PropertyChanges but working other places.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mahaboobsab
                    wrote on last edited by
                    #10

                    Hi SteveG,

                    singleton single 1.0 MySingleton.qml

                    it worked by giving first letter of singleton name in Caps as "Single"

                    i am wondering how its related to name ..if it is qml file name it has to be in Caps but singleton name.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mahaboobsab
                      wrote on last edited by
                      #11

                      Hi Steveg

                      I am using singleton but facing problem if i use more than 2 singleton in one qmldir .
                      can u comment on this.?

                      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