Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to receive a string from python and read on Qlabel ?
Forum Updated to NodeBB v4.3 + New Features

How to receive a string from python and read on Qlabel ?

Scheduled Pinned Locked Moved Solved General and Desktop
qt5pythonsockettcpserver
11 Posts 4 Posters 5.1k Views
  • 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.
  • T Tofu_panda

    Hi guys I have a python string send in local network:

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    stringTosend = ("Hello,World")
    print(stringTosend)
    sock.connect(('192.168.2.39', 42207))
    try:
     sock.sendall(stringTosend)
    except socket.error:
     print 'Send failed'
     sys.exit()
    print'Sent'
    

    and my qt main.cpp is:

        tcpServer.listen(QHostAddress::Any,42207);
        readMessage();
    
    void MainWindow::readMessage()
    {
    
        ui->receivedata_2->setText("no connection yet");
        QTcpSocket *socket = tcpServer.nextPendingConnection();
        ui->receivedata_2->setText("received connection");
       QByteArray received = socket->readAll();
        ui->receivedata_2->setText(received);
        socket->close();
       socket->deleteLater();
    }
    

    but it is not work ..... any one canhelp me ?

    VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by VRonin
    #2

    @Tofu_panda said in How to receive a string from python and read on Qlabel ?:

    QTcpSocket *socket = tcpServer.nextPendingConnection();

    this probably returns NULL, you have to wait for the newConnection() signal before calling nextPendingConnection()

    @Tofu_panda said in How to receive a string from python and read on Qlabel ?:

    QByteArray received = socket->readAll();

    You can't blindly read all, you need to wait for the socket to emit readyRead() signal (or call waitForReadyRead) before reading then you need to make sure all the data has been received (but in the use case above you have no way to do it as the sender gives you no way of knowing how much data it is sending unless python does something magic in sendall and adds a header to the message)

    See http://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html and http://doc.qt.io/qt-5/qtnetwork-fortuneclient-example.html

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    T 1 Reply Last reply
    1
    • VRoninV VRonin

      @Tofu_panda said in How to receive a string from python and read on Qlabel ?:

      QTcpSocket *socket = tcpServer.nextPendingConnection();

      this probably returns NULL, you have to wait for the newConnection() signal before calling nextPendingConnection()

      @Tofu_panda said in How to receive a string from python and read on Qlabel ?:

      QByteArray received = socket->readAll();

      You can't blindly read all, you need to wait for the socket to emit readyRead() signal (or call waitForReadyRead) before reading then you need to make sure all the data has been received (but in the use case above you have no way to do it as the sender gives you no way of knowing how much data it is sending unless python does something magic in sendall and adds a header to the message)

      See http://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html and http://doc.qt.io/qt-5/qtnetwork-fortuneclient-example.html

      T Offline
      T Offline
      Tofu_panda
      wrote on last edited by VRonin
      #3

      @VRonin said in How to receive a string from python and read on Qlabel ?:

      yRead() signal (or call waitForReadyRead) be

      thank you for your reply i make some modify but still show me wrong it is :

      #include <QApplication>
      #include <QtNetwork>
      #include <string>
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          QTimer *timer=new QTimer(this);
          connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
          timer->start();
      
      
          tcpServer.listen(QHostAddress::Any,42207);
          QObject::connect(&tcpServer,&QTcpServer::newConnection,[&tcpServer]{
              QTcpSocket *socket = tcpServer.nextPendingConnection();
              QObject::connect(socket,&QTcpSocket::readyRead,[socket]{
                  QByteArray received = socket->readAll();
              });
          });
         
      
          readMessage();
      }
      
      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #4

        remove readMessage();

        @Tofu_panda said in How to receive a string from python and read on Qlabel ?:

        show me wrong

        What "shows wrong"?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        T 2 Replies Last reply
        0
        • VRoninV VRonin

          remove readMessage();

          @Tofu_panda said in How to receive a string from python and read on Qlabel ?:

          show me wrong

          What "shows wrong"?

          T Offline
          T Offline
          Tofu_panda
          wrote on last edited by
          #5

          @VRonin
          thanks your reply it show me like this:
          C:\Users\Administrator\Desktop\Views\test1\mainwindow.cpp:25: error: capture of non-variable 'MainWindow::tcpServer'
          QObject::connect(&tcpServer,&QTcpServer::newConnection,[&tcpServer]{
          ^

          VRoninV 1 Reply Last reply
          1
          • VRoninV VRonin

            remove readMessage();

            @Tofu_panda said in How to receive a string from python and read on Qlabel ?:

            show me wrong

            What "shows wrong"?

            T Offline
            T Offline
            Tofu_panda
            wrote on last edited by
            #6

            @VRonin
            Do you mind I asking now i think the python can send string successfully , qt connect the ip add and port is successfully , but i do not know how to show the string on the label ? can you help me out???

            jsulmJ 1 Reply Last reply
            1
            • T Tofu_panda

              @VRonin
              Do you mind I asking now i think the python can send string successfully , qt connect the ip add and port is successfully , but i do not know how to show the string on the label ? can you help me out???

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @Tofu_panda Just read the documentation: http://doc.qt.io/qt-5/qstring.html#QString-8 and http://doc.qt.io/qt-5/qlabel.html#text-prop

              QByteArray received = socket->readAll();
              QString str(received);
              ui->label->setText(str);
              

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

              T 1 Reply Last reply
              2
              • T Tofu_panda

                @VRonin
                thanks your reply it show me like this:
                C:\Users\Administrator\Desktop\Views\test1\mainwindow.cpp:25: error: capture of non-variable 'MainWindow::tcpServer'
                QObject::connect(&tcpServer,&QTcpServer::newConnection,[&tcpServer]{
                ^

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #8

                @Tofu_panda said in How to receive a string from python and read on Qlabel ?:

                capture of non-variable 'MainWindow::tcpServer'

                The error is quite clear, you are capturing a member without capturing this. change [&tcpServer] to [this]

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                T 1 Reply Last reply
                1
                • VRoninV VRonin

                  @Tofu_panda said in How to receive a string from python and read on Qlabel ?:

                  capture of non-variable 'MainWindow::tcpServer'

                  The error is quite clear, you are capturing a member without capturing this. change [&tcpServer] to [this]

                  T Offline
                  T Offline
                  Tofu_panda
                  wrote on last edited by
                  #9

                  @VRonin it is work now ! thanks !

                  Taz742T 1 Reply Last reply
                  1
                  • T Tofu_panda

                    @VRonin it is work now ! thanks !

                    Taz742T Offline
                    Taz742T Offline
                    Taz742
                    wrote on last edited by
                    #10

                    @Tofu_panda
                    Go to Topic tools and Mark As Solved

                    Do what you want.

                    1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @Tofu_panda Just read the documentation: http://doc.qt.io/qt-5/qstring.html#QString-8 and http://doc.qt.io/qt-5/qlabel.html#text-prop

                      QByteArray received = socket->readAll();
                      QString str(received);
                      ui->label->setText(str);
                      
                      T Offline
                      T Offline
                      Tofu_panda
                      wrote on last edited by
                      #11

                      @jsulm
                      thanks for you reply! it is work! thanks again!

                      1 Reply Last reply
                      1

                      • Login

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