通过 Qt tcp 套接字更改 Qt GUI?

change Qt GUI by Qt tcp socket?

我有一个服务器,client.i 我打算将我的时钟句柄更改为新值,该值将由客户端发送给 server.i 添加了下面的源代码。 我的问题是当客户端向服务器发送数据后连接到服务器,将时钟句柄更改为新数据。 在 Server::startRead() 方法中创建的 gui 自动出现和消失。请问我的代码有什么问题。

server.h

#ifndef SERVER_H
#define SERVER_H
#include <QtNetwork>
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>

class Server: public QObject
{
Q_OBJECT
public:

   QString buf;
  Server(QObject * parent = 0);
  ~Server();
public slots:
  void acceptConnection();
  void startRead();
  QString getinfo();

private:
  QTcpServer server;
  QTcpSocket* client;
};


#endif // SERVER_H

server.cpp

#include <QGuiApplication>
#include "server.h"
#include <QDebug>
#include <iostream>
#include <QApplication>
#include "qlabel.h"
#include <QtGui>
Server::Server(QObject* parent): QObject(parent)
{

connect(&server, SIGNAL(newConnection()),
    this, SLOT(acceptConnection()));


  server.listen(QHostAddress::Any, 1234);
  qDebug() <<  "connected";
}

Server::~Server()
{
  server.close();
}

void Server::acceptConnection()
{

  qDebug()<<"accept connection";
  client = server.nextPendingConnection();
  client->write("Hello client\r\n");

  connect(client, SIGNAL(readyRead()),
    this, SLOT(startRead()));

}

void Server::startRead()
{

    client->waitForReadyRead(3000);
    QString buf=client->readAll();

    qDebug() << buf;

    static const QPoint minuteHand[3] = {
        QPoint(7, 8),
        QPoint(-7, 8),
        QPoint(0, -40)
    };
     int side = qMin(200, 200);
    QColor minuteColor(60, 60, 0);
    QPixmap pixmap( 200, 200 );
    pixmap.fill( Qt::red );
    QPainter painter( &pixmap );
    painter.setRenderHint(QPainter::Antialiasing);
    painter.translate(200 / 2, 200 / 2);
    painter.scale(side / 200.0, side / 200.0);

    painter.setPen(Qt::NoPen);

    for (int i = 0; i < 12; ++i) {
        painter.drawLine(88, 0, 96, 0);
        painter.rotate(30.0);
    }
    painter.setPen(Qt::NoPen);
    painter.setBrush(minuteColor);

    painter.save();
    painter.rotate(240);
    painter.drawConvexPolygon(minuteHand, 3);
    painter.restore();

    painter.setPen(minuteColor);

    for (int j = 0; j < 60; ++j) {
        if ((j % 5) != 0)
            painter.drawLine(92, 0, 96, 0);
        painter.rotate(6.0);
    }

    QLabel l;
    l.setPixmap(pixmap);
    l.show();

}
QString Server::getinfo()
{

 qDebug()<< buf;
    return buf;

}

main.cpp

#include "server.h"
#include <QApplication>
#include "analogclock.h"
#include "qlabel.h"
#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

Server s;



    return a.exec();
}

应用输出

Starting C:\Qt\Qt5.3.0_1\Tools\QtCreator\bin\build-NHAJA-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\NHAJA.exe...
connected
accept connection
"11"
C:\Qt\Qt5.3.0_1\Tools\QtCreator\bin\build-NHAJA-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\NHAJA.exe exited with code 0

非常感谢。

函数中创建的变量仅在调用函数时存在,因此当函数执行完毕时,变量将被消除,在您的情况下,QLabel 将被消除。

一个可能的解决方案是创建 QLabel 作为 class 的成员:

server.h

private:
    QLabel *label;

server.cpp

Server::Server(QObject *parent) : QObject(parent)
{
    [...]
    qDebug() <<  "connected";
    label = new QLabel;
}

void Server::startRead()
{
    [...]
    for (int j = 0; j < 60; ++j) {
        if ((j % 5) != 0)
            painter.drawLine(92, 0, 96, 0);
        painter.rotate(6.0);
    }

    label->setPixmap(pixmap);
    label->show();

}