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. Calling static function from QML? Doable?
Forum Update on Monday, May 27th 2025

Calling static function from QML? Doable?

Scheduled Pinned Locked Moved QML and Qt Quick
18 Posts 8 Posters 25.6k 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.
  • T Offline
    T Offline
    task_struct
    wrote on 4 Oct 2011, 20:38 last edited by
    #4

    Ahh, I've tried only with static function in NumberMachine and it works.

    Now I've tried with singleton and it seems that you need a public constructor to register class in QML.

    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

    • Linu...
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SnowmanX
      wrote on 4 Oct 2011, 20:57 last edited by
      #5

      Right, but that defeats the whole purpose of the singleton - if there is a public constructor, it is no longer singleton ....

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on 4 Oct 2011, 21:03 last edited by
        #6

        Perhaps you could write a small utility wrapper class which you can instantiate, which then has methods which pass things back and from from the singleton.

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SnowmanX
          wrote on 4 Oct 2011, 21:14 last edited by
          #7

          Yeah, doable, but not exactly feasible ... Furthermore I seem to have more problems with enums at this point - the enum declared in the class declaring the static function (the singleton) is not visible to QML when used from the other helper class ... another bummer :(

          1 Reply Last reply
          0
          • T Offline
            T Offline
            task_struct
            wrote on 4 Oct 2011, 21:16 last edited by
            #8

            You can write a wrapper class that can be instantiated multiple times in QML and communicates with the singleton. It's not pretty, but I don't know what else can be done.

            Edit:
            Sorry. I was too slow and there wasn't warning that there are new posts :(

            "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

            • Linu...
            1 Reply Last reply
            0
            • S Offline
              S Offline
              SnowmanX
              wrote on 4 Oct 2011, 21:41 last edited by
              #9

              Thanks guys, I guess I'll try to enter in issue that static functions should be directly callable from QML, as not having this feature is making certain patterns difficult if not impossible (the mentioned problem with enum coming from a different class ...).

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mlong
                wrote on 4 Oct 2011, 21:50 last edited by
                #10

                Sounds like a good idea.

                As an aside, as a workaround, you could -- and yes, I know it's dirty and nasty -- always mirror the enum in the class that's visible to QML.

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SnowmanX
                  wrote on 5 Oct 2011, 00:10 last edited by
                  #11

                  Thanks for the tip. It might do it for at least some of the usecases.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mlong
                    wrote on 5 Oct 2011, 15:41 last edited by
                    #12

                    If you do add a jira request for the issue, please be sure and add a link to it here for future reference.

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mbrasser
                      wrote on 5 Oct 2011, 23:36 last edited by
                      #13

                      Hi,

                      For QtQuick 2.0 we are introducing an additional "module API", which should allow you to implement the singleton pattern more easily.

                      The docs for this don't seem to be correctly generated at the moment, but you can view them in source at https://qt.gitorious.org/qt/qtdeclarative/blobs/master/src/declarative/qml/qdeclarative.h (look for qmlRegisterModuleApi)

                      Regards,
                      Michael

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lgeyer
                        wrote on 6 Oct 2011, 12:29 last edited by
                        #14

                        [quote author="mbrasser" date="1317857789"]Hi, For QtQuick 2.0 we are introducing an additional "module API", which should allow you to implement the singleton pattern more easily.[/quote]

                        Do you coincidentally know if there is a (provisional) changelog for QtQuick 2.0, a compiled list of (planned) changes or something like that?

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          sierdzio
                          Moderators
                          wrote on 6 Oct 2011, 16:33 last edited by
                          #15

                          [quote author="Lukas Geyer" date="1317904148"][quote author="mbrasser" date="1317857789"]Hi, For QtQuick 2.0 we are introducing an additional "module API", which should allow you to implement the singleton pattern more easily.[/quote]

                          Do you coincidentally know if there is a (provisional) changelog for QtQuick 2.0, a compiled list of (planned) changes or something like that?[/quote]

                          Not a real changelog, but at least a glimpse on what's to come: http://doc.qt.nokia.com/qt5-snapshot/qtquick2-whatsnew.html

                          (Z(:^

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            lgeyer
                            wrote on 7 Oct 2011, 09:11 last edited by
                            #16

                            Thanks.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              spiritqt
                              wrote on 13 Nov 2011, 10:23 last edited by
                              #17

                              Hi,
                              you can use approach described "here":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#embedding-c-objects-into-qml-components. It works fine for me.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                steno
                                wrote on 25 Jun 2013, 23:01 last edited by
                                #18

                                [quote author="mbrasser" date="1317857789"]Hi,

                                For QtQuick 2.0 we are introducing an additional "module API", which should allow you to implement the singleton pattern more easily.

                                [/quote]

                                So, is the new module api the function qmlRegisterSingletonType?

                                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