hh
绘制
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QMouseEvent>
#include<QPaintEvent>
#include<QPixmap>
#include<QPainter>
#include<QPen>
#include<QColorDialog>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void paintEvent(QPaintEvent *event) override;
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
QPixmap *pix;
QPoint startPoint;
QColor c;
int pointSize = 3;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include<QInputDialog>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
pix=new QPixmap(this->size());
pix->fill();
c=QColor("red");
}
Widget::~Widget()
{
delete ui;
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
QPainter p1(pix);
QPen pen(QColor("red"));
pen.setWidth(5);
p1.setPen(pen);
p1.drawLine(startPoint,event->pos());
startPoint=event->pos();
this->update();
}
void Widget::mousePressEvent(QMouseEvent *event)
{
startPoint = event->pos();
}
void Widget::paintEvent(QPaintEvent *event)
{
//实例化一个画家
QPainter p2(this);
//将pix绘制到ui界面
p2.drawPixmap(QPoint(0,0),*pix);
}
void Widget::on_pushButton_clicked()
{
c = QColorDialog::getColor();
}
void Widget::on_pushButton_2_clicked()
{
pointSize=QInputDialog::getInt(this,"输入线条","请输入");
}
tcp服务器
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTcpServer>
#include<QTcpSocket>
#include<QList>
#include<QMessageBox>
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_serverBtn_clicked();
void newConnection_slot(); //自定义处理newConnection信号的槽函数的声明
void readyRead_slot();
private:
Ui::Widget *ui;
QTcpServer *server;
QList<QTcpSocket *>clientList;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
server=new QTcpServer(this);
connect(server, &QTcpServer::newConnection, this, &Widget::newConnection_slot);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_serverBtn_clicked()
{
if(ui->serverBtn->text() == "启动服务器")
{
//启动服务器逻辑
//获取ui界面上的端口号
quint16 port = ui->portEdit->text().toUInt();
//启动监听
//函数原型:bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);、
//参数1:要监听的ip地址,QHostAddress::Any表示监听任意一台主机
//参数2:服务器的端口号,如果填0表示让系统自动分配端口号
//返回值:成功监听返回真(成功启动服务器),否则返回假
if(!server->listen(QHostAddress::Any, port))
{
QMessageBox::information(this,"提示","服务器启动失败");
return ;
}else
{
QMessageBox::information(this,"提示","服务器启动成功");
}
//程序执行至此,表示服务器已经启动了,此时如果有客户端向服务器发来连接请求,那么该服务器就会自动发射一个newConnection的信号
//我们可以将该信号连接到自定义的槽函数中执行相关逻辑,由于连接只需要一次,我们可以写在构造函数中
//更新按钮文本内容
ui->serverBtn->setText("关闭服务器");
}else
{
//关闭服务器逻辑
server->close();
//更新按钮文本内容
ui->serverBtn->setText("启动服务器");
}
}
void Widget::newConnection_slot()
{
//QMessageBox::information(this, "提示", "有新客户发来连接请求了");
qDebug() << "有新客户端发来连接请求";
//获取最新连接的客户端套接字
//函数原型:QTcpSocket *nextPendingConnection();
//参数:无
//返回值:最新连接的套接字的地址
QTcpSocket* s = server->nextPendingConnection();
//将该套接字放入到客户端链表中即可
clientList.push_back(s);
//程序执行至此,一个服务器和多个客户端就已经建立了连接
//当某个客户端向服务器发送消息后,该套接字就会自动发射一个readyRead信号
//我们可以将该信号连接到自定义的槽函数中执行相关逻辑
connect(s, &QTcpSocket::readyRead, this, &Widget::readyRead_slot);
}