Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
28 Posts 4 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on last edited by
    #2

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

    void Server::onNewConnection()

    Just have some qDebug in this slot & see if connection request has arrived. This should help if the connection is established or not.

    Dheerendra
    @Community Service
    Certified Qt Specialist
    http://www.pthinks.com

    Q 1 Reply Last reply
    0
    • dheerendraD dheerendra

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

      void Server::onNewConnection()

      Just have some qDebug in this slot & see if connection request has arrived. This should help if the connection is established or not.

      Q Offline
      Q Offline
      qcoderpro
      wrote on last edited by
      #3

      @dheerendra

      I already tested it, and no, not connection arrives! I don't know where the problem is! :(

      1 Reply Last reply
      0
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #4

        Maybe you are using the wrong ip and/or port

        Q 1 Reply Last reply
        0
        • GrecKoG GrecKo

          Maybe you are using the wrong ip and/or port

          Q Offline
          Q Offline
          qcoderpro
          wrote on last edited by
          #5

          @GrecKo
          The client's sendAddress slot sends the IP and port number as a string and int respectively, based on this version.

          1 Reply Last reply
          0
          • GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by
            #6

            What I am saying is that the server can be providing an unreachable IP and port.

            QString Server::initServer() {
            //.. Provide the IP address and port number for the client
            }
            

            Doesn't tell us much.

            Q 1 Reply Last reply
            0
            • GrecKoG GrecKo

              What I am saying is that the server can be providing an unreachable IP and port.

              QString Server::initServer() {
              //.. Provide the IP address and port number for the client
              }
              

              Doesn't tell us much.

              Q Offline
              Q Offline
              qcoderpro
              wrote on last edited by
              #7

              @GrecKo

              The code for that is:

              QString Server::initServer() {
              
                  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.";
              }
              
              1 Reply Last reply
              0
              • GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #8

                You are calling initServer() twice, once in c++ and once in QML, thus creating 2 QTcpServer. You are connecting yout onNewConnection only to the first one but displaying the ip and port of the second one in your UI.

                Your initServer function does too much stuff. It shouldn't return the text like you are doing. Expose the ip and port of your server as properties and display them properly in your QML. Don't call initServer from QML.

                Q 1 Reply Last reply
                1
                • GrecKoG GrecKo

                  You are calling initServer() twice, once in c++ and once in QML, thus creating 2 QTcpServer. You are connecting yout onNewConnection only to the first one but displaying the ip and port of the second one in your UI.

                  Your initServer function does too much stuff. It shouldn't return the text like you are doing. Expose the ip and port of your server as properties and display them properly in your QML. Don't call initServer from QML.

                  Q Offline
                  Q Offline
                  qcoderpro
                  wrote on last edited by
                  #9

                  @GrecKo
                  Yes, you're right. I removed the call from the constructor (in C++) but still nothing different in result. As for setting IP and port as properties on the QML side, that's not hard but I don't think there's a problem with exposing them inside a string to be shown on the server's UI. But if you think setting them as properties will solve the issue with the connection, OK I do that.

                  GrecKoG 1 Reply Last reply
                  0
                  • Q qcoderpro

                    @GrecKo
                    Yes, you're right. I removed the call from the constructor (in C++) but still nothing different in result. As for setting IP and port as properties on the QML side, that's not hard but I don't think there's a problem with exposing them inside a string to be shown on the server's UI. But if you think setting them as properties will solve the issue with the connection, OK I do that.

                    GrecKoG Offline
                    GrecKoG Offline
                    GrecKo
                    Qt Champions 2018
                    wrote on last edited by
                    #10

                    @qcoderpro I don't think that will solve the problem, I just think that's the proper and sane way to do it.

                    Are you connecting the newConnection signal in the QTcpServer created in the initServer() called from QML?

                    Q 1 Reply Last reply
                    0
                    • GrecKoG GrecKo

                      @qcoderpro I don't think that will solve the problem, I just think that's the proper and sane way to do it.

                      Are you connecting the newConnection signal in the QTcpServer created in the initServer() called from QML?

                      Q Offline
                      Q Offline
                      qcoderpro
                      wrote on last edited by
                      #11

                      @GrecKo

                      Are you connecting the newConnection signal in the QTcpServer created in the initServer() called from QML?

                      What did you mean, please? I didn't get this completely.
                      The connection:
                      connect(tcpServer, &QTcpServer::newConnection, this, &Server::onNewConnection); is defined in the serve's constructor. And I guess that signal (newConnection) is emitted when there's a connection coming from the client app (for example in the sendAddress slot there). That is, I guess when that slot in the client is called, the tcpSocket connects to host successfully, and on the other side, the server is that way notified with a new connection signal to it in turn calls the onNewConnection slot. Is it wrong to you?

                      1 Reply Last reply
                      0
                      • GrecKoG Offline
                        GrecKoG Offline
                        GrecKo
                        Qt Champions 2018
                        wrote on last edited by 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 Reply Last reply
                        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 Offline
                          Q Offline
                          qcoderpro
                          wrote on last edited by 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 Reply Last reply
                          0
                          • GrecKoG Offline
                            GrecKoG Offline
                            GrecKo
                            Qt Champions 2018
                            wrote on last edited by
                            #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 Reply Last reply
                            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 Offline
                              Q Offline
                              qcoderpro
                              wrote on last edited by
                              #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 Reply Last reply
                              0
                              • Q Offline
                                Q Offline
                                qcoderpro
                                wrote on last edited by
                                #16

                                No further help! :(

                                1 Reply Last reply
                                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 Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #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 Reply Last reply
                                  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 Offline
                                    Q Offline
                                    qcoderpro
                                    wrote on last edited by
                                    #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 Reply Last reply
                                    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 Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #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 Reply Last reply
                                      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 Offline
                                        Q Offline
                                        qcoderpro
                                        wrote on last edited by
                                        #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 Reply Last reply
                                        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 Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #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 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved