Hi,

the MDeclarativeCache used by sailfishapplication.cpp is supposed to
give you a boost at application startup. Besides that
sailfishapplication.cpp contains a few more optimizations (e.g.
rendering flags for the graphics view). Its purpose is about the same
as the qmlapplicationviewer from Harmattan (which IMHO was bloated by
handling Symbian and desktop support as well).

You don't have to use sailfishapplication.cpp in your project and you
could set the rendering optimization flags yourself on a
QDeclarativeView. It's just easier to use the sailfishapplication.cpp.
People who only want to code with QML/JavaScript can treat it as a
black box  and still get an application running.

There are basically two ways of combining C++ objects with QML and
both can be used with sailfishapplication.cpp as well.

1. qmlRegisterType

Any QObject-based class can be exported to a QML namespace this way.
After importing the namespace in QML, the objects can be used like
normal QML elements. Instances of the objects are created in QML.
Use qmlRegisterType before loading the main.qml into the QDeclarativeView.

2. view->rootContext()->setContextProperty

QObjects instantiated in C++ code and primitive types can be made
available to QML this way. This can be useful, if your app is mainly
C++, and you just want to expose a facade for UI control to QML.


For combining QML with C++, you should remember a few rules:

* Any QObject can be exposed to QML.

* QML element properties are implemented using the Q_PROPERTY macro in
the C++ class header.

* Any public slot can be invoked from QML.

* Any public method declared with Q_INVOKABLE can be invoked from QML
(internally, Q_INVOKABLE converts the method to a slot).

* QML can connect to any signal of a QObject, e.g. use onMySignal in
QML to connect to a signal called mySignal.

* JavaScript function references (e.g. for callbacks) can be passed to
C++ as a parameter of type QScriptValue. The QScriptValue object can
be executed in C++ with its call method.

* Arbitrary QObject based objects can be passed from QML to C++ as
QVariant from which you can retrieve a QObject* and then cast to a
specific type with qobject_cast.

* A model for QML ListView (or SilicaListView) and similar elements
can be quickly implemented by deriving from a QAbstractListModel and
implementing the virtual methods rowCount and data.

* Simple list models can be exposed to QML by returning a QStringList
or QList<QVariant>.

* Structs or hashtables can be exposed to QML by returning a
QVariantMap. In QML you use the . notation to access the fields, e.g.
myData.value.

* Graphical QML elements can be implemented by deriving from
QDeclarativeItem and implementing the virtual paint method.

* Any C++ method invoked from QML should return quickly as it is
blocking the UI thread.


Martin
_______________________________________________
SailfishOS.org Devel mailing list

Reply via email to