Bonjour à toutes et à tous,
J'utilise Qt depuis quelques temps pour créer des interfaces graphiques et pourquoi ne pas utiliser QML, ce que j'ai essayé de faire à travers une application très simple pour me faire la main. Patatras ça ne fonctionne pas. La connexion entre le signal de la méthode C++ et du signal handler de QML reste inactive. Voici mon code:
currenttime.h
Code Qt : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #define CURRENTTIME_H #include <QObject> #include <QString> class CurrentTime : public QObject { Q_OBJECT Q_PROPERTY(QString showTime READ showTime WRITE setShowTime NOTIFY showTimeChanged) public: explicit CurrentTime(QObject *parent = nullptr); void setShowTime(const QString &showTime); private: QString m_showTime; signals: void showTimeChanged(); public slots: QString showTime(); }; #endif // CURRENTTIME_H
currenttime.cpp
Code Qt : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 #include "currenttime.h" #include <QTimer> #include <QTime> #include <QDebug> CurrentTime::CurrentTime(QObject *parent) : QObject(parent) { QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &CurrentTime::showTime); timer->start(1000); } QString CurrentTime::showTime() { m_showTime = QTime::currentTime().toString("hh:mm:ss"); qDebug(m_showTime.toLatin1()); return m_showTime; } void CurrentTime::setShowTime(const QString &showTime) { if (showTime == m_showTime) return; else { m_showTime = showTime; emit showTimeChanged(); } }
main.cpp
Code Qt : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QString> #include "currenttime.h" int main(int argc, char **argv) { QGuiApplication app(argc, argv); qmlRegisterType<CurrentTime>("myLibrary", 1, 0, "CurrentTime"); QQmlApplicationEngine engine; engine.load(QStringLiteral("qrc:/main.qml")); // QObject* root = engine.rootObjects().value(0); // QObject* timeText = root->findChild<QObject*>("timeText"); // timeText->setProperty("text", QVariant("Coucou from Main()")); return app.exec(); }
Main.qml
Mon horloge n'est pas remise à jour, et qml semble ignorer totalement les événements C++ ???
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 import QtQuick 2.5 import QtQuick.Controls 2.4 import myLibrary 1.0 ApplicationWindow { id: root visible: true width: 300 height: 100 CurrentTime{ id: currentime onShowTimeChanged: console.log("showTime connections", currentime.showTime) } MouseArea{ anchors.fill: parent onClicked: console.log("showTime clicked", currentime.showTime) } Text { id:timeText anchors.centerIn: parent objectName: "timeText" text: currentime.showTime font.bold: true font.pointSize: 40 Connections{ target:currentime onShowTimeChanged: console.log("showTime connections", currentime.showTime) } } }
Merci de vos suggestions et bien cordialement
Partager