diff --git a/2024/tp3/main.cpp b/2024/tp3/main.cpp index 38f5bd3..2026efe 100644 --- a/2024/tp3/main.cpp +++ b/2024/tp3/main.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "synchronisable.h" @@ -26,10 +28,27 @@ int main(int argc, char *argv[]) Synchronisable s{42}, copie{1}; + qDebug() << "==Sans connexion=="; s.setCpt(122); + qDebug() << "s: " << s.cpt() << "copie: " << copie.cpt() ; + // Connecte le signal cptChanged de s au slot setCpt de copie + QObject::connect(&s, &Synchronisable::cptChanged, + &copie, &Synchronisable::setCpt); + + s.setCpt(9); + qDebug() << "==Avec connexion=="; + qDebug() << "s: " << s.cpt() << "copie: " << copie.cpt() ; + + QTimer timer; + + QObject::connect(&timer, &QTimer::timeout, &s, &Synchronisable::inc); + QObject::connect(&s, &Synchronisable::cptChanged, [&s](){ qDebug() << s.cpt(); } + ); + timer.start(1000); + + engine.rootContext()->setContextProperty("miam", &s); - qDebug() << s.cpt() << "\n"; engine.load(url); diff --git a/2024/tp3/main.qml b/2024/tp3/main.qml index e2d2e56..61536ce 100644 --- a/2024/tp3/main.qml +++ b/2024/tp3/main.qml @@ -6,4 +6,9 @@ Window { height: 480 visible: true title: qsTr("Hello World") + + Text { + text: miam.cpt + font.pointSize: 30 + } } diff --git a/2024/tp3/synchronisable.cpp b/2024/tp3/synchronisable.cpp index d5ca4a3..f8d69a2 100644 --- a/2024/tp3/synchronisable.cpp +++ b/2024/tp3/synchronisable.cpp @@ -17,3 +17,8 @@ void Synchronisable::setCpt(int newCpt) m_cpt = newCpt; emit cptChanged(m_cpt); } + +void Synchronisable::inc() +{ + setCpt(m_cpt+1); +} diff --git a/2024/tp3/synchronisable.h b/2024/tp3/synchronisable.h index caa19ed..4bd2ffb 100644 --- a/2024/tp3/synchronisable.h +++ b/2024/tp3/synchronisable.h @@ -12,6 +12,7 @@ public: int cpt() const; void setCpt(int newCpt); + void inc(); signals: void cptChanged(int);