【问题标题】:change Qt GUI by Qt tcp socket?通过 Qt tcp 套接字更改 Qt GUI?
【发布时间】:2017-11-15 21:01:02
【问题描述】:

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

服务器.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

服务器.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

我真的很感激。

【问题讨论】:

  • 你能隔离你认为可能是问题的代码吗?看来你有一个很直接的问题。

标签: c++ qt qt5 qtcpsocket qtcpserver


【解决方案1】:

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

一种可能的解决方案是创建QLabel 作为该类的成员:

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

}

【讨论】:

    猜你喜欢
    • 2014-10-19
    • 2014-04-08
    • 1970-01-01
    • 2014-12-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    相关资源
    最近更新 更多