Hi Alex,

On 28/10/2010, at 5:04 AM, ext Alex 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()
> 

The static function can't be directly called from QML.

One way to do it would be to return Settings::instance() from a property 
instead:

class MyAppData : public QObject
{
        Q_OBJECT
        Q_PROPERTY(Settings* settings READ settings)

public:
        Settings *settings() const { return Settings::instance(); }
};

Then if MyAppData and Settings are both registered as QML types, you can do:

// qml

MyAppData { id: data }

property variant settings: data.settings


For the Settings class, if you register it using the version of 
qmlRegisterType() that takes no arguments, it does not need a default 
constructor.



regards,

Bea


_______________________________________________
Qt-qml mailing list
Qt-qml@trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-qml

Reply via email to