c++ - Qt Signals and Slots - nothing happens -


i'm trying connect qml signal c++ slot unsuccessfully.

i had on several other examples think didn't got how root object of qml document...

my problem is, seems signal sent qml file, not received in cpp file. there no errors when execute code.

//counter.h #ifndef counter_h #define counter_h  #include <qobject>  class counter : public qobject {     q_object  private:     int counter;  public:     explicit counter(qobject *parent = 0);  signals:     void counterhaschanged(int);  public slots:     void click(); };  #endif // counter_h   //counter.cpp #include "counter.h" #include <qdebug>  counter::counter(qobject *parent) :     qobject(parent) {     this->counter = 0;      qdebug() << "class counter created"; }  void counter::click() {     this->counter++;      qdebug() << "clickregistered() - emit counterhaschanged()";      emit counterhaschanged(counter); }    //main.cpp #include <qtgui/qguiapplication> #include "qtquick2applicationviewer.h" #include "counter.h" #include <qobject> #include <qtquick> #include <qdebug> #include <qquickitem> #include <qqmlcontext> #include <qtcore>  int main(int argc, char *argv[]) {     qguiapplication app(argc, argv);      qtquick2applicationviewer viewer;     viewer.setmainqmlfile(qstringliteral("qml/statestest2/main.qml"));     viewer.showexpanded();      qquickview view;     view.setsource(qurl::fromlocalfile("qml/statestest2/main.qml"));      qobject *item = view.rootobject();      counter counter;      qobject::connect(item, signal(click()), &counter, slot(click()));      return app.exec(); }    //main.qml import qtquick 2.0  rectangle {     id: root     width: 360     height: 360      signal click      text {         text: qstr("hello world")         anchors.centerin: parent     }      mousearea {          anchors.fill: parent         onclicked: {             root.click             console.log("click() qml")         }     }      text {         text: "clicks: "         x: 30         y: 250     }      text {         id: counter         text: "0"         x: 75         y: 250     } } 

i know there tons of topics this.. reason, no other solution worked me.. maybe should change ide :d

i suggest add counter context property.. requires following changes.

//counter.h #ifndef counter_h #define counter_h  #include <qobject>  class counter : public qobject {     q_object  private:     int counter;  public:     explicit counter(qobject *parent = 0);  signals:     void counterhaschanged(int value);  public slots:     void click(); };  #endif // counter_h 

the counter.cpp file

//counter.cpp #include "counter.h" #include <qdebug>  counter::counter(qobject *parent) :     qobject(parent) {     this->counter = 0;      qdebug() << "class counter created"; }  void counter::click() {     this->counter++;      qdebug() << "clickregistered() - emit counterhaschanged()";      emit counterhaschanged(counter); } 

the main.cpp file

   //main.cpp #include <qtgui/qguiapplication> #include "qtquick2applicationviewer.h" #include "counter.h" #include <qobject> #include <qtquick> #include <qdebug> #include <qquickitem> #include <qqmlcontext> #include <qtcore>  int main(int argc, char *argv[]) {     qguiapplication app(argc, argv);      counter counter;     qtquick2applicationviewer viewer;     viewer.rootcontext()->setcontextproperty("counterobj",&counter);      viewer.setmainqmlfile(qstringliteral("qml/so_qmlcppcommunication/main.qml"));       viewer.showexpanded();      return app.exec(); } 

and in qml file, can access slot of counter object referring counterobj.

  //main.qml import qtquick 2.0  rectangle {     id: root     width: 360     height: 360      signal click      text {         text: qstr("hello world")         anchors.centerin: parent     }      mousearea {          anchors.fill: parent         onclicked: {             counterobj.click()             console.log("click() qml")         }     }  text {     text: "clicks: "     x: 30     y: 250 }  text {     id: counter     text: "0"     x: 75     y: 250 } connections {     id:cppconnection     target:counterobj     ignoreunknownsignals : true     oncounterhaschanged:{           //to access signal parameter,please name parameter.           console.debug("counter value changed")           counter.text = value    } }  } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -