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