思维导图
QT连接TCP网络通信
服务器
ser.h
namespace Ui { class Ser; }
class Ser : public QWidget
Ser(QWidget *parent = nullptr);
void on_connectbtn_clicked();
void newConnection_slot();
ser.cpp
Ser::Ser(QWidget *parent)
server=new QTcpServer(this); //实例化一个服务器
connect(server,&QTcpServer::newConnection,this,&Ser::newConnection_slot);
void Ser::on_connectbtn_clicked()
quint16 port=ui->portbtn->text().toUInt();
if(server->listen(QHostAddress::Any,port)){
QMessageBox::information(this,"成功","服务器启动成功");
QMessageBox::information(this,"失败","服务器启动失败");
void Ser::newConnection_slot(){
QTcpSocket *s=server->nextPendingConnection(); //获取最新连接的客户端套接字
clientList.push_back(s); //将套接字放入客户端链表中
connect(s,&QTcpSocket::readyRead,this,&Ser::readyRead_slot);
void Ser::readyRead_slot(){
for(int i=0;i<clientList.count();i++){
if(clientList[i]->state()==0){
for(int i=0;i<clientList.count();i++){
if(clientList[i]->bytesAvailable()!=0){
QByteArray msg=clientList[i]->readAll();
ui->msgwidget->addItem(QString::fromLocal8Bit(msg));
for(int j=0;j<clientList.count();j++){
clientList[j]->write(msg);
客户端
cli.h
namespace Ui { class cli; }
class cli : public QWidget
cli(QWidget *parent = nullptr);
void on_connectbtn_clicked();
void on_sendbtn_clicked();
void on_disconnectbtn_clicked();
void disconnected_slot();
cli.cpp
cli::cli(QWidget *parent)
socket=new QTcpSocket(this);
ui->disconnectbtn->setEnabled(false);
ui->sendbtn->setEnabled(false);
ui->msgedit->setEnabled(false);
connect(socket,&QTcpSocket::connected,this,&cli::connected_slot);
connect(socket,&QTcpSocket::readyRead,this,&cli::readyRead_slot);
connect(socket, &QTcpSocket::disconnected, this, &cli::disconnected_slot);
void cli::on_connectbtn_clicked()
username=ui->usernameedit->text();
QString ip=ui->ipedit->text();
quint16 port=ui->portedit->text().toUInt();
socket->connectToHost(ip,port);
void cli::connected_slot(){
ui->disconnectbtn->setEnabled(true);
ui->sendbtn->setEnabled(true);
ui->msgedit->setEnabled(true);
ui->usernameedit->setEnabled(false);
ui->ipedit->setEnabled(false);
ui->portedit->setEnabled(false);
ui->connectbtn->setEnabled(false);
QMessageBox::information(this,"成功","成功进入聊天室");
QString msg=username+": 进入聊天室";
socket->write(msg.toLocal8Bit());
void cli::readyRead_slot(){
QByteArray msg=socket->readAll();
ui->msgwidget->addItem(QString::fromLocal8Bit(msg));
void cli::on_sendbtn_clicked()
QString msg=username+": "+ui->msgedit->text();
socket->write(msg.toLocal8Bit());
void cli::on_disconnectbtn_clicked()
QString msg=username+": 离开聊天室";
socket->write(msg.toLocal8Bit());
socket->disconnectFromHost();
void cli::disconnected_slot(){
ui->disconnectbtn->setEnabled(false);
ui->sendbtn->setEnabled(false);
ui->msgedit->setEnabled(false);
ui->usernameedit->setEnabled(true);
ui->ipedit->setEnabled(true);
ui->portedit->setEnabled(true);
ui->connectbtn->setEnabled(true);
QMessageBox::information(this,"退出","退出成功");
效果图
绘制一个时钟
clockwidget.h
class ClockWidget : public QWidget
ClockWidget(QWidget *parent = nullptr);
void paintEvent(QPaintEvent *);
clockwidget.cpp
ClockWidget::ClockWidget(QWidget *parent) : QWidget(parent)
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, QOverload<>::of(&ClockWidget::update));
void ClockWidget::paintEvent(QPaintEvent *)
QTime currentTime = QTime::currentTime();
painter.setRenderHint(QPainter::Antialiasing, true);
painter.translate(width() / 2, height() / 2);
painter.scale(width() / 200.0, height() / 200.0);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::white);
painter.drawEllipse(QPoint(0, 0), 90, 90);
painter.setPen(Qt::black);
for (int i = 1; i <= 12; ++i) {
painter.drawLine(0, 70, 0, 80);
painter.setPen(Qt::gray);
for (int i = 0; i < 60; ++i) {
painter.drawLine(0, 70, 0, 75);
painter.setPen(Qt::black);
painter.rotate(30.0 * ((currentTime.hour() + currentTime.minute() / 60.0)));
painter.drawLine(0, 0, 0, -40);
painter.setPen(Qt::blue);
painter.rotate(6.0 * ((currentTime.minute() + currentTime.second() / 60.0)));
painter.drawLine(0, 0, 0, -60);
painter.rotate(6.0 * currentTime.second());
painter.drawLine(0, 0, 0, -70);
painter.setPen(Qt::black);
painter.setBrush(Qt::black);
painter.drawEllipse(QPoint(0, 0), 3, 3);
效果图