#网络库:network(这个很重要)
QT += widgets gui network
HEADERS += \
TcpServer.h \
TcpClient.h
SOURCES += \
TcpServer.cpp \
TcpClient.cpp \
main.cpp
main.cpp
#include <QApplication>
#include "TcpServer.h"
#include "TcpClient.h"
int main(int Argc,char** Argv)
{
QApplication App(Argc,Argv);
TcpServer Server;
Server.setWindowTitle("Server");
Server.show();
TcpClient Client;
Client.setWindowTitle("Client");
Client.show();
return App.exec();
}
TcpClient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QWidget>
#include <QTcpSocket>
#include <QPainter>
#include <QMouseEvent>
#include <QBuffer>
#include <QByteArray>
#include <QVector>
class TcpClient : public QWidget
{
Q_OBJECT
public:
explicit TcpClient(QWidget *parent = nullptr);
private:
QTcpSocket* _Socket;
QVector<QVector<QPoint>> _Lines;
private:
void paintEvent(QPaintEvent* Ev);
void mousePressEvent(QMouseEvent* Ev);
void mouseReleaseEvent(QMouseEvent* Ev);
void mouseMoveEvent(QMouseEvent* Ev);
signals:
public slots:
};
#endif
TcpClient.cpp
#include "TcpClient.h"
TcpClient::TcpClient(QWidget *parent) : QWidget(parent)
{
this->_Socket=new QTcpSocket;
this->_Socket->connectToHost("127.0.0.1",8848);
}
void TcpClient::paintEvent(QPaintEvent* Ev)
{
QPixmap Pix(this->size());
Pix.fill(Qt::white);
QPainter p(&Pix);
for(int i(0);i<this->_Lines.size();i++)
{
const QVector<QPoint>& Line(this->_Lines.at(i));
for(int j(0);j<Line.size()-1;j++)
{
p.drawLine(Line.at(j),Line.at(j+1));
}
}
p.end();
QBuffer SendBuf;
SendBuf.open(QIODevice::ReadWrite);
Pix.save(&SendBuf,"PNG");
this->_Socket->write(SendBuf.buffer());
p.begin(this);
p.drawPixmap(0,0,Pix);
}
void TcpClient::mousePressEvent(QMouseEvent* Ev)
{
QVector<QPoint>Line;
this->_Lines.append(Line);
QVector<QPoint>& LastLine(this->_Lines.last());
LastLine.append(Ev->pos());
}
void TcpClient::mouseReleaseEvent(QMouseEvent* Ev)
{
QVector<QPoint>& LastLine(this->_Lines.last());
LastLine.append(Ev->pos());
}
void TcpClient::mouseMoveEvent(QMouseEvent* Ev)
{
QVector<QPoint>& LastLine(this->_Lines.last());
LastLine.append(Ev->pos());
update();
}
TcpServer.h
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QPainter>
#include <QPixmap>
#include <QBuffer>
#include <QTextBrowser>
#include <QVBoxLayout>
class TcpServer : public QWidget
{
Q_OBJECT
public:
explicit TcpServer(QWidget *parent = nullptr);
private:
QTcpServer* _Server;
QTcpSocket* _Socket;
QPixmap _Pix;
private:
void paintEvent(QPaintEvent* Ev);
signals:
public slots:
void SlotNewConnection();
void SlotReadyRead();
};
#endif
TcpServer.cpp
#include "TcpServer.h"
TcpServer::TcpServer(QWidget *parent) : QWidget(parent)
{
this->_Server=new QTcpServer;
this->_Server->listen(QHostAddress("127.0.0.1"),8848);
connect(this->_Server,SIGNAL(newConnection()),this,SLOT(SlotNewConnection()));
}
void TcpServer::SlotNewConnection()
{
while(this->_Server->hasPendingConnections())
{
this->_Socket=this->_Server->nextPendingConnection();
connect(this->_Socket,SIGNAL(readyRead()),this,SLOT(SlotReadyRead()));
}
}
void TcpServer::SlotReadyRead()
{
QByteArray Buf(this->_Socket->readAll());
this->_Pix.loadFromData(Buf);
update();
}
void TcpServer::paintEvent(QPaintEvent* Ev)
{
QPainter p(this);
p.drawPixmap(0,0,this->_Pix);
}