简体   繁体   English

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

[英]change Qt GUI by Qt tcp socket?

i have a server and client.i am going to change the handle of my clock to new value that will send by client to server.i added the source code below. 我有一个服务器和client.i将时钟的句柄更改为新值,该值将由client发送到server.i在下面添加了源代码。 my problem is when client connect to server after client send data to server to change the handle of clock to new data. 我的问题是当客户端向服务器发送数据以将时钟的句柄更改为新数据后,客户端连接到服务器。 the gui that created in Server::startRead() method appear and disappear automatically . 在Server :: startRead()方法中创建的gui会自动出现和消失 what is wrong with my code please. 请问我的代码有什么问题。

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 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 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();
}

application output 应用程序输出

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

I really appreciate it. 我真的很感激。

The variables created in the functions only exist while the function is called, so when the function is finished executing the variables are eliminated, and in your case the QLabel is eliminated. 在函数中创建的变量仅在调用函数时存在,因此在函数执行完毕后,将消除变量,在您的情况下,将消除QLabel。

A possible solution is to create QLabel as a member of the class: 一个可能的解决方案是将QLabel创建为该类的成员:

server.h 服务器

private:
    QLabel *label;

server.cpp 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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM