How to receive a string from python and read on Qlabel ?
-
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 ?
wrote on 4 Jul 2017, 19:03 last edited by VRonin 7 Apr 2017, 19:06@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 callingnextPendingConnection()
@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
-
@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 callingnextPendingConnection()
@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
wrote on 4 Jul 2017, 19:22 last edited by VRonin 7 Apr 2017, 19:44@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(); }
-
wrote on 4 Jul 2017, 19:45 last edited by
remove
readMessage();
@Tofu_panda said in How to receive a string from python and read on Qlabel ?:
show me wrong
What "shows wrong"?
-
remove
readMessage();
@Tofu_panda said in How to receive a string from python and read on Qlabel ?:
show me wrong
What "shows wrong"?
wrote on 4 Jul 2017, 20:01 last edited by@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]{
^ -
remove
readMessage();
@Tofu_panda said in How to receive a string from python and read on Qlabel ?:
show me wrong
What "shows wrong"?
wrote on 4 Jul 2017, 20:09 last edited by@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??? -
@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???@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);
-
@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]{
^wrote on 5 Jul 2017, 06:54 last edited by@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]
-
@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]
wrote on 5 Jul 2017, 13:14 last edited by@VRonin it is work now ! thanks !
-
@VRonin it is work now ! thanks !
wrote on 5 Jul 2017, 13:19 last edited by@Tofu_panda
Go to Topic tools and Mark As Solved -
@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);
wrote on 5 Jul 2017, 15:02 last edited by@jsulm
thanks for you reply! it is work! thanks again!
11/11