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
+4 -2
View File
@@ -51,7 +51,8 @@ HEADERS += \
src/core/common/qttools/qthelper/qthelper.h \ src/core/common/qttools/qthelper/qthelper.h \
src/core/common/qttools/qttoolswrapper.h \ src/core/common/qttools/qttoolswrapper.h \
src/core/common/qttoolsinterface.h \ src/core/common/qttoolsinterface.h \
src/gui/qml/cpp/qmlwrapper.h src/gui/qml/cpp/qmlwrapper.h \
src/gui/qml/cpp/qmlconfig.h
SOURCES += \ SOURCES += \
@@ -60,7 +61,8 @@ SOURCES += \
src/core/common/qttools/qthelper/qthelper.cpp \ src/core/common/qttools/qthelper/qthelper.cpp \
src/core/common/qttools/qttoolswrapper.cpp \ src/core/common/qttools/qttoolswrapper.cpp \
src/core/common/qttoolsinterface.cpp \ src/core/common/qttoolsinterface.cpp \
src/gui/qml/cpp/qmlwrapper.cpp src/gui/qml/cpp/qmlwrapper.cpp \
src/gui/qml/cpp/qmlconfig.cpp
LIBS += \ LIBS += \
-lpokerth_lib \ -lpokerth_lib \
+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 <QtCore>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QQmlContext>
#include <configfile.h> #include <configfile.h>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include "qmlconfig.h"
#include "qmlwrapper.h" #include "qmlwrapper.h"
QmlWrapper::QmlWrapper(boost::shared_ptr<ConfigFile> c) QmlWrapper::QmlWrapper(boost::shared_ptr<ConfigFile> c)
:QObject(), myConfig(c) :QObject(), myConfig(c)
{ {
qDebug("QmlWrapperKonstruktor");
myEngine.reset(new QQmlApplicationEngine); myEngine.reset(new QQmlApplicationEngine);
myQmlConfig = new QmlConfig(myConfig); //need to be a default pointer because QML doesnt support shared_ptr()
// Add c++ content here // Add c++ content here
// myEngine.rootContext()->setContextProperty("model1", &model1); myEngine->rootContext()->setContextProperty("Config", myQmlConfig);
myEngine->load(QUrl(QStringLiteral("qrc:/main.qml"))); myEngine->load(QUrl(QStringLiteral("qrc:/main.qml")));
} }
+2
View File
@@ -4,6 +4,7 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
class ConfigFile; class ConfigFile;
class QmlConfig;
class QQmlApplicationEngine; class QQmlApplicationEngine;
class QmlWrapper: public QObject class QmlWrapper: public QObject
@@ -19,6 +20,7 @@ public slots:
private: private:
boost::shared_ptr<QQmlApplicationEngine> myEngine; boost::shared_ptr<QQmlApplicationEngine> myEngine;
boost::shared_ptr<ConfigFile> myConfig; boost::shared_ptr<ConfigFile> myConfig;
QmlConfig *myQmlConfig;
}; };
#endif // QMLWRAPPER_H #endif // QMLWRAPPER_H
@@ -35,7 +35,7 @@ Rectangle {
ListElement { value: "2" } ListElement { value: "2" }
] ]
myValueIsIndex: false myValueIsIndex: false
myValue: "10" myValue: ""
} }
ListElement { ListElement {
myId: "spinBox_StartCash" myId: "spinBox_StartCash"
@@ -44,7 +44,7 @@ Rectangle {
myMaxValue: 1000000 myMaxValue: 1000000
myMinValue: 1000 myMinValue: 1000
myPrefix: "$" myPrefix: "$"
myValue: "5000" myValue: ""
} }
ListElement { ListElement {
myId: "comboBox_Blinds" myId: "comboBox_Blinds"
@@ -54,7 +54,7 @@ Rectangle {
ListElement { value: qsTr("Use saved blinds settings") }, ListElement { value: qsTr("Use saved blinds settings") },
ListElement { value: qsTr("Change blinds settings ...") } ListElement { value: qsTr("Change blinds settings ...") }
] ]
myValue: "0" myValue: ""
myValueIsIndex: true myValueIsIndex: true
} }
ListElement { ListElement {
@@ -74,7 +74,7 @@ Rectangle {
ListElement { value: "2" }, ListElement { value: "2" },
ListElement { value: "1" } ListElement { value: "1" }
] ]
myValue: "5" myValue: ""
myValueIsIndex: false myValueIsIndex: false
} }
ListElement { ListElement {
@@ -94,9 +94,17 @@ Rectangle {
ListElement { value: "2" }, ListElement { value: "2" },
ListElement { value: "1" } ListElement { value: "1" }
] ]
myValue: "5" myValue: ""
myValueIsIndex: false myValueIsIndex: false
} }
Component.onCompleted: {
createLocalGameViewModel.setProperty(0, "myValue", Config.readConfigInt("NumberOfPlayers"));
createLocalGameViewModel.setProperty(1, "myValue", Config.readConfigInt("StartCash"));
createLocalGameViewModel.setProperty(2, "myValue", "0");
createLocalGameViewModel.setProperty(3, "myValue", Config.readConfigInt("GameSpeed"));
createLocalGameViewModel.setProperty(4, "myValue", Config.readConfigInt("GameSpeed"));
}
} }
delegate: MyListViewDelegateFactory { delegate: MyListViewDelegateFactory {
myListViewsHeight: createLocalGameViewList.height myListViewsHeight: createLocalGameViewList.height