Results 1 to 9 of 9

Thread: c++ and qml binding

  1. #1
    Join Date
    Nov 2011
    Location
    coimbatore
    Posts
    80
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default c++ and qml binding

    hi,
    how to receive signal from c++ to qml....using qdeclarativeview i am able to bind qml with c++ but i don't know how to bind c++ with qml....

  2. The following user says thank you to ganeshgladish for this useful post:


  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: c++ and qml binding

    Expose a C++ object to QML engine using QDeclarativeContext::setContextProperty() and use one of the means available (either the Connections element or javascript object.signal.connect(slot) expression) to connect a QML function to such object's signal.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:


  5. #3
    Join Date
    Nov 2011
    Location
    coimbatore
    Posts
    80
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: c++ and qml binding

    thank you for your reply,
    here i am using qml for ui and c++ for database ....whenever i retrieve data from c++.... i want to give to qml..

  6. The following user says thank you to ganeshgladish for this useful post:


  7. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: c++ and qml binding

    You might want to consider exposing the data as a model.
    Maybe you can even use QSqlQueryModel?

    Cheers,
    _
    Last edited by anda_skoa; 8th May 2013 at 14:12.

  8. The following user says thank you to anda_skoa for this useful post:


  9. #5
    Join Date
    Nov 2011
    Location
    coimbatore
    Posts
    80
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: c++ and qml binding

    thank you for your reply,
    but error is comming like this,
    QMetaObject::invokeMethod: No such method QDeclarativeRectangle::myQmlFunction(QVariant)
    QML function returned: ""

    thank you for your reply,
    but error is comming like this,
    QMetaObject::invokeMethod: No such method QDeclarativeRectangle::myQmlFunction(QVariant)
    QML function returned: ""

  10. The following user says thank you to ganeshgladish for this useful post:


  11. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: c++ and qml binding

    Show your code please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:


  13. #7
    Join Date
    Nov 2011
    Location
    coimbatore
    Posts
    80
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: c++ and qml binding

    this is my qt-c++ code, here i am checking user name and password,if user name and password matching true means i am send string "hai" to my qml...in qml i am checking that string then i taking decision.....
    that userin and passin coming from qml using qdeclarative......


    Qt Code:
    1. Q_INVOKABLE void login_database(const QString &userin,const QString &passin)
    2. {
    3. QSqlQuery querylogin(db);
    4. if(db.isOpen())
    5. {
    6. querylogin.exec("SELECT * FROM hospital WHERE UserName=\'"+userin+"\'");
    7. if(!querylogin.exec())
    8. {
    9. QMessageBox::information(this, tr("Login Match"),tr("Sorry query is not execute"),QMessageBox::Ok);
    10. }
    11.  
    12. else
    13. {
    14. QString Pass,Uname;
    15. bool matchPass=FALSE;
    16. bool matchUname=FALSE;
    17. while (querylogin.next())
    18. {
    19. Uname=querylogin.value(1).toString();
    20. Pass = querylogin.value(2).toString();
    21.  
    22. if(Uname==userin)
    23. matchUname=TRUE;
    24.  
    25.  
    26. }
    27.  
    28.  
    29.  
    30.  
    31. if(!matchUname)
    32. {
    33.  
    34. QMessageBox::information(this, tr("Login Match"),tr("Invalid Username"),QMessageBox::Ok);
    35.  
    36. }
    37.  
    38. else if(!matchPass)
    39. {
    40. // loginlbl->setText("Wrong Password");
    41. QMessageBox::information(this, tr("Login Match"),tr("Invalid Password"),QMessageBox::Ok);
    42. return;
    43. }
    44.  
    45. else
    46. {
    47.  
    48. QMessageBox::information(this, tr("Login Match"),tr("Sucessfully ur login to database"),QMessageBox::Ok);
    49.  
    50.  
    51. QDeclarativeEngine engine1;
    52. QDeclarativeComponent component(&engine1, "main.qml");
    53. QObject *object = component.create();
    54.  
    55. QVariant returnedValue;
    56. QVariant msg = "Hello from C++";
    57. QMetaObject::invokeMethod(object, "myQmlFunction",
    58. Q_RETURN_ARG(QVariant, returnedValue),
    59. Q_ARG(QVariant, msg));
    60.  
    61. qDebug() << "QML function returned:" << returnedValue.toString();
    62. delete object;
    63.  
    64.  
    65.  
    66. }
    67.  
    68.  
    69. }
    70.  
    71.  
    72.  
    73.  
    74. }
    To copy to clipboard, switch view to plain text mode 


    Added after 7 minutes:


    here i am using qml for gui...here i am enter username and password,then i am click login button then that text goto c++ da

    javascript Code:
    1. // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    2. import QtQuick 1.1
    3.  
    4. Rectangle {
    5. id:hdcs_ui_login_rect1
    6. width: 1600
    7. height:900
    8. // color:"red"
    9.  
    10.  
    11. Image {
    12. id: back
    13.  
    14. anchors.fill: parent
    15. source: "img/backg.png"
    16. cache: true
    17.  
    18. }
    19.  
    20.  
    21. Rectangle{
    22.  
    23.  
    24. id:outline
    25. width: parent.width/1.2
    26. height: parent.height/1.2
    27. color: "transparent"
    28. anchors.centerIn: parent
    29.  
    30.  
    31.  
    32. Image {
    33. id:login
    34.  
    35. anchors.topMargin: 11
    36. source: "img/doctors_26.png"
    37. cache: true
    38.  
    39.  
    40. }
    41.  
    42. Rectangle{
    43.  
    44.  
    45. id:outline1
    46. width: parent.width/2
    47. height: parent.height/1.8
    48. color: "transparent"
    49. anchors.centerIn: parent
    50.  
    51.  
    52. Image {
    53. id:login1
    54.  
    55. anchors.topMargin: 11
    56. anchors.fill: parent
    57.  
    58.  
    59. // anchors.top:coll.top
    60. source: "img/newimage1.png"
    61. cache: true
    62.  
    63.  
    64. }
    65.  
    66.  
    67. Column
    68. {
    69. id:coll
    70. anchors.centerIn: parent
    71. spacing: 30
    72.  
    73.  
    74.  
    75. Column{
    76. spacing: 5
    77. Text {
    78. id: title
    79. text: qsTr("Login")
    80. font.pixelSize: 30
    81. color:"white"
    82. font.bold: true
    83. }
    84.  
    85. }
    86.  
    87.  
    88.  
    89.  
    90. Column{
    91. spacing: 5
    92. Text {
    93. id: name
    94. text: qsTr("User Id:")
    95. font.pixelSize: 20
    96. color:"white"
    97. }
    98. Rectangle{
    99. id:rect
    100. width: 300
    101. height: 30
    102. color: "white"
    103.  
    104. TextInput
    105. {
    106. id: userInputlog
    107. width: parent.width-6
    108. height: parent.height-6
    109. anchors.centerIn: parent
    110. font.pixelSize: 15
    111. font.bold: true
    112. maximumLength: 15
    113.  
    114. }
    115. }
    116. }
    117. Column
    118. {
    119. spacing: 5
    120. Text {
    121. id: passlog
    122. text: qsTr("Password:")
    123. font.pixelSize: 20
    124. // font.bold: true
    125. color:"white"
    126. }
    127. Rectangle{
    128. id:rect1
    129. width: 300
    130. height: 30
    131. color: "white"
    132.  
    133. TextInput
    134. {
    135. id: passwordInput
    136. width: parent.width-6
    137. height: parent.height-6
    138. anchors.centerIn: parent
    139. font.pixelSize: 15
    140. font.bold: true
    141. maximumLength: 15
    142. echoMode: TextInput.Password
    143.  
    144. }
    145. }
    146. }
    147.  
    148. Rectangle{
    149. // spacing: 100
    150. id:buttons
    151. width: parent.width
    152. color: "transparent"
    153.  
    154.  
    155. }
    156.  
    157. Row{
    158. spacing: 20
    159. anchors.horizontalCenter: parent.horizontalCenter
    160.  
    161.  
    162. Image {
    163. id: img
    164. source: "img/re1.png"
    165. fillMode: Image.PreserveAspectFit
    166. Text {
    167. id:log
    168. text: qsTr("Login")
    169. anchors.centerIn: parent
    170. color:"white"
    171. font.pixelSize: 20
    172. font.bold:true
    173.  
    174. }
    175.  
    176.  
    177. function myQmlFunction(msg) {
    178. console.log("Got message:", msg)
    179. return "some return value"
    180. }
    181.  
    182. MouseArea
    183. {
    184. anchors.fill: parent
    185. onPressed:
    186. {
    187. myObject.login_database(userInputlog.text,passlog.text);
    188.  
    189. function myQmlFunction(msg) {
    190. console.log("Got message:", msg);
    191.  
    192. if(msg == userInputlog.text)
    193. {
    194. loader.source = "HDCS_UI_DR_CHF.qml"
    195.  
    196. }
    197.  
    198.  
    199. }
    200.  
    201.  
    202.  
    203.  
    204. }
    205.  
    206.  
    207. }
    208.  
    209. }
    210.  
    211.  
    212. Image {
    213. id: img1
    214. source: "img/re1.png"
    215. Text {
    216. id:log2
    217. anchors.leftMargin: 20
    218. text: qsTr("Forgot Password")
    219. anchors.centerIn: parent
    220. color:"white"
    221. font.bold: true
    222. font.pixelSize: 20
    223. }
    224. }
    225.  
    226. }
    227. }
    228. }
    229.  
    230.  
    231.  
    232. }
    233.  
    234. Image {
    235. id: shutdown
    236. anchors.verticalCenterOffset: 350
    237. anchors.horizontalCenterOffset: 550
    238. anchors.horizontalCenter:parent.horizontalCenter
    239. anchors.verticalCenter: parent.verticalCenter
    240.  
    241. source: "img/power.png"
    242. cache: true
    243. smooth: true
    244.  
    245. MouseArea{
    246.  
    247. anchors.fill: parent
    248. onClicked:
    249. {
    250.  
    251. Qt.quit();
    252. }
    253.  
    254. }
    255.  
    256. }
    257.  
    258.  
    259. Loader {
    260. id: loader
    261.  
    262.  
    263. anchors.fill: parent
    264. focus: true
    265. }
    266.  
    267.  
    268. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 7th May 2013 at 20:07. Reason: missing [code] tags

  14. The following user says thank you to ganeshgladish for this useful post:


  15. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: c++ and qml binding

    Your code is bizzare... you're creating a QML UI as a response to SQL query? I have no idea what you are trying to acomplish but it looks like you should study the difference between declarative and imperative programming. Not speaking of trying to call some function on some object that is defined in a totally different object. No wonder it doesn't work, right?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. The following user says thank you to wysota for this useful post:


  17. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: c++ and qml binding

    If all you need is to communicate a successful login to the QtQuick UI, it would be a lot easier to have a property for that on your C++ object and to change it when login succeeds.

    The QML code can then just bind to that property.

    Something like
    Qt Code:
    1. loader.source = myobject.loggedIn ? "HDCS_UI_DR_CHF.qml" : ""
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  18. The following user says thank you to anda_skoa for this useful post:


Similar Threads

  1. Add new QML component when binding with C++
    By alenn.masic in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2013, 15:59
  2. qt binding?
    By giugio in forum Qt Programming
    Replies: 4
    Last Post: 15th November 2012, 14:33
  3. binding text in QMl to c++
    By rajeshk.ict in forum Newbie
    Replies: 1
    Last Post: 16th March 2012, 08:40
  4. QUdpSocket binding
    By db in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2008, 11:24
  5. binding
    By mickey in forum General Discussion
    Replies: 9
    Last Post: 26th September 2006, 21:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.