-1

hello everyone I use python send a string to qt but i do not know how show the string on a label can anyone help me ??? my mainwindow.cpp is

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);
    //QByteArray Msg= tcpSocket->readAll();
    readMessage();
}

 void MainWindow::showTime()
{
   QTime time=QTime::currentTime();
   QString time_text=time.toString("hh:mm:ss");
   ui->Digital_clock->setText(time_text);

   QDateTime dateTime = QDateTime::currentDateTime();
   QString datetimetext=dateTime.toString();
   ui->date->setText(datetimetext);



}

void MainWindow::readMessage()
{

    ui->receivedata_2->setText("no connection yet");

    if(!tcpServer.listen(QHostAddress::Any,42207))
    ui->receivedata_2->setText("waitting!");
     //QByteArray Msg= tcpSocket->readAll();

}

every time i try to put socket->readall() it will get crashed when i debug

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
robinshion
  • 33
  • 1
  • 7
  • What type is `tcpSocket` (although I'm guessing `QTcpSocket*`) and where do you initialize it (should probably be the value returned from [`tcpServer.nextPendingConnection`](http://doc.qt.io/qt-5/qtcpserver.html#nextPendingConnection()))? – G.M. Jul 04 '17 at 20:38

1 Answers1

0

The connection between sockets is not necessarily sequential, can occur at any time so that QtcpServer handles appropriate signals, when creating a new connection we must use the signal newConnection.

In the slot that connects to the previous signal we must use nextPendingConnection that returns a pending connection through a socket.

This socket issues the readyRead signal when there is pending information, and in that task you get the data you want.

Also when disconnected this emits the signal disconnected, in that slot we must eliminate the socket.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTcpServer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void newConnection();
    void readyRead();
    void disconnected();
private:
    Ui::MainWindow *ui;

    QTcpServer* tcpServer;
};

#endif // MAINWINDOW_H

mainwindow.h

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QTcpSocket>
#include <QLabel>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tcpServer = new QTcpServer(this);

    connect(tcpServer, &QTcpServer::newConnection, this, &MainWindow::newConnection);

    if(!tcpServer->listen(QHostAddress::Any,42207)){
        qDebug() << "Server could not start";
    }
    else{
        qDebug() << "Server started!";
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::newConnection()
{
    QTcpSocket *socket = tcpServer->nextPendingConnection();
    connect(socket, &QTcpSocket::readyRead, this, &MainWindow::readyRead);
    connect(socket, &QTcpSocket::disconnected, this, &MainWindow::disconnected);
}

void MainWindow::readyRead()
{
    QTcpSocket* socket = qobject_cast<QTcpSocket *>(sender());
    ui->receivedata_2->setText(socket->readAll());
}

void MainWindow::disconnected()
{
    sender()->deleteLater();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks so much for your reply. I put the code in to the qt but it seems not get connection. i can read from python said the connection refused – robinshion Jul 05 '17 at 12:50