《Qt Cerator》TCP通信——远程画图

看图

TcpPainter.pro

#网络库: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_H

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);
//	绘制到 “Pix”
    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();
//	创建“Buffer”(文件)
    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_H

TcpServer.cpp

#include "TcpServer.h"

TcpServer::TcpServer(QWidget *parent) : QWidget(parent)
{
//	创建服务器
    this->_Server=new QTcpServer;
//	监听 IP 对应的 端口
    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);
}

猜你喜欢

转载自blog.csdn.net/baidu_41905806/article/details/85988756
今日推荐