Results 1 to 11 of 11

Thread: Best way to pass data from backend c++

  1. #1
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Best way to pass data from backend c++

    Hi, I need to link the data from the classes connected to the backend, for example I have this class where it receives the data from the server through this method:

    Qt Code:
    1. void ClientServices::readyRead()
    2. {
    3. QDataStream in(tcpSocket);//QtcpSocket
    4.  
    5. int code = 0;
    6. double pCost = 0;
    7. double pPublic = 0;
    8.  
    9. in >> code >> pCost >> pPublic;
    10.  
    11. qDebug() << code << pCost << pPublic;
    12.  
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    the Backend is declare in this mode:

    qmlRegisterType<Backend>("io.qt.Backend", 1, 0, "Backend");

    can I do?

    I think to do with the Q_Property but but I wouldn't go for the backend anymore. In practice I have to fill in text boxes with variable :

    code >> pCost >> pPublic;

    see my schema:
    Property.jpg

  2. #2
    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: Best way to pass data from backend c++

    Since your other thread suggests that you will get multiple "rows" of these three values, the best way to present these rows of values to QML/QtQuick is a list model.

    The ClientService object needs to notify the model about new incoming data, maybe via signal.

    Easiest way is probably to do all the object creation and connecting on the C++ side and simply expose the fully working model to QML via QQmlContext::setContextProperty() on the engine's rootContext().

    Cheers,
    _

  3. #3
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Best way to pass data from backend c++

    I think I'll follow your path,when I have done I'll show you,tanks for help me

  4. #4
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Best way to pass data from backend c++

    there are almost, the signal emit sendCode (c_code); is data download, I will look in the qml when I call it "ReferenceError: c_code is not defined" error
    Cattura.jpg

    this is my code Qml:

    Qt Code:
    1. Backend {
    2. id: backend
    3.  
    4. onSendCode: {
    5. console.log( "This is code---> " + c_code);
    6. }
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default [Qml/c++] ReferenceError: code is not defined

    Good moorning it give me a error in the row 16: ReferenceError: code is not defined

    this is my Riparazioni.qml

    Qt Code:
    1. import QtQuick 2.10
    2. import QtQuick.Controls 2.3
    3. import io.qt.Backend 1.0
    4.  
    5. Item
    6. {
    7. id: element
    8.  
    9. Backend {
    10. id: backend
    11.  
    12. onSendCode: {
    13.  
    14. console.log( "This is code---> " + code); // errrrrrrrrrorrrrrrrr
    15. }
    16.  
    17. }
    18.  
    19.  
    20. Row {
    21. id: row1
    22. height: 50
    23. anchors.right: parent.right
    24. anchors.rightMargin: 5
    25. anchors.left: parent.left
    26. anchors.leftMargin: 5
    27. anchors.top: parent.top
    28. anchors.topMargin: 3
    29.  
    30. Rectangle {
    31. id: rectangle_Search
    32. height: 44
    33. color: "#0e99b2"
    34. anchors.top: parent.verticalCenter
    35. anchors.topMargin: -25
    36. anchors.left: parent.left
    37.  
    38. anchors.right: parent.right
    39.  
    40.  
    41. Text {
    42. id:lbl_SearchCode
    43. color: "#0a0808"
    44. text: qsTr("Codice")
    45. anchors.verticalCenter: parent.verticalCenter
    46. anchors.left: parent.left
    47. anchors.leftMargin: 4
    48. font.pixelSize: 12
    49. }
    50.  
    51. CustomTextEdit {
    52. id: c_Txt_codice
    53. y: 0
    54. width: 50
    55. height: 20
    56. anchors.verticalCenter: parent.verticalCenter
    57. anchors.bottom: parent
    58. anchors.bottomMargin: 0
    59. anchors.left: lbl_SearchCode.right
    60. anchors.leftMargin: 20
    61.  
    62. }
    63.  
    64. CustomButton {
    65. id: btnSearchKey
    66. x: 583
    67. y: 3
    68. text: "S"
    69. anchors.verticalCenter: parent.verticalCenter
    70. anchors.right: parent.right
    71. anchors.rightMargin: 5
    72. anchors.top: parent.verticalCenter
    73. anchors.topMargin: -22
    74. color: enabled ? this.down ? "#78C37F" : "#87DB8D" : "gray"
    75. onClicked: {
    76. // backend.connectClicked()
    77. backend.connectClicked()
    78. backend.sendClicked(c_Txt_codice.text)
    79. }
    80. }
    81.  
    82. }
    83. }
    84. Row {
    85. id: row2
    86. height: 200
    87. anchors.right: parent.right
    88. anchors.rightMargin: 3
    89. anchors.left: parent.left
    90. anchors.leftMargin: 3
    91. anchors.top: row1.bottom
    92. anchors.topMargin: 2
    93.  
    94. Rectangle{
    95. id: rectangle_Articolo
    96. color: "#0e99b2"
    97. anchors.fill: parent
    98.  
    99.  
    100.  
    101. Label{
    102. id: lbl_NBusta
    103. color: "#ebedef"
    104. text: "N°Busta"
    105. anchors.left: parent.left
    106. anchors.leftMargin: 5
    107. anchors.top: parent.top
    108. anchors.topMargin: 10
    109. }
    110.  
    111. CustomTextEdit {
    112. id: txtCodice
    113. width: 40
    114. height: 18
    115. anchors.top: parent.top
    116. anchors.topMargin: 10
    117. anchors.left: lbl_NBusta.right
    118. anchors.leftMargin: 10
    119. }
    120.  
    121. }
    122.  
    123. }
    124. }
    To copy to clipboard, switch view to plain text mode 
    this is my main.cpp
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    4.  
    5. QGuiApplication app(argc, argv);
    6.  
    7. //Pass my c++ to Qml
    8.  
    9. //qmlRegisterType<ClientServices>("io.qt.Clienteservices", 1, 0, "Clientservices");
    10.  
    11.  
    12. qmlRegisterType<Backend>("io.qt.Backend", 1, 0, "Backend");
    13. // Backend backend;
    14.  
    15. // QQmlContext *context = engine.rootContext();
    16. // context->setContextProperty("backend",&backend);
    17. QQmlApplicationEngine engine;
    18. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    19.  
    20.  
    21. if (engine.rootObjects().isEmpty())
    22. return -1;
    23.  
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    this is the Signal with send at Riparazioni.qml and I have do the debug it has data:

    Cattura.jpg
    Last edited by Nio74; 16th March 2019 at 09:22.

  6. #6
    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: Best way to pass data from backend c++

    How is the sendCode() parameter called in the signal's declaration?

    And: have you decided against using a model?

    Cheers,
    _

  7. #7
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Best way to pass data from backend c++

    in the backend constructor

    Qt Code:
    1. #include "backend.h"
    2.  
    3. Backend::Backend(QObject *parent) : QObject(parent)
    4. {
    5. client = new ClientServices("192.168.1.72",6547);
    6.  
    7. connect(client, &ClientServices::hasReadSome, this, &Backend::receivedSomething);
    8. connect(client, &ClientServices::statusChanged, this, &Backend::setStatus);
    9. // FIXME change this connection to the new syntax
    10. connect(client->tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
    11. this, SLOT(gotError(QAbstractSocket::SocketError)));
    12.  
    13. //connect data to source database
    14. connect(client, &ClientServices::sendRiparazione, this ,&Backend::recivedRiparazione);
    15.  
    16.  
    17. }
    18.  
    19.  
    20. bool Backend::getStatus()
    21. {
    22. return client->getStatus();
    23. }
    24.  
    25. void Backend::setStatus(bool newStatus)
    26. {
    27. //qDebug() << "new status is:" << newStatus;
    28. if (newStatus)
    29. { emit statusChanged("CONNECTED"); }
    30. else
    31. { emit statusChanged("DISCONNECTED"); }
    32. }
    33.  
    34. void Backend::recivedRiparazione(int code, double p_pCosto, double p_Pubblico)
    35. {
    36. emit sendCode(code);
    37. emit sendPCosto(p_pCosto);
    38. emit sendPPubblico(p_Pubblico);
    39.  
    40. }
    41.  
    42. void Backend::receivedSomething(QString msg)
    43. {
    44. emit someMessage(msg);
    45. }
    46.  
    47.  
    48.  
    49. void Backend::gotError(QAbstractSocket::SocketError err)
    50. {
    51. //qDebug() << "got error";
    52. QString strError = "unknown";
    53. switch (err)
    54. {
    55. case 0:
    56. strError = "Connection was refused";
    57. break;
    58. case 1:
    59. strError = "Remote host closed the connection";
    60. break;
    61. case 2:
    62. strError = "Host address was not found";
    63. break;
    64. case 5:
    65. strError = "Connection timed out";
    66. break;
    67. default:
    68. strError = "Unknown error";
    69. }
    70.  
    71. emit someError(strError);
    72. }
    73.  
    74. void Backend::connectClicked()
    75. {
    76. client->connect2host();
    77. }
    78.  
    79. void Backend::sendClicked(QString msg)
    80. {
    81. QByteArray arrBlock;
    82. QDataStream out(&arrBlock, QIODevice::WriteOnly);
    83. //out.setVersion(QDataStream::Qt_5_10);
    84. out << quint16(0) << msg;
    85.  
    86. out.device()->seek(0);
    87. out << quint16(arrBlock.size() - sizeof(quint16));
    88.  
    89. client->tcpSocket->write(arrBlock);
    90. }
    91.  
    92. void Backend::disconnectClicked()
    93. {
    94. client->closeConnection();
    95. }
    To copy to clipboard, switch view to plain text mode 

    this is clientService.cpp:
    Qt Code:
    1. #include "clientservices.h"
    2. #include <QBuffer>
    3.  
    4.  
    5. ClientServices::ClientServices(const QString hostAddress, int portNumber) : QObject (),m_nNextBlockSize(0)
    6. {
    7. status = false;
    8. tcpSocket = new QTcpSocket();
    9.  
    10. host = hostAddress;
    11. port = portNumber;
    12.  
    13. timeoutTimer = new QTimer();
    14. timeoutTimer->setSingleShot(true);
    15. connect(timeoutTimer, &QTimer::timeout, this, &ClientServices::connectionTimeout);
    16.  
    17. connect(tcpSocket, &QTcpSocket::disconnected, this, &ClientServices::closeConnection);
    18.  
    19. }
    20.  
    21. bool ClientServices::getStatus(){return status;
    22. }
    23.  
    24. void ClientServices::closeConnection()
    25. {
    26. timeoutTimer->stop();
    27.  
    28. //qDebug() << tcpSocket->state();
    29. disconnect(tcpSocket, &QTcpSocket::connected, 0, 0);
    30. disconnect(tcpSocket, &QTcpSocket::readyRead, 0, 0);
    31.  
    32. bool shouldEmit = false;
    33. switch (tcpSocket->state())
    34. {
    35. case 0:
    36. tcpSocket->disconnectFromHost();
    37. shouldEmit = true;
    38. break;
    39. case 2:
    40. tcpSocket->abort();
    41. shouldEmit = true;
    42. break;
    43. default:
    44. tcpSocket->abort();
    45. }
    46.  
    47. if (shouldEmit)
    48. {
    49. status = false;
    50. emit statusChanged(status);
    51. }
    52.  
    53. }
    54.  
    55. void ClientServices::connect2host()
    56. {
    57.  
    58. timeoutTimer->start(3000);
    59.  
    60. tcpSocket->connectToHost(host, port);
    61. connect(tcpSocket, &QTcpSocket::connected, this, &ClientServices::connected);
    62. connect(tcpSocket, &QTcpSocket::readyRead, this, &ClientServices::readyRead);
    63. }
    64.  
    65. void ClientServices::readyRead()
    66. {
    67. QDataStream in(tcpSocket);//QtcpSocket
    68.  
    69. in >> code >> pCost >> pPublic;
    70.  
    71. qDebug() << code << pCost << pPublic;
    72.  
    73. emit sendRiparazione(code,pCost,pPublic);
    74.  
    75. }
    76.  
    77. void ClientServices::connected()
    78. {
    79. status = true;
    80. emit statusChanged(status);
    81.  
    82. }
    83.  
    84. void ClientServices::connectionTimeout()
    85. {
    86. //qDebug() << tcpSocket->state();
    87. if(tcpSocket->state() == QAbstractSocket::ConnectingState)
    88. {
    89. tcpSocket->abort();
    90. emit tcpSocket->error(QAbstractSocket::SocketTimeoutError);
    91. }
    92. }
    To copy to clipboard, switch view to plain text mode 

    my Idea Controller Backend.cpp Model clientservice.cpp(and Other) View clsses Qml
    Last edited by Nio74; 16th March 2019 at 11:00.

  8. #8
    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: Best way to pass data from backend c++

    Interesting but neither of the two code sections contains the declaration of the sendCode() signal.

    The argument name in that declaration is the name available on the QML side.
    It is irrelevant how the variable is called to you pass at emit time.

    Cheers,
    _

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

    Nio74 (16th March 2019)

  10. #9
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Best way to pass data from backend c++

    Backend.h

    Qt Code:
    1. #ifndef BACKEND_H
    2. #define BACKEND_H
    3.  
    4. #include <QObject>
    5. #include "clientservices.h"
    6.  
    7. class Backend : public QObject
    8. {
    9. Q_OBJECT
    10. Q_PROPERTY(bool currentStatus READ getStatus NOTIFY statusChanged)
    11. public:
    12. explicit Backend(QObject *parent = nullptr);
    13. bool getStatus();
    14.  
    15. signals:
    16. void statusChanged(QString newStatus);
    17. void someError(QString err);
    18. void someMessage(QString msg);
    19. void sendCode(int c_Code);
    20. void sendPCosto(double p_pCosto);
    21. void sendPPubblico(double p_pPubblico);
    22.  
    23. public slots:
    24.  
    25. void setStatus(bool newStatus);
    26.  
    27. void recivedRiparazione(int code,double p_pCosto,double p_Pubblico);
    28. void receivedSomething(QString msg);
    29. void gotError(QAbstractSocket::SocketError err);
    30. void sendClicked(QString msg);
    31. void connectClicked();
    32. void disconnectClicked();
    33. private:
    34. ClientServices *client;
    35. int codiceBusta;
    36. double prezzoCosto,prezzoPubblico;
    37.  
    38. };
    39.  
    40. #endif // BACKEND_H
    To copy to clipboard, switch view to plain text mode 


    Added after 19 minutes:


    you say i should declare the variable code? I'm following this wiki
    Last edited by Nio74; 16th March 2019 at 11:22.

  11. #10
    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: Best way to pass data from backend c++

    Quote Originally Posted by Nio74 View Post
    Qt Code:
    1. void sendCode(int c_Code);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. onSendCode: console.log("code=" + c_Code);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

    Nio74 (16th March 2019)

  13. #11
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Best way to pass data from backend c++

    what a fool I am now works thanks

Similar Threads

  1. pass data to SLOT
    By Surfman19 in forum Qt Programming
    Replies: 7
    Last Post: 9th July 2015, 13:28
  2. Replies: 8
    Last Post: 14th June 2013, 11:16
  3. Change phonon backend to vlc backend
    By stereoMatching in forum Newbie
    Replies: 1
    Last Post: 8th September 2012, 06:10
  4. Best way to pass data between Windows and Linux
    By deepakn in forum General Programming
    Replies: 1
    Last Post: 28th December 2011, 13:40
  5. Replies: 5
    Last Post: 1st March 2010, 16:55

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.