Put c++ data to QML the first time: YEAAAAHHHH ;-)

This commit is contained in:
Felix Hammer
2015-08-13 22:36:11 +02:00
parent 93b9881e75
commit ee897f17ef
8 changed files with 66 additions and 47 deletions
+19
View File
@@ -0,0 +1,19 @@
#include "qmlconfig.h"
#include "configfile.h"
QmlConfig::QmlConfig(boost::shared_ptr<ConfigFile> c)
:QObject(), myConfig(c)
{
}
QmlConfig::~QmlConfig() {}
QmlConfig::QmlConfig(const QmlConfig&) :QObject() {}
QString QmlConfig::readConfigString(QString varName) {
return QString::fromUtf8(myConfig->readConfigString(varName.toStdString()).c_str());
}
QString QmlConfig::readConfigInt(QString varName) {
return QString::number(myConfig->readConfigInt(varName.toStdString()));
}
+24
View File
@@ -0,0 +1,24 @@
#ifndef QMLCONFIG_H
#define QMLCONFIG_H
#include <QtCore>
#include <boost/shared_ptr.hpp>
class ConfigFile;
class QmlConfig: public QObject
{
Q_OBJECT
public:
Q_INVOKABLE QString readConfigString(QString);
Q_INVOKABLE QString readConfigInt(QString);
QmlConfig(boost::shared_ptr<ConfigFile>);
~QmlConfig();
QmlConfig(const QmlConfig&);
private:
boost::shared_ptr<ConfigFile> myConfig;
};
#endif // QMLCONFIG_H
-19
View File
@@ -1,19 +0,0 @@
#include "qmlconfigwrapper.h"
#include "configfile.h"
QmlConfigWrapper::QmlConfigWrapper(ConfigFile *c)
:QObject(), myConfig(c)
{
}
QmlConfigWrapper::~QmlConfigWrapper()
{
}
QmlConfigWrapper::QmlConfigWrapper(const QmlConfigWrapper&)
:QObject()
{
}
-19
View File
@@ -1,19 +0,0 @@
#ifndef QMLCONFIGWRAPPER_H
#define QMLCONFIGWRAPPER_H
#include <QtCore>
class ConfigFile;
class QmlConfigWrapper: public QObject
{
Q_OBJECT
public:
QmlConfigWrapper(ConfigFile *c);
~QmlConfigWrapper();
QmlConfigWrapper(const QmlConfigWrapper&);
private:
ConfigFile *myConfig;
};
#endif // QMLCONFIGWRAPPER_H
+4 -2
View File
@@ -1,18 +1,20 @@
#include <QtCore>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <configfile.h>
#include <boost/shared_ptr.hpp>
#include "qmlconfig.h"
#include "qmlwrapper.h"
QmlWrapper::QmlWrapper(boost::shared_ptr<ConfigFile> c)
:QObject(), myConfig(c)
{
qDebug("QmlWrapperKonstruktor");
myEngine.reset(new QQmlApplicationEngine);
myQmlConfig = new QmlConfig(myConfig); //need to be a default pointer because QML doesnt support shared_ptr()
// Add c++ content here
// myEngine.rootContext()->setContextProperty("model1", &model1);
myEngine->rootContext()->setContextProperty("Config", myQmlConfig);
myEngine->load(QUrl(QStringLiteral("qrc:/main.qml")));
}
+2
View File
@@ -4,6 +4,7 @@
#include <boost/shared_ptr.hpp>
class ConfigFile;
class QmlConfig;
class QQmlApplicationEngine;
class QmlWrapper: public QObject
@@ -19,6 +20,7 @@ public slots:
private:
boost::shared_ptr<QQmlApplicationEngine> myEngine;
boost::shared_ptr<ConfigFile> myConfig;
QmlConfig *myQmlConfig;
};
#endif // QMLWRAPPER_H