qtcSocket如何返回QString

qtcSocket如何返回QString,qt,qtcpsocket,Qt,Qtcpsocket,我有一个服务:GWT客户端调用QT QTcpSocket函数,该函数向设备发出请求并获取响应(它不能只是一个响应,我应该等待所有响应) 根据QT文档,我不能使用waitForReadyRead()函数,因为我使用的是Windows平台 注意:此功能在Windows上可能会随机失败。考虑使用 事件循环和readyRead()信号(如果软件将在上运行) 窗户 我现在只有一个决定: 伪代码: QString MainQTFunc() { create new thread; whi

我有一个服务:GWT客户端调用QT QTcpSocket函数,该函数向设备发出请求并获取响应(它不能只是一个响应,我应该等待所有响应)

根据QT文档,我不能使用waitForReadyRead()函数,因为我使用的是Windows平台

注意:此功能在Windows上可能会随机失败。考虑使用 事件循环和readyRead()信号(如果软件将在上运行) 窗户

我现在只有一个决定:

伪代码:

QString MainQTFunc() {

    create new thread;

    while (!thread.isStopped()) {
        sleep(x);
    }

    return QString variable from thread to GWT client;
}



 New Thread {

      run() {
          make a TcpRequest to the device...
      }

    boolean isStopped() {
        if(we got the response!!!) {
            return true;
        }
    }

 }
这样做是最好的解决方案吗?我不明白在得到结果后如何简单地发送QString变量。强大的QT真的不可能吗?

现在我有(没有任何线程):

//函数应该将QString返回给GWT客户端
QString主窗口::TcpConnect(QByteArray数据){
_pSocket=新的QTcpSocket(本);
连接(_pSocket,信号(readyRead()),插槽(readTcpData());
连接(_pSocket,信号(connected()),插槽(connected());
连接(_pSocket,信号(disconnected()),插槽(disconnected());
数据全局=数据;
_pSocket->connectToHost(“IP”,端口);
//在此处等待所有响应并发送最后一个响应
返回响应exglobal;
}
void MainWindow::connected(){

qDebug()这是我找到的最好的解决方案。它有效,但我不喜欢

QString JSPrinter::connectTcp(QByteArray data) {

        QTimer timer;
        timer.setSingleShot(true);
        QEventLoop loop;

        _pSocket = new QTcpSocket( this ); // <-- needs to be a member variable: QTcpSocket * _pSocket;

        connect( _pSocket,  SIGNAL(readyRead()),      SLOT(readTcpData()) );
        connect( _pSocket,  SIGNAL(connected()),      SLOT(connected()) );
        connect( _pSocket,  SIGNAL(disconnected()),   SLOT(disconnected()) );
        connect( &timer,    SIGNAL(timeout()), &loop, SLOT(quit()) );
        loop.connect( this, SIGNAL(exitLoop()),       SLOT(quit()) );

        dataGlobal = data;

        _pSocket->connectToHost(ip_, port_);

        timer.start(75000);
        loop.exec();

        if(timer.isActive())
            logger()->info("ok");
        else
            logger()->info("timeout");

        return responseHexGlobal;

    }



    void JSPrinter::connected() {

        qDebug() << "connected. " << QDateTime::currentDateTime();
        _pSocket->write( dataGlobal );

    }

    void JSPrinter::disconnected() {

        qDebug() << "disconnected. " << QDateTime::currentDateTime();

    }

    void JSPrinter::readTcpData() {

        QByteArray data = _pSocket->readAll();
        QByteArray as_hex_string = data.toHex();
        std::string stdString(as_hex_string.constData(), as_hex_string.length());
        qDebug() << "readTcpData response " << QDateTime::currentDateTime() << QString::fromStdString(stdString);

        // doing something...

        if(condition) {
            responseHexGlobal = received;
            qDebug() << "responseHexGlobal " << responseHexGlobal;
            emit exitLoop();
            _pSocket->disconnectFromHost();

        } else {
            emit exitLoop();
            _pSocket->disconnectFromHost();
            return;
        }

    }
QString JSPrinter::connectTcp(QByteArray数据){
定时器;
定时器设置单脉冲(真);
QEventLoop循环;
_pSocket=new qtcsocket(this);//connectToHost(ip_u,port_u);
定时器启动(75000);
loop.exec();
if(timer.isActive())
记录器()->信息(“正常”);
其他的
记录器()->info(“超时”);
返回响应exglobal;
}
void JSPrinter::connected(){
qDebug()
QString JSPrinter::connectTcp(QByteArray data) {

        QTimer timer;
        timer.setSingleShot(true);
        QEventLoop loop;

        _pSocket = new QTcpSocket( this ); // <-- needs to be a member variable: QTcpSocket * _pSocket;

        connect( _pSocket,  SIGNAL(readyRead()),      SLOT(readTcpData()) );
        connect( _pSocket,  SIGNAL(connected()),      SLOT(connected()) );
        connect( _pSocket,  SIGNAL(disconnected()),   SLOT(disconnected()) );
        connect( &timer,    SIGNAL(timeout()), &loop, SLOT(quit()) );
        loop.connect( this, SIGNAL(exitLoop()),       SLOT(quit()) );

        dataGlobal = data;

        _pSocket->connectToHost(ip_, port_);

        timer.start(75000);
        loop.exec();

        if(timer.isActive())
            logger()->info("ok");
        else
            logger()->info("timeout");

        return responseHexGlobal;

    }



    void JSPrinter::connected() {

        qDebug() << "connected. " << QDateTime::currentDateTime();
        _pSocket->write( dataGlobal );

    }

    void JSPrinter::disconnected() {

        qDebug() << "disconnected. " << QDateTime::currentDateTime();

    }

    void JSPrinter::readTcpData() {

        QByteArray data = _pSocket->readAll();
        QByteArray as_hex_string = data.toHex();
        std::string stdString(as_hex_string.constData(), as_hex_string.length());
        qDebug() << "readTcpData response " << QDateTime::currentDateTime() << QString::fromStdString(stdString);

        // doing something...

        if(condition) {
            responseHexGlobal = received;
            qDebug() << "responseHexGlobal " << responseHexGlobal;
            emit exitLoop();
            _pSocket->disconnectFromHost();

        } else {
            emit exitLoop();
            _pSocket->disconnectFromHost();
            return;
        }

    }