Bonjour, j'ai un probleme de binding C++/QML. Je n'arrive pas a binder un bouton(child element du root) avec un objet. J'y arrive avec le root mais pas le child. Voila le code C++:
Code C++ : 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 Q_DECL_EXPORT int main(int argc, char *argv[]) { QScopedPointer<QApplication> app(createApplication(argc, argv)); TimerObject timerObj; QDeclarativeView view; view.setResizeMode(QDeclarativeView::SizeRootObjectToView); view.setSource(QUrl("../Resources/qml/qmlcpp/main.qml")); QGraphicsObject* rootObj = view.rootObject(); QDeclarativeItem *myButton = rootObj->findChild<QDeclarativeItem*>("mybutton"); int height = myButton->height(); QDeclarativeContext* ctxButton = qmlContext(myButton); ctxButton->setContextProperty("timer", &timerObj); return app->exec(); }
et le Qml :
le TimerObject :
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 // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 Rectangle { width: 360 height: 360 Text { y : 10; id:myText text: qsTr("Hello World") //anchors.centerIn: parent } MouseArea { anchors.fill: parent onClicked: { //Qt.quit(); //myText.text = timer.getCurrentDateTime(); //myText.text = "toto"; } } Button { id:mybutton anchors.centerIn:parent text: "Test Button" width: parent.width/2 -10 objectName: "mybutton" onClicked: { console.log("Add folder clicked"); text= timer.getCurrentDateTime(); } } }
Code C++ : 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 #ifndef TIMEROBJECT_H #define TIMEROBJECT_H #include <QObject> #include <QDateTime> class TimerObject : public QObject { Q_OBJECT public: TimerObject(); virtual ~TimerObject(); Q_INVOKABLE QDateTime getCurrentDateTime() const { return QDateTime::currentDateTime(); } }; #endif // TIMEROBJECT_H
Je rappelle bien que ca marche nickel si je bind le root au TimerObject avec :
Code C++ : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 QDeclarativeContext *ctxt = view.rootContext(); ctxt->setContextProperty("timer", &timerObj);
Merci d'avance
Partager