[Qt-qml] Accessing C++ singleton from QML

Bradley Smith bsmith at baysmith.com
Thu Oct 28 09:25:11 CEST 2010


If you set a context property on the rootContext of the
QDeclarativeView, you can access public slots on the property from
QML.

C++:
    view->rootContext()->setContextProperty("mySettings",
Settings::instance());

QML:
    MouseArea { onClicked: { mySettings.doSomethingSlot() }  }


On Oct 27, 12:04 pm, Alex <alexlordax... at gmail.com> wrote:
> Hi all,
>
> This is probably a silly question, but how do I access a singleton
> instance of a C++ class from QML?
>
> For example, I have:
>
> class Settings : QObject {
> public:
>   static Settings *instance();
>
> private:
>   Settings();
>
> };
>
> In my QML code, I want to do the moral equivalent of the following:
>
> property variant mySettings : Settings::instance()
>
> What is the recommended way of doing this?
>
> Thanks,
> Alex
>
> PS:  On a related note, I am using "variant" above because I can't
> even qmlRegisterType the Settings class, since it doesn't have a
> public default constructor.
> _______________________________________________
> Qt-qml mailing list
> Qt-... at trolltech.comhttp://lists.trolltech.com/mailman/listinfo/qt-qml




More information about the Qt-qml mailing list