Created
November 26, 2015 13:34
-
-
Save ultramcu/895c0d63645b6b73a237 to your computer and use it in GitHub Desktop.
Exposing Attributes of C++ Types to QML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "calculator.h" | |
calculator::calculator() | |
{ | |
this->num = 0; | |
} | |
qint32 calculator::get_number() | |
{ | |
return this->num; | |
} | |
void calculator::set_number(const qint32 &num) | |
{ | |
this->num = this->num + num; | |
emit number_changed(); | |
} | |
bool calculator::do_clear() | |
{ | |
this->num = 1; | |
emit number_changed(); | |
return true; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef CALCULATOR_H | |
#define CALCULATOR_H | |
#include <QObject> | |
#include <QGuiApplication> | |
#include <QQmlApplicationEngine> | |
#include <QQmlEngine> | |
#include <QQmlContext> | |
#include <QQmlComponent> | |
class calculator : public QObject | |
{ | |
Q_OBJECT | |
Q_PROPERTY(qint32 number READ get_number WRITE set_number NOTIFY number_changed) | |
Q_PROPERTY(bool clear READ do_clear NOTIFY number_changed) | |
private: | |
qint32 num; | |
public: | |
calculator(); | |
qint32 get_number(); | |
void set_number(const qint32 &num); | |
bool do_clear(); | |
signals: | |
void number_changed(); | |
public slots: | |
}; | |
#endif // CALCULATOR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QGuiApplication> | |
#include <QQmlApplicationEngine> | |
#include <QQmlEngine> | |
#include <QQmlContext> | |
#include <QQmlComponent> | |
#include "calculator.h" | |
int main(int argc, char *argv[]) | |
{ | |
qmlRegisterType<calculator>("my.calculator", 1, 0, "Cal"); | |
QGuiApplication app(argc, argv); | |
QQmlApplicationEngine engine; | |
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); | |
return app.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick 2.3 | |
import QtQuick.Window 2.2 | |
import my.calculator 1.0 | |
Window { | |
id: main | |
visible: true | |
width: 400 | |
height: 300 | |
Cal | |
{ | |
id : cal | |
Component.onCompleted: | |
{ | |
number = 1; | |
} | |
} | |
Rectangle { | |
id: rec_show | |
x:0 | |
y:0; | |
width: main.width | |
height: main.height/3; | |
color:"#fa5f5f" | |
border.color: "gray" | |
border.width: 2 | |
Text | |
{ | |
anchors.centerIn: parent | |
text: cal.number | |
font.pixelSize: 80 | |
} | |
} | |
Rectangle { | |
id: rec_plus | |
x:0 | |
y:rec_show.y + rec_show.height; | |
width: main.width | |
height: main.height/3; | |
color:"#76fa5f" | |
border.color: "gray" | |
border.width: 2 | |
Text { | |
font.pixelSize: 100; | |
text: qsTr("++") | |
anchors.centerIn: parent | |
} | |
MouseArea { | |
anchors.fill: parent | |
onClicked: { | |
cal.number = 1 | |
} | |
} | |
} | |
Rectangle { | |
id: rec_clear | |
x:0 | |
y:rec_plus.y + rec_plus.height; | |
width: main.width | |
height: main.height/3; | |
color:"#5faffa" | |
border.color: "gray" | |
border.width: 2 | |
Text { | |
font.pixelSize: 80; | |
text: qsTr("clear") | |
anchors.centerIn: parent | |
} | |
MouseArea { | |
anchors.fill: parent | |
onClicked: { | |
cal.clear | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment