Hi Antonio
Here is a simplified example from one of my projects, showing a number
of ways to expose C++ to QML.
I tend to use qmlRegisterXXXX when exposing C++ objects with methods,
and SetContextProperty when I want to expose simple stuff like global
constants only.
I do things that way because it works for me ...
Chris
#ifdef QT_QML_DEBUG
#include <QtQuick>
#endif
#include <QGuiApplication>
#include <QQuickView>
#include <QQmlContext>
#include <sailfishapp.h>
//#include "landedtorch.h" //harmattan
#include "gsttorch.h" //sailfish
#include "satinfosource.h"
#include "operatingsystem.h"
#include "windowingsystem.h"
#include "telepathyhelper.h"
#include "landedtheme.h"
#include "jsonstorage.h"
//HELPER Functions getting system information which will be exported to QML
bool isSimulator()
{
bool simulator;
#if defined(QT_SIMULATOR)
simulator = true;
#else
// real device or emulator
simulator = false;
#endif
return simulator;
}
int getOperatingSystem()
{
//C++ representation of the OperatingSystem enumg
const OperatingSystem Os;
int OSId;
#if defined(Q_OS_SYMBIAN)
OSId = Os.Symbian;
#elif defined(Q_OS_MAC64)
OSId = Os.Mac64;
#elif defined(Q_OS_UNIX)
OSId = Os.Unix;
#elif defined(Q_OS_WIN32)
OSId = Os.Win32;
#elif defined(Q_OS_WIN64)
OSId = Os.Win64;
#elif defined(Q_OS_WINCE)
OSId = Os.WinCE;
#elif defined(Q_OS_SIMULATOR)
OSId = Os.Simulator;
#else
// other OS
OSId = Os.Other;
#endif
return OSId;
}
//END HELPER Functions
static QObject *theme_singletontype_provider(QQmlEngine *engine,
QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
LandedTheme *landedTheme = new LandedTheme();
return landedTheme;
}
static QObject *jsonStorage_singletontype_provider(QQmlEngine *engine,
QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
JSONStorage *jsonStorage = new JSONStorage();
return jsonStorage;
}
int main(int argc, char *argv[])
{
bool simulator = isSimulator();
//expose an enum of operating systems types to QML
qmlRegisterUncreatableType<OperatingSystem>("OperatingSystem", 1,
0, "OperatingSystem", "");
int OperatingSystemId = getOperatingSystem();
qmlRegisterType<GstTorch>("GstTorch",1,0,"GstTorch");
qmlRegisterType<TelepathyHelper>("TelepathyHelper",1,0,"TelepathyHelper");
qmlRegisterType<SatInfoSource>("SatInfoSource",1,0,"SatInfoSource");
qmlRegisterSingletonType<LandedTheme>("LandedTheme", 1, 0,
"LandedTheme", theme_singletontype_provider);
qmlRegisterSingletonType<JSONStorage>("JSONStorage",1,0,"JSONStorage",
jsonStorage_singletontype_provider);
QScopedPointer<QGuiApplication> app(SailfishApp::application(argc, argv));
QScopedPointer<QQuickView> view(SailfishApp::createView());
view->rootContext()->setContextProperty("OperatingSystemId",
OperatingSystemId);
view->rootContext()->setContextProperty("simulator", simulator);
view->setSource(SailfishApp::pathTo("qml/landed26_QT5.qml"));
view->show();
view->showFullScreen();
return app->exec();
}
Zitat von "Andrey Kozhevnikov" <coderusin...@gmail.com>:
???
contextProperty should be set BEFORE loading QML source.
On 25.02.2014 21:33, martin.gri...@gmail.com wrote:
Hi,
you should set the context property after loading the QML source.
Then it ought to be available from QML.
Martin
Am Tue Feb 25 2014 15:06:39 GMT+0100 (CET) schrieb
antonio.cano.go...@ovi.com:
Hi,
I am trying to do a simple Sailfish aplication that uses a c++
library. I try to connect my qml code with the C++ code.
First I create QObject extended library:
#ifndef BLOOMFILTERS_H
#define BLOOMFILTERS_H
#include <QObject>
#include "svn/bloom-read-only/bloom_filter.hpp"
class Bloomfilters : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void generateFilterInvokable2() const ;
Q_INVOKABLE void generateFilterInvokable(const QString
&cadenaK, const QString &cadenaM) const ;
explicit Bloomfilters(QObject *parent = 0);
void generateFilter() ;
Q_INVOKABLE void insertElement(const QString &cadena) ;
Q_INVOKABLE bool checkElement(const QString &cadena) ;
signals:
public slots:
private:
bloom_parameters _parameters ;
bloom_filter _filter;
bool _filter_charged;
QString _k;
};
#endif // BLOOMFILTERS_H
An I try to connect this with my sailfish application with the code:
QGuiApplication *app = SailfishApp::application(argc, argv);
QQuickView *view = SailfishApp::createView();
Bloomfilters * bloomfilters = new Bloomfilters();
view->rootContext()->setContextProperty("Bloomfilters",
bloomfilters);
view->setSource(SailfishApp::pathTo("qml/bloomfilter.qml"));
view->showFullScreen();
return app->exec();
But this is not working.
Someone have any idea about what am I doing wrong?
Best Regards
_______________________________________________
SailfishOS.org Devel mailing list
_______________________________________________
SailfishOS.org Devel mailing list