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 ?
-
@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
-
@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(); }
-
remove
readMessage();
@Tofu_panda said in How to receive a string from python and read on Qlabel ?:
show me wrong
What "shows wrong"?
-
@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]{
^ -
@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);
-
@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
Go to Topic tools and Mark As Solved