c++ - How to connect QQuickitem with Qml Object? -


i trying make simple program allow user connect specific websites via clicking image. here code:

account.h:

#ifndef accounts_h #define accounts_h #include <qobject> #include <qurl> #include <qdesktopservices> class accounts : public qobject {     q_object public:     explicit accounts(qobject* parent = 0) : qobject(parent){} public slots:  void gmailopen(const qstring &msg) {     qurl gmailurl(msg);     qdesktopservices::openurl(gmailurl); } }; #endif // accounts_h 

main.cpp:

#include <qtgui/qguiapplication> #include <qtquick/qquickview> #include <qtqml> #include "accounts.h" int main(int argc, char *argv[]) {     qguiapplication app(argc, argv);     qquickview *view = new qquickview;     qobject *gmail = view->rootobject().findchild<qobject*>("gmaillink");     accounts *gmailaccount = new accounts;     qobject::connect(gmail, signal(gmailsignal(qstring)),gmailaccount,slot(gmailopen(qstring)));     view->setsource(qurl::fromlocalfile("/users/yudelin/documents/mycrazyprojects/managers4manyaccounts/main.qml"));     view->show();     return app.exec(); } 

main.qml:

import qtquick 2.0  rectangle {      width: 360     height: 360     color: "silver"      image {         id:gmaillink         objectname: "gmaillink"         width: 102         height: 199         fillmode: image.preserveaspectfit         source: "...." //the url long omit it.         anchors.centerin: parent         signal gmaisignal (string msg)         mousearea {             anchors.fill: parent             onclicked:                 gmaillink.gmailsignal("http://mail.google.com")         }     }  } 

as can see, trying connect qml image c++ object accounts. use qobject *gmail = view->rootobject().findchild<qobject*>("gmaillink"); fetch qml object. not work. guess suit older qt version. how fix problem?

  1. you want gmailopen slot called on clicking on image. there simpler , preferred ways of doing other doing this.

    qobject *gmail = view->rootobject().findchild("gmaillink");

  2. as @kunal pointed out, can use qt.openurlexternally(string).

  3. if want open url using qdesktopservices::openurl gmailopen slot, can call slot directly setting context property.

4, qobject *gmail = view->rootobject().findchild<qobject*>("gmaillink");... here before setting qml file, trying reference gmaillink object. not created yet.

  1. following code explains take on problem

    5a. accounts.h file


#ifndef accounts_h #define accounts_h #include <qobject> #include <qurl> #include <qdesktopservices> class accounts : public qobject {     q_object public:     explicit accounts(qobject* parent = 0) : qobject(parent){} public slots:  void gmailopen(const qstring &msg) {     qurl gmailurl(msg);     qdesktopservices::openurl(gmailurl); } }; #endif // accounts_h 

5b. main.cpp file


#include <qtgui/qguiapplication> #include <qtquick/qquickview> #include <qtqml> #include "accounts.h" int main(int argc, char *argv[]) {     qguiapplication app(argc, argv);     accounts *gmailaccount = new accounts;     qquickview *view = new qquickview;     view->engine()->rootcontext()->setcontextproperty("accounts",gmailaccount);     view->setsource(qurl::fromlocalfile("qml/so_openexternallink/main.qml"));     view->show();     return app.exec(); }  5c. main.qml file 

import qtquick 2.1 import qtquick.dialogs 1.0 rectangle {     width: 360     height: 360     color: "silver"     image {         id:gmaillink         objectname: "gmaillink"         width: 102         height: 199         fillmode: image.preserveaspectfit         source: "http://upload.wikimedia.org/wikipedia/commons/4/41/flag_of_india.svg"         anchors.centerin: parent         mousearea {             anchors.fill: parent             onclicked:qt.openurlexternally("http://mail.google.com")         }     }     image{         id:secondimage         width:102         height:199         fillmode:image.preserveaspectfit         source: "http://upload.wikimedia.org/wikipedia/commons/5/55/emblem_of_india.svg"         anchors.left:gmaillink.right         anchors.top: gmaillink.top         mousearea {             anchors.fill: parent             onclicked:filedialog.visible = true         }     }     filedialog {         id: filedialog         title: "please choose file"         namefilters: [ "image files (*.jpg *.png)", "all files (*)" ]         visible:false;         selectmultiple:false         onaccepted:secondimage.source = filedialog.fileurl     }  } 

5d. need change following above code

i. image source

ii. qml file path in main.cpp

iii. possibly import qtquick 2.0 if don't have qtquick 2.1 yet.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -