Calling static function from QML? Doable?



  • Hi everyone,

    I'd like to call a static C++ function from QML, but no luck so far. The function is declared in a class NumberMachine exteding QObject like this:

    @
    Q_INVOKABLE static int number();
    @

    The implementation is straightforward, and the class is registered within QML using qmlRegisterUncreatableType() (as it has no public onstructor) ... but the following QML code fails to execute:

    @
    console.log("number: " +NumberMachine.number());
    @

    the engine complains: TypeError: Result of expression 'NumberMachine.number' [undefined] is not a function.

    My question is - is it possible to call static functions from QML at all? If so, does anyone have any idea what I'm missing?

    Thx.



  • Hello,

    you need an instance of NumberMachine

    @
    import QtQuick 1.0

    import MyApp 1.0

    Rectangle {
    width: 360
    height: 360
    Text {
    text: "Hello World"
    anchors.centerIn: parent
    }
    MouseArea {
    anchors.fill: parent
    onClicked: {
    console.log( numberMachine.number() )
    }
    }

    NumberMachine {
        id: numberMachine
    }
    

    }

    @



  • Yeah, that might work for some, but mu NumberMachine is a singleton class - i.e. I cannot create more than one instance of it. Anyway from the post you sent looks like there is no support for calling static functions from QML. Does it apply to QML only or is this unsupported in the script engine?

    Thx.



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



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


  • Moderators

    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.



  • 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 :(



  • 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 :(



  • 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 ...).


  • Moderators

    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.



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


  • Moderators

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



  • 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



  • [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?


  • Moderators

    [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



  • Thanks.



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



  • [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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.