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 Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Solved General and Desktop
qt5pythonsockettcpserver
11 Posts 4 Posters 4.9k 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
    4 Jul 2017, 18:10

    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 ?

    V Offline
    V Offline
    VRonin
    wrote on 4 Jul 2017, 19:03 last edited by VRonin 7 Apr 2017, 19:06
    #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 4 Jul 2017, 19:22
    1
    • V VRonin
      4 Jul 2017, 19:03

      @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 4 Jul 2017, 19:22 last edited by VRonin 7 Apr 2017, 19:44
      #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
      • V Offline
        V Offline
        VRonin
        wrote on 4 Jul 2017, 19:45 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 4 Jul 2017, 20:01
        0
        • V VRonin
          4 Jul 2017, 19:45

          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 4 Jul 2017, 20:01 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]{
          ^

          V 1 Reply Last reply 5 Jul 2017, 06:54
          1
          • V VRonin
            4 Jul 2017, 19:45

            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 4 Jul 2017, 20:09 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???

            J 1 Reply Last reply 5 Jul 2017, 04:52
            1
            • T Tofu_panda
              4 Jul 2017, 20:09

              @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???

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 5 Jul 2017, 04:52 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 5 Jul 2017, 15:02
              2
              • T Tofu_panda
                4 Jul 2017, 20:01

                @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]{
                ^

                V Offline
                V Offline
                VRonin
                wrote on 5 Jul 2017, 06:54 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 5 Jul 2017, 13:14
                1
                • V VRonin
                  5 Jul 2017, 06:54

                  @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 5 Jul 2017, 13:14 last edited by
                  #9

                  @VRonin it is work now ! thanks !

                  T 1 Reply Last reply 5 Jul 2017, 13:19
                  1
                  • T Tofu_panda
                    5 Jul 2017, 13:14

                    @VRonin it is work now ! thanks !

                    T Offline
                    T Offline
                    Taz742
                    wrote on 5 Jul 2017, 13:19 last edited by
                    #10

                    @Tofu_panda
                    Go to Topic tools and Mark As Solved

                    Do what you want.

                    1 Reply Last reply
                    1
                    • J jsulm
                      5 Jul 2017, 04:52

                      @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 5 Jul 2017, 15:02 last edited by
                      #11

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

                      1 Reply Last reply
                      1

                      11/11

                      5 Jul 2017, 15:02

                      • Login

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