跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  2. Qt Development
  3. QML and Qt Quick
  4. First attempt to use Qt networking on QML apps
Forum Updated to NodeBB v4.3 + New Features

First attempt to use Qt networking on QML apps

已排程 已置頂 已鎖定 已移動 Unsolved QML and Qt Quick
28 貼文 4 Posters 2.9k 瀏覽 1 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • GrecKoG 離線
    GrecKoG 離線
    GrecKo
    Qt Champions 2018
    寫於 最後由 GrecKo 編輯
    #12

    The connection: connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection); is defined in the serve's constructor.

    but

    I removed the call [to initServer] from the constructor (in C++) but still nothing different in result

    You are connecting to a null tcpServer, so it can't work.

    Q 1 條回覆 最後回覆
    0
    • GrecKoG GrecKo

      The connection: connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection); is defined in the serve's constructor.

      but

      I removed the call [to initServer] from the constructor (in C++) but still nothing different in result

      You are connecting to a null tcpServer, so it can't work.

      Q 離線
      Q 離線
      qcoderpro
      寫於 最後由 qcoderpro 編輯
      #13

      @GrecKo
      This is server's qml file:

      ColumnLayout {
              anchors.fill: parent
      
              Label {
                  text: myObj.initServer()
              }
      
              Label {
                  id: msgLabel
                  text: myObj.getMessage()
              }
          }
      
          ServerClass{
              id: myObj
          }
      

      If I comment out the label's text text: myObj.initServer(), nothing will be shown on its UI. But by uncommenting it, the IP address and port are shown on the UI. then I manually write them on the client's UI and click on Send address there.

      qeweqwe.PNG

      It's the only time the initServer is called and I'm using its output on the client's text fields, so why a null tcpServer, please?
      The send button calls the sendAddress slot:

      void Client::sendAddress(QString ip, QString port)
      {
          tcpSocket->abort();
          tcpSocket->connectToHost(ip, port.toInt());
      
          connect(tcpSocket, &QAbstractSocket::connected, []() {
              qDebug() << "Connected to the host";
          });
      }
      

      Here, too, qDebug in the lambda prints the message. So there's been a proper connection seemingly, otherwise I'm missing something that makes the projects not work properly! :|

      1 條回覆 最後回覆
      0
      • GrecKoG 離線
        GrecKoG 離線
        GrecKo
        Qt Champions 2018
        寫於 最後由 編輯
        #14

        As I said, you are not connecting to your QTcpServer newConnection signal.
        The connection you are trying to do in the constructor is too soon because you don't have any QTcpServer yet.

        Q 1 條回覆 最後回覆
        1
        • GrecKoG GrecKo

          As I said, you are not connecting to your QTcpServer newConnection signal.
          The connection you are trying to do in the constructor is too soon because you don't have any QTcpServer yet.

          Q 離線
          Q 離線
          qcoderpro
          寫於 最後由 編輯
          #15

          @GrecKo

          As I said, you are not connecting to your QTcpServer newConnection signal.

          Yes, that's right and I know it but don't know how to solve it! :|

          The connection you are trying to do in the constructor is too soon because you don't have any QTcpServer yet.

          Yea, that makes sense. So I cut that connection there and pasted it in the initServer(). Still theonNewConnectionis not called! :|

          jsulmJ 1 條回覆 最後回覆
          0
          • Q 離線
            Q 離線
            qcoderpro
            寫於 最後由 編輯
            #16

            No further help! :(

            1 條回覆 最後回覆
            0
            • Q qcoderpro

              @GrecKo

              As I said, you are not connecting to your QTcpServer newConnection signal.

              Yes, that's right and I know it but don't know how to solve it! :|

              The connection you are trying to do in the constructor is too soon because you don't have any QTcpServer yet.

              Yea, that makes sense. So I cut that connection there and pasted it in the initServer(). Still theonNewConnectionis not called! :|

              jsulmJ 線上
              jsulmJ 線上
              jsulm
              Lifetime Qt Champion
              寫於 最後由 編輯
              #17

              @qcoderpro said in First attempt to use Qt networking on QML apps:

              So I cut that connection there and pasted it in the initServer(). Still theonNewConnectionis not called!

              Please show your current code

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              Q 1 條回覆 最後回覆
              0
              • jsulmJ jsulm

                @qcoderpro said in First attempt to use Qt networking on QML apps:

                So I cut that connection there and pasted it in the initServer(). Still theonNewConnectionis not called!

                Please show your current code

                Q 離線
                Q 離線
                qcoderpro
                寫於 最後由 編輯
                #18

                @jsulm

                This is code for server.cpp:

                #include "server.h"
                #include <QtNetwork>
                #include <QtCore>
                
                Server::Server(QObject *parent) : QObject{parent}
                    , tcpServer(new QTcpServer(this)) { }
                
                QString Server::initServer() {
                    // Called from the front-end
                
                    tcpServer = new QTcpServer(this);
                    if(!tcpServer->listen())
                        return "Server Unable to start the server: " +
                                tcpServer->errorString();
                
                    QString ipAddress;
                    QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
                
                    // use the first non-local IPv4 address
                    for(int i=0; i<ipAddressesList.size(); ++i)
                        if(ipAddressesList.at(i) != QHostAddress::LocalHost &&
                                ipAddressesList.at(i).toIPv4Address()) {
                            ipAddress = ipAddressesList.at(i).toString();
                            break;
                        }
                
                    // if we did not find one, use IPv4 localhost
                    if(ipAddress.isEmpty())
                        ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
                
                    return " The server is running on\n\n IP: " +
                            ipAddress + "\n port: " + QString::number(tcpServer->serverPort())
                            + "\n\n Run the Client example now.";
                    connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection);
                }
                
                void Server::onNewConnection()
                {
                    qDebug() << "OnNewConnection was called!\n";
                
                    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
                    connect(clientConnection, &QAbstractSocket::disconnected,
                            clientConnection, &QObject::deleteLater);
                    in.setDevice(clientConnection);
                    in.setVersion(QDataStream::Qt_4_0);
                    connect(clientConnection, &QAbstractSocket::readyRead, this, &Server::setMessage);
                }
                
                void Server::setMessage()
                {
                    in.startTransaction();
                
                    QString msg;
                    in >> msg;
                
                    if (!in.commitTransaction())
                        message = "commitTransaction error" ;
                    else
                        message = msg;
                }
                
                QString Server::getMessage() const
                {
                    return message;
                }
                

                Totally there're 6 files as follows:
                server.h, server.cpp, server qml file, client.h, client.cpp and client qml file

                If you like I can pack them all in a zip file and send it to a global resource for freely downloading and consideration.

                jsulmJ 1 條回覆 最後回覆
                0
                • Q qcoderpro

                  @jsulm

                  This is code for server.cpp:

                  #include "server.h"
                  #include <QtNetwork>
                  #include <QtCore>
                  
                  Server::Server(QObject *parent) : QObject{parent}
                      , tcpServer(new QTcpServer(this)) { }
                  
                  QString Server::initServer() {
                      // Called from the front-end
                  
                      tcpServer = new QTcpServer(this);
                      if(!tcpServer->listen())
                          return "Server Unable to start the server: " +
                                  tcpServer->errorString();
                  
                      QString ipAddress;
                      QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
                  
                      // use the first non-local IPv4 address
                      for(int i=0; i<ipAddressesList.size(); ++i)
                          if(ipAddressesList.at(i) != QHostAddress::LocalHost &&
                                  ipAddressesList.at(i).toIPv4Address()) {
                              ipAddress = ipAddressesList.at(i).toString();
                              break;
                          }
                  
                      // if we did not find one, use IPv4 localhost
                      if(ipAddress.isEmpty())
                          ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
                  
                      return " The server is running on\n\n IP: " +
                              ipAddress + "\n port: " + QString::number(tcpServer->serverPort())
                              + "\n\n Run the Client example now.";
                      connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection);
                  }
                  
                  void Server::onNewConnection()
                  {
                      qDebug() << "OnNewConnection was called!\n";
                  
                      QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
                      connect(clientConnection, &QAbstractSocket::disconnected,
                              clientConnection, &QObject::deleteLater);
                      in.setDevice(clientConnection);
                      in.setVersion(QDataStream::Qt_4_0);
                      connect(clientConnection, &QAbstractSocket::readyRead, this, &Server::setMessage);
                  }
                  
                  void Server::setMessage()
                  {
                      in.startTransaction();
                  
                      QString msg;
                      in >> msg;
                  
                      if (!in.commitTransaction())
                          message = "commitTransaction error" ;
                      else
                          message = msg;
                  }
                  
                  QString Server::getMessage() const
                  {
                      return message;
                  }
                  

                  Totally there're 6 files as follows:
                  server.h, server.cpp, server qml file, client.h, client.cpp and client qml file

                  If you like I can pack them all in a zip file and send it to a global resource for freely downloading and consideration.

                  jsulmJ 線上
                  jsulmJ 線上
                  jsulm
                  Lifetime Qt Champion
                  寫於 最後由 編輯
                  #19

                  @qcoderpro said in First attempt to use Qt networking on QML apps:

                  ipAddress = ipAddressesList.at(i).toString();

                  What is ipAddress used for? You are not using it when you call listen().

                  But the actuall issue is that you have a return just before connect(), so connect() is never called...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Q 1 條回覆 最後回覆
                  2
                  • jsulmJ jsulm

                    @qcoderpro said in First attempt to use Qt networking on QML apps:

                    ipAddress = ipAddressesList.at(i).toString();

                    What is ipAddress used for? You are not using it when you call listen().

                    But the actuall issue is that you have a return just before connect(), so connect() is never called...

                    Q 離線
                    Q 離線
                    qcoderpro
                    寫於 最後由 編輯
                    #20

                    @jsulm

                    What is ipAddress used for? You are not using it when you call listen().

                    ipAddress is used to store the list of local/non-local IP addresses. But I didn't understand the part "You are not using it when you call listen()".

                    But the actuall issue is that you have a return just before connect(), so connect() is never called..

                    Right. I moved the connect() to the part right after the listen(). Now the onNewConnection() slot is successfully called and it prints the message "OnNewConnection was called" in Application Output window as expected.

                    Now there's still a problem remaining!
                    This is client.cpp:

                    #include "client.h"
                    #include <QtNetwork>
                    
                    Client::Client(QObject *parent)
                        : QObject{parent}
                        , tcpSocket(new QTcpSocket(this))
                    {
                        out.setDevice(tcpSocket);
                        out.setVersion(QDataStream::Qt_4_0);
                    }
                    
                    void Client::sendAddress(QString ip, QString port)
                    {
                        tcpSocket->abort();
                        tcpSocket->connectToHost(ip, port.toInt());
                    
                        connect(tcpSocket, &QAbstractSocket::connected, []() {
                            qDebug() << "Connected to the host";
                        });
                    }
                    
                    void Client::sendMessage(const QString& message)
                    //called from front-end
                    {
                        QByteArray block;
                        QDataStream out(&block, QIODevice::WriteOnly);
                        out.setVersion(QDataStream::Qt_5_10);
                        out << message;
                        tcpSocket->write(block);
                    }
                    

                    The sendAddress slot prints the message of the lambda in output ("Connected to the host") so by that I assume the connection between the client and server the way above is properly established. The remaining problem is that the sendMessage slot still doesn't send its message (its string parameter) to the sever! :(

                    jsulmJ 1 條回覆 最後回覆
                    0
                    • Q qcoderpro

                      @jsulm

                      What is ipAddress used for? You are not using it when you call listen().

                      ipAddress is used to store the list of local/non-local IP addresses. But I didn't understand the part "You are not using it when you call listen()".

                      But the actuall issue is that you have a return just before connect(), so connect() is never called..

                      Right. I moved the connect() to the part right after the listen(). Now the onNewConnection() slot is successfully called and it prints the message "OnNewConnection was called" in Application Output window as expected.

                      Now there's still a problem remaining!
                      This is client.cpp:

                      #include "client.h"
                      #include <QtNetwork>
                      
                      Client::Client(QObject *parent)
                          : QObject{parent}
                          , tcpSocket(new QTcpSocket(this))
                      {
                          out.setDevice(tcpSocket);
                          out.setVersion(QDataStream::Qt_4_0);
                      }
                      
                      void Client::sendAddress(QString ip, QString port)
                      {
                          tcpSocket->abort();
                          tcpSocket->connectToHost(ip, port.toInt());
                      
                          connect(tcpSocket, &QAbstractSocket::connected, []() {
                              qDebug() << "Connected to the host";
                          });
                      }
                      
                      void Client::sendMessage(const QString& message)
                      //called from front-end
                      {
                          QByteArray block;
                          QDataStream out(&block, QIODevice::WriteOnly);
                          out.setVersion(QDataStream::Qt_5_10);
                          out << message;
                          tcpSocket->write(block);
                      }
                      

                      The sendAddress slot prints the message of the lambda in output ("Connected to the host") so by that I assume the connection between the client and server the way above is properly established. The remaining problem is that the sendMessage slot still doesn't send its message (its string parameter) to the sever! :(

                      jsulmJ 線上
                      jsulmJ 線上
                      jsulm
                      Lifetime Qt Champion
                      寫於 最後由 編輯
                      #21

                      @qcoderpro said in First attempt to use Qt networking on QML apps:

                      But I didn't understand the part "You are not using it when you call listen()".

                      You know that you can pass an IP to listen? See https://doc.qt.io/qt-5/qtcpserver.html#listen

                      Where and when do you call sendMessage ?
                      You also should add code for error handling: https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred
                      https://doc.qt.io/qt-5/qiodevice.html#errorString

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      Q 1 條回覆 最後回覆
                      0
                      • jsulmJ jsulm

                        @qcoderpro said in First attempt to use Qt networking on QML apps:

                        But I didn't understand the part "You are not using it when you call listen()".

                        You know that you can pass an IP to listen? See https://doc.qt.io/qt-5/qtcpserver.html#listen

                        Where and when do you call sendMessage ?
                        You also should add code for error handling: https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred
                        https://doc.qt.io/qt-5/qiodevice.html#errorString

                        Q 離線
                        Q 離線
                        qcoderpro
                        寫於 最後由 qcoderpro 編輯
                        #22

                        @jsulm

                        You know that you can pass an IP to listen? See https://doc.qt.io/qt-5/qtcpserver.html#listen

                        No, I didn't know that and if I use listen() that way I need to someway convert the ipAddress which a QString to a QHostAddress. But now the connection is properly established and the connect(...) calls the slot onNewConnection.

                        Where and when do you call sendMessage ?

                        In the front-end:

                         ColumnLayout {
                                anchors.fill: parent
                        
                                TextField {
                                    id: ipAddrs
                                }
                                TextField {
                                        id: portNum
                                    }
                                Button {
                                    text: "Send Address"
                                    onClicked: myObj.sendAddress(ipAddrs.text.toString(), portNum.text.toString())
                                }
                        
                                RowLayout {
                                    Layout.alignment: Qt.AlignBottom
                        
                                    TextField {
                                        id: txtField
                                        Layout.fillWidth: true
                                    }
                                    Button {
                                        text: qsTr("Send")
                                        onClicked: myObj.sendMessage(txtField.text)
                                    }
                                }
                            }
                        
                            MyClass {
                                id: myObj
                            }
                        

                        wqerwr.PNG

                        You also should add code for error handling: https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred
                        https://doc.qt.io/qt-5/qiodevice.html#errorString

                        For that, I added this connect() after the lambda in client.cpp:
                        connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &Client::displayError); and this slot there:

                        void Client::displayError(QAbstractSocket::SocketError socketError) {
                           // for this stage, printing the error messages using qDenug will be fine
                        
                            switch (socketError) {
                            case QAbstractSocket::RemoteHostClosedError:
                                break;
                            case QAbstractSocket::HostNotFoundError:
                                qDebug() <<"The host was not found. Please check the "
                                           "host name and port settings.";
                                break;
                            case QAbstractSocket::ConnectionRefusedError:
                                qDebug() << "The connection was refused by the peer. "
                                            "Make sure the server is running, "
                                            "and check that the host name and port "
                                            "settings are correct.";
                                break;
                            default:
                                qDebug() << "The following error occurred: " + tcpSocket->errorString();
                            }
                        }
                        

                        Still no message is sent nor there's any error.

                        jsulmJ 1 條回覆 最後回覆
                        0
                        • Q qcoderpro

                          @jsulm

                          You know that you can pass an IP to listen? See https://doc.qt.io/qt-5/qtcpserver.html#listen

                          No, I didn't know that and if I use listen() that way I need to someway convert the ipAddress which a QString to a QHostAddress. But now the connection is properly established and the connect(...) calls the slot onNewConnection.

                          Where and when do you call sendMessage ?

                          In the front-end:

                           ColumnLayout {
                                  anchors.fill: parent
                          
                                  TextField {
                                      id: ipAddrs
                                  }
                                  TextField {
                                          id: portNum
                                      }
                                  Button {
                                      text: "Send Address"
                                      onClicked: myObj.sendAddress(ipAddrs.text.toString(), portNum.text.toString())
                                  }
                          
                                  RowLayout {
                                      Layout.alignment: Qt.AlignBottom
                          
                                      TextField {
                                          id: txtField
                                          Layout.fillWidth: true
                                      }
                                      Button {
                                          text: qsTr("Send")
                                          onClicked: myObj.sendMessage(txtField.text)
                                      }
                                  }
                              }
                          
                              MyClass {
                                  id: myObj
                              }
                          

                          wqerwr.PNG

                          You also should add code for error handling: https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred
                          https://doc.qt.io/qt-5/qiodevice.html#errorString

                          For that, I added this connect() after the lambda in client.cpp:
                          connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &Client::displayError); and this slot there:

                          void Client::displayError(QAbstractSocket::SocketError socketError) {
                             // for this stage, printing the error messages using qDenug will be fine
                          
                              switch (socketError) {
                              case QAbstractSocket::RemoteHostClosedError:
                                  break;
                              case QAbstractSocket::HostNotFoundError:
                                  qDebug() <<"The host was not found. Please check the "
                                             "host name and port settings.";
                                  break;
                              case QAbstractSocket::ConnectionRefusedError:
                                  qDebug() << "The connection was refused by the peer. "
                                              "Make sure the server is running, "
                                              "and check that the host name and port "
                                              "settings are correct.";
                                  break;
                              default:
                                  qDebug() << "The following error occurred: " + tcpSocket->errorString();
                              }
                          }
                          

                          Still no message is sent nor there's any error.

                          jsulmJ 線上
                          jsulmJ 線上
                          jsulm
                          Lifetime Qt Champion
                          寫於 最後由 編輯
                          #23

                          @qcoderpro said in First attempt to use Qt networking on QML apps:

                          For that, I added this connect() after the lambda in client.cpp:

                          The idea was actually to check the errors on server side

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          Q 1 條回覆 最後回覆
                          1
                          • jsulmJ jsulm

                            @qcoderpro said in First attempt to use Qt networking on QML apps:

                            For that, I added this connect() after the lambda in client.cpp:

                            The idea was actually to check the errors on server side

                            Q 離線
                            Q 離線
                            qcoderpro
                            寫於 最後由 編輯
                            #24

                            @jsulm
                            Yeah, right.
                            I declared a QTcpSocket object in the server app and added the connect() and displayError slot as mentioned above in it. As before, the connection is established but no message is sent nor is an error displayed!
                            I shared both projects in a zip file in link below. If you have time, take a look at them please. I'm sure the remaining problem will be clear to you. I tried to keep the projects as simple as possible just to do the job: https://www.4shared.com/s/f_Gct6Kjwiq

                            1 條回覆 最後回覆
                            0
                            • GrecKoG 離線
                              GrecKoG 離線
                              GrecKo
                              Qt Champions 2018
                              寫於 最後由 編輯
                              #25

                              This file sharing is unusable without account it seems.

                              If you want to share multiple files, using https://gist.github.com would be better for the people that want to help you.

                              Q 1 條回覆 最後回覆
                              1
                              • GrecKoG GrecKo

                                This file sharing is unusable without account it seems.

                                If you want to share multiple files, using https://gist.github.com would be better for the people that want to help you.

                                Q 離線
                                Q 離線
                                qcoderpro
                                寫於 最後由 編輯
                                #26

                                @GrecKo
                                That's a code sharing platform (instead of files) but if code is meant to be easier than the projects both in a zip file then as usual it's feasible to share them here.
                                server.h:

                                #include <QObject>
                                #include <QAbstractSocket>
                                
                                class QTcpServer;
                                class QTcpSocket;
                                
                                class Server : public QObject
                                {
                                    Q_OBJECT
                                public:
                                    explicit Server(QObject *parent = nullptr);
                                
                                public slots:
                                    QString initServer();
                                    void setMessage();
                                    QString getMessage() const;
                                    void onNewConnection();
                                    void displayError(QAbstractSocket::SocketError);
                                
                                private:
                                    QTcpServer* tcpServer { nullptr };
                                    QTcpSocket* tcpSocket { nullptr };
                                    QDataStream in;
                                    QString message;
                                };
                                

                                server.cpp:

                                #include "server.h"
                                #include <QtNetwork>
                                #include <QtCore>
                                
                                Server::Server(QObject *parent) : QObject{parent}
                                    , tcpServer(new QTcpServer(this))
                                    , tcpSocket(new QTcpSocket(this)) { }
                                
                                QString Server::initServer() {
                                    // Called from the front-end
                                
                                    tcpServer = new QTcpServer(this);
                                    if(!tcpServer->listen())
                                        return "Server Unable to start the server: " +
                                                tcpServer->errorString();
                                
                                    connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection);
                                    connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &Server::displayError);
                                
                                    QString ipAddress;
                                    QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
                                
                                    // use the first non-local IPv4 address
                                    for(int i=0; i<ipAddressesList.size(); ++i)
                                        if(ipAddressesList.at(i) != QHostAddress::LocalHost &&
                                                ipAddressesList.at(i).toIPv4Address()) {
                                            ipAddress = ipAddressesList.at(i).toString();
                                            break;
                                        }
                                
                                    // if we did not find one, use IPv4 localhost
                                    if(ipAddress.isEmpty())
                                        ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
                                
                                    return " The server is running on\n\n IP: " +
                                            ipAddress + "\n port: " + QString::number(tcpServer->serverPort())
                                            + "\n\n Run the Client example now.";
                                }
                                
                                void Server::onNewConnection()
                                {
                                    qDebug() << "OnNewConnection was called!\n";
                                
                                    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
                                    connect(clientConnection, &QAbstractSocket::disconnected,
                                            clientConnection, &QObject::deleteLater);
                                    in.setDevice(clientConnection);
                                    in.setVersion(QDataStream::Qt_4_0);
                                    connect(clientConnection, &QAbstractSocket::readyRead, this, &Server::setMessage);
                                }
                                
                                void Server::displayError(QAbstractSocket::SocketError socketError)
                                {
                                    // for this stage, printing the error messages using qDenug will be fine
                                
                                     switch (socketError) {
                                     case QAbstractSocket::RemoteHostClosedError:
                                         break;
                                     case QAbstractSocket::HostNotFoundError:
                                         qDebug() <<"The host was not found. Please check the "
                                                    "host name and port settings.";
                                         break;
                                     case QAbstractSocket::ConnectionRefusedError:
                                         qDebug() << "The connection was refused by the peer. "
                                                     "Make sure the server is running, "
                                                     "and check that the host name and port "
                                                     "settings are correct.";
                                         break;
                                     default:
                                         qDebug() << "The following error occurred: " + tcpSocket->errorString();
                                     }
                                }
                                
                                void Server::setMessage()
                                {
                                    in.startTransaction();
                                
                                    QString msg;
                                    in >> msg;
                                
                                    if (!in.commitTransaction())
                                        message = "commitTransaction error" ;
                                    else
                                        message = msg;
                                }
                                
                                QString Server::getMessage() const
                                {
                                    return message;
                                }
                                

                                server's project qml file:

                                import QtQuick
                                import QtQuick.Controls
                                import QtQuick.Layouts
                                import SomeClass 1.0
                                
                                Window {
                                    width: 300
                                    height: 200
                                    visible: true
                                    title: qsTr("Server")
                                    color: "lightblue"
                                
                                    ColumnLayout {
                                        anchors.fill: parent
                                
                                        Label {
                                            text: myObj.initServer()
                                        }
                                
                                        Label {
                                            id: msgLabel
                                            text: myObj.getMessage()
                                        }
                                    }
                                
                                    MyClass {
                                        id: myObj
                                    }
                                }
                                

                                client.h:

                                #include <QDataStream>
                                #include <QObject>
                                
                                class QTcpSocket;
                                
                                class Client : public QObject
                                {
                                    Q_OBJECT
                                
                                public:
                                    explicit Client(QObject *parent = nullptr);
                                
                                public slots:
                                    void sendAddress(QString, QString);
                                    void sendMessage(const QString&);
                                
                                private:
                                    QTcpSocket* tcpSocket { nullptr };
                                    QDataStream out;
                                };
                                

                                client.cpp:

                                #include "client.h"
                                #include <QtNetwork>
                                
                                Client::Client(QObject *parent)
                                    : QObject{parent}
                                    , tcpSocket(new QTcpSocket(this))
                                {
                                    out.setDevice(tcpSocket);
                                    out.setVersion(QDataStream::Qt_4_0);
                                }
                                
                                void Client::sendAddress(QString ip, QString port)
                                {
                                    tcpSocket->abort();
                                    tcpSocket->connectToHost(ip, port.toInt());
                                
                                    connect(tcpSocket, &QAbstractSocket::connected, []() {
                                        qDebug() << "Connected to the host";
                                    });
                                }
                                
                                void Client::sendMessage(const QString& message) {
                                      //called from front-end
                                    QByteArray block;
                                    QDataStream out(&block, QIODevice::WriteOnly);
                                    out.setVersion(QDataStream::Qt_5_10);
                                    out << message;
                                    tcpSocket->write(block);
                                }
                                

                                client's project qml file:

                                import QtQuick
                                import QtQuick.Controls
                                import QtQuick.Layouts
                                import SomeClass 1.0
                                
                                Window {
                                    width: 300
                                    height: 200
                                    visible: true
                                    title: qsTr("Client")
                                    color: "lightblue"
                                
                                    ColumnLayout {
                                        anchors.fill: parent
                                
                                        TextField {
                                            id: ipAddrs
                                        }
                                        TextField {
                                                id: portNum
                                            }
                                        Button {
                                            text: "Send Address"
                                            onClicked: myObj.sendAddress(ipAddrs.text.toString(), portNum.text.toString())
                                        }
                                
                                        RowLayout {
                                            Layout.alignment: Qt.AlignBottom
                                
                                            TextField {
                                                id: txtField
                                                Layout.fillWidth: true
                                            }
                                            Button {
                                                text: qsTr("Send")
                                                onClicked: myObj.sendMessage(txtField.text)
                                            }
                                        }
                                    }
                                
                                    MyClass {
                                        id: myObj
                                    }
                                }
                                

                                Hopefully we can now find the issue. :(

                                1 條回覆 最後回覆
                                0
                                • Q 離線
                                  Q 離線
                                  qcoderpro
                                  寫於 最後由 編輯
                                  #27

                                  Here is also the link for that platform embracing the files code: https://gist.github.com/Tomtesty/eff99a9721afa7a44558fe097e65b079

                                  1 條回覆 最後回覆
                                  0
                                  • Q 離線
                                    Q 離線
                                    qcoderpro
                                    寫於 最後由 編輯
                                    #28

                                    Is the problem still ambiguous, please? I tried hard but can't find the issue. :(

                                    1 條回覆 最後回覆
                                    0

                                    • 登入

                                    • Login or register to search.
                                    • 第一個貼文
                                      最後的貼文
                                    0
                                    • 版面
                                    • 最新
                                    • 標籤
                                    • 熱門
                                    • 使用者
                                    • 群組
                                    • 搜尋
                                    • Get Qt Extensions
                                    • Unsolved