More QML!

This commit is contained in:
Christoph Sterz 2022-10-10 10:43:56 +02:00
parent b6eee3a9d9
commit 14631a937d
3 changed files with 9 additions and 7 deletions

View File

@ -6,13 +6,14 @@
class CounterManager : public QObject class CounterManager : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit CounterManager(QObject *parent = nullptr); explicit CounterManager(QObject *parent = nullptr);
int counter() const;
signals: signals:
protected: protected:
private: private:
int m_counter; int m_counter;

View File

@ -13,6 +13,7 @@ int main(int argc, char *argv[])
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
auto counterManager = new CounterManager(); auto counterManager = new CounterManager();
engine.rootContext()->setContextProperty("_counterManager", counterManager); engine.rootContext()->setContextProperty("_counterManager", counterManager);
const QUrl url(QStringLiteral("qrc:/main.qml")); const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) { &app, [url](QObject *obj, const QUrl &objUrl) {

View File

@ -7,12 +7,12 @@ Window {
visible: true visible: true
title: qsTr("Hello World") title: qsTr("Hello World")
Text { Item {
text: "Counter is " + _counterManager.counter; anchors.fill: parent
anchors.centerIn: parent
MouseArea{ Text {
anchors.fill: parent id: text
onClicked: {_counterManager.increase()} text: "Hello, World!"
} }
} }
} }