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

Create a global object

Scheduled Pinned Locked Moved QML and Qt Quick
13 Posts 4 Posters 7.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.
  • Y Offline
    Y Offline
    yan_
    wrote on last edited by
    #1

    Hi would like create a Global object to expose function.

    And is it possible to add it in a plugin?

    thanks

    [edit: fixed typos / $chetankjain ]

    "QExtend":http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
    "QEXtend Video":http://www.youtube.com/user/QEx...

    1 Reply Last reply
    0
    • I Offline
      I Offline
      ivan
      wrote on last edited by
      #2

      If I understood you correctly, you can create a class with a static accessor. Something like:

      @class Global {
      static Global * self();

      private:
      Global();
      static Global * instance;

      };@

      As for the plugin, you can just modify the accessor to get the object from the plugin.

      Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
      Qt Ambassador (from the old Nokia days)

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yan_
        wrote on last edited by
        #3

        I want make a global object like Qt Object to expose functions :
        "http://doc.trolltech.com/main-snapshot/qml-qt.html":http://doc.trolltech.com/main-snapshot/qml-qt.html

        Or something like
        @import "file.js" as myObject@

        And this global Object is added with import of my QML plugin. This object could be coded in C++ or JS

        [edit: fixed hyperlink / $chetankjain ]

        "QExtend":http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
        "QEXtend Video":http://www.youtube.com/user/QEx...

        1 Reply Last reply
        0
        • I Offline
          I Offline
          ivan
          wrote on last edited by
          #4

          Sorry, didn't realize you are talking about scripting. Haven't played with QML that much, bit if things are inherited from the QtScript module, it should go something like this:

          @engine.globalObject().setProperty("myObject", engine.newQObject(object));@

          After that, your object will be available to qtscript.

          As for making it pluggable with both c++ and js, I can't really help. (I could think of a dirty way to achieve it, but IMO it is better to wait for our friendly Trolls to tell us the proper way)

          Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
          Qt Ambassador (from the old Nokia days)

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            yan_
            wrote on last edited by
            #5

            Thanks.

            They do it in Qt Declarative code (QDeclarativeScriptEngine constructor in QDeclarativeEngine code)

            I've an access to QDeclarativeEngine which use a QScriptEngine (internaly) on plugin init function.
            But i don't find how to access to a QScriptEngine.

            "QExtend":http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
            "QEXtend Video":http://www.youtube.com/user/QEx...

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mbrasser
              wrote on last edited by
              #6

              You could try using setContextProperty() on the root context of the engine -- this will effectively add the object as a context property that is available to all the elements in the engine. From a plugin, you can do so inside the "initializeEngine()":http://doc.trolltech.com/main-snapshot/qdeclarativeextensionplugin.html#initializeEngine function.

              I believe there are plans to look at this more for a future release, so plugins can easily add "namespaces" like the Qt object with functions, enum values, etc.

              1 Reply Last reply
              0
              • Y Offline
                Y Offline
                yan_
                wrote on last edited by
                #7

                With this method, how create QScriptValue with newXXXX function ?
                "http://doc.qt.nokia.com/4.7-snapshot/qscriptengine.html":http://doc.qt.nokia.com/4.7-snapshot/qscriptengine.html

                Another solution that i will test, it's create a component with QDeclarativeComponent
                "http://doc.qt.nokia.com/4.7-snapshot/qdeclarativecomponent.html":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativecomponent.html

                Make @import "file.js" as myObject@ equivalent in qmldir file could be interesting.

                [edit: fixed hyperlink / $chetakjain ]

                "QExtend":http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
                "QEXtend Video":http://www.youtube.com/user/QEx...

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #8

                  hi yan, you could also use the link feature for hyperlinks

                  1 Reply Last reply
                  0
                  • Y Offline
                    Y Offline
                    yan_
                    wrote on last edited by
                    #9

                    [quote author="chetankjain" date="1283761818"]hi yan, you could also use the link feature for hyperlinks[/quote]
                    Sorry. I don't see this forum don't make auto-linked.

                    "QExtend":http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
                    "QEXtend Video":http://www.youtube.com/user/QEx...

                    1 Reply Last reply
                    0
                    • ? This user is from outside of this forum
                      ? This user is from outside of this forum
                      Guest
                      wrote on last edited by
                      #10

                      yan wrote:

                      bq. Sorry. I don’t see this forum don’t make auto-linked.

                      That I believe is in the roadmap ... :)

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

                        [quote author="yan_" date="1283758523"]With this method, how create QScriptValue with newXXXX function ?[/quote]

                        With the setContextProperty() approach, you would use a QObject with slots or Q_INVOKABLEs for your "global" object, rather than a QScriptValue. The Minehunt demo shows this approach (adds a MinehuntGame object in the root context).

                        1 Reply Last reply
                        0
                        • Y Offline
                          Y Offline
                          yan_
                          wrote on last edited by
                          #12

                          [quote author="mbrasser" date="1283814387"]With the setContextProperty() approach, you would use a QObject with slots or Q_INVOKABLEs for your "global" object, rather than a QScriptValue. The Minehunt demo shows this approach (adds a MinehuntGame object in the root context).[/quote]
                          Thanks.
                          I've ever test it and it's work perfectly ^^

                          I find a solution for JS code :D
                          1- create a qml file wich define function on the root element (I use QtObject)
                          2- in initializeEngine(), use a"QDeclarativeComponent ":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativecomponent.html.
                          3- set qml file path or use setData
                          4- use create to make a QObject
                          5- add this QObject with setContextProperty

                          Like QDeclarativeComponent use qml file, i think it's so possible to mix JS and C++ code

                          "QExtend":http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
                          "QEXtend Video":http://www.youtube.com/user/QEx...

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            johnlamericain
                            wrote on last edited by
                            #13

                            It's really impressive, thanks for that !

                            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