• Qt(day4)


    思维导图

     QT连接TCP网络通信

    服务器

    ser.h

    1. #ifndef SER_H
    2. #define SER_H
    3. #include
    4. #include //服务器头文件
    5. #include //客户端头文件
    6. #include //消息对话框
    7. #include //链表头文件
    8. QT_BEGIN_NAMESPACE
    9. namespace Ui { class Ser; }
    10. QT_END_NAMESPACE
    11. class Ser : public QWidget
    12. {
    13. Q_OBJECT
    14. public:
    15. Ser(QWidget *parent = nullptr);
    16. ~Ser();
    17. private slots:
    18. void on_connectbtn_clicked();
    19. void newConnection_slot(); //处理newConnection信号的槽函数
    20. void readyRead_slot(); //处理readyRead信号的槽函数
    21. private:
    22. Ui::Ser *ui;
    23. QTcpServer *server; //定义服务器指针
    24. QList clientList; //定义客户端容器
    25. };
    26. #endif // SER_H

    ser.cpp

    1. #include "ser.h"
    2. #include "ui_ser.h"
    3. Ser::Ser(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::Ser)
    6. {
    7. ui->setupUi(this);
    8. server=new QTcpServer(this); //实例化一个服务器
    9. connect(server,&QTcpServer::newConnection,this,&Ser::newConnection_slot);
    10. }
    11. Ser::~Ser()
    12. {
    13. delete ui;
    14. }
    15. //启动服务器按钮对应的槽函数
    16. void Ser::on_connectbtn_clicked()
    17. {
    18. quint16 port=ui->portbtn->text().toUInt();
    19. if(server->listen(QHostAddress::Any,port)){
    20. QMessageBox::information(this,"成功","服务器启动成功");
    21. }
    22. else{
    23. QMessageBox::information(this,"失败","服务器启动失败");
    24. return;
    25. }
    26. }
    27. //处理newConnection信号的槽函数
    28. void Ser::newConnection_slot(){
    29. QTcpSocket *s=server->nextPendingConnection(); //获取最新连接的客户端套接字
    30. clientList.push_back(s); //将套接字放入客户端链表中
    31. connect(s,&QTcpSocket::readyRead,this,&Ser::readyRead_slot);
    32. }
    33. //处理readyRead信号的槽函数
    34. void Ser::readyRead_slot(){
    35. for(int i=0;i<clientList.count();i++){
    36. if(clientList[i]->state()==0){
    37. clientList.removeAt(i);
    38. }
    39. }
    40. for(int i=0;i<clientList.count();i++){
    41. if(clientList[i]->bytesAvailable()!=0){
    42. QByteArray msg=clientList[i]->readAll();
    43. ui->msgwidget->addItem(QString::fromLocal8Bit(msg));
    44. for(int j=0;j<clientList.count();j++){
    45. clientList[j]->write(msg);
    46. }
    47. }
    48. }
    49. }

    客户端

    cli.h

    1. #ifndef CLI_H
    2. #define CLI_H
    3. #include
    4. #include
    5. #include
    6. QT_BEGIN_NAMESPACE
    7. namespace Ui { class cli; }
    8. QT_END_NAMESPACE
    9. class cli : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. cli(QWidget *parent = nullptr);
    14. ~cli();
    15. private slots:
    16. void on_connectbtn_clicked();
    17. void on_sendbtn_clicked();
    18. void on_disconnectbtn_clicked();
    19. void connected_slot();
    20. void readyRead_slot();
    21. void disconnected_slot();
    22. private:
    23. Ui::cli *ui;
    24. QTcpSocket *socket;
    25. QString username;
    26. };
    27. #endif // CLI_H

    cli.cpp

    1. #include "cli.h"
    2. #include "ui_cli.h"
    3. cli::cli(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::cli)
    6. {
    7. ui->setupUi(this);
    8. socket=new QTcpSocket(this);
    9. ui->disconnectbtn->setEnabled(false);
    10. ui->sendbtn->setEnabled(false);
    11. ui->msgedit->setEnabled(false);
    12. connect(socket,&QTcpSocket::connected,this,&cli::connected_slot);
    13. connect(socket,&QTcpSocket::readyRead,this,&cli::readyRead_slot);
    14. connect(socket, &QTcpSocket::disconnected, this, &cli::disconnected_slot);
    15. }
    16. cli::~cli()
    17. {
    18. delete ui;
    19. }
    20. void cli::on_connectbtn_clicked()
    21. {
    22. username=ui->usernameedit->text();
    23. QString ip=ui->ipedit->text();
    24. quint16 port=ui->portedit->text().toUInt();
    25. socket->connectToHost(ip,port);
    26. }
    27. void cli::connected_slot(){
    28. ui->disconnectbtn->setEnabled(true);
    29. ui->sendbtn->setEnabled(true);
    30. ui->msgedit->setEnabled(true);
    31. ui->usernameedit->setEnabled(false);
    32. ui->ipedit->setEnabled(false);
    33. ui->portedit->setEnabled(false);
    34. ui->connectbtn->setEnabled(false);
    35. QMessageBox::information(this,"成功","成功进入聊天室");
    36. QString msg=username+": 进入聊天室";
    37. socket->write(msg.toLocal8Bit());
    38. }
    39. void cli::readyRead_slot(){
    40. QByteArray msg=socket->readAll();
    41. ui->msgwidget->addItem(QString::fromLocal8Bit(msg));
    42. }
    43. void cli::on_sendbtn_clicked()
    44. {
    45. QString msg=username+": "+ui->msgedit->text();
    46. socket->write(msg.toLocal8Bit());
    47. ui->msgedit->clear();
    48. }
    49. void cli::on_disconnectbtn_clicked()
    50. {
    51. QString msg=username+": 离开聊天室";
    52. socket->write(msg.toLocal8Bit());
    53. socket->disconnectFromHost();
    54. }
    55. void cli::disconnected_slot(){
    56. ui->disconnectbtn->setEnabled(false);
    57. ui->sendbtn->setEnabled(false);
    58. ui->msgedit->setEnabled(false);
    59. ui->usernameedit->setEnabled(true);
    60. ui->ipedit->setEnabled(true);
    61. ui->portedit->setEnabled(true);
    62. ui->connectbtn->setEnabled(true);
    63. QMessageBox::information(this,"退出","退出成功");
    64. }

    效果图

     绘制一个时钟

    clockwidget.h

    1. #ifndef CLOCKWIDGET_H
    2. #define CLOCKWIDGET_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. class ClockWidget : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. ClockWidget(QWidget *parent = nullptr);
    12. protected:
    13. void paintEvent(QPaintEvent *);
    14. };
    15. #endif // CLOCKWIDGET_H

    clockwidget.cpp

    1. #include "clockwidget.h"
    2. ClockWidget::ClockWidget(QWidget *parent) : QWidget(parent)
    3. {
    4. setFixedSize(400, 400);
    5. QTimer *timer = new QTimer(this);
    6. connect(timer, &QTimer::timeout, this, QOverload<>::of(&ClockWidget::update));
    7. timer->start(1000);
    8. }
    9. void ClockWidget::paintEvent(QPaintEvent *)
    10. {
    11. QPainter painter(this);
    12. QTime currentTime = QTime::currentTime();
    13. painter.setRenderHint(QPainter::Antialiasing, true);
    14. painter.translate(width() / 2, height() / 2);
    15. painter.scale(width() / 200.0, height() / 200.0);
    16. painter.setPen(Qt::NoPen);
    17. painter.setBrush(Qt::white);
    18. painter.drawEllipse(QPoint(0, 0), 90, 90);
    19. painter.setPen(Qt::black);
    20. for (int i = 1; i <= 12; ++i) {
    21. painter.drawLine(0, 70, 0, 80);
    22. painter.rotate(30);
    23. }
    24. painter.setPen(Qt::gray);
    25. for (int i = 0; i < 60; ++i) {
    26. if (i % 5 != 0)
    27. painter.drawLine(0, 70, 0, 75);
    28. painter.rotate(6);
    29. }
    30. //时针
    31. painter.setPen(Qt::black);
    32. painter.save();
    33. painter.rotate(30.0 * ((currentTime.hour() + currentTime.minute() / 60.0)));
    34. painter.drawLine(0, 0, 0, -40);
    35. painter.restore();
    36. //分针
    37. painter.setPen(Qt::blue);
    38. painter.save();
    39. painter.rotate(6.0 * ((currentTime.minute() + currentTime.second() / 60.0)));
    40. painter.drawLine(0, 0, 0, -60);
    41. painter.restore();
    42. //秒针
    43. painter.setPen(Qt::red);
    44. painter.save();
    45. painter.rotate(6.0 * currentTime.second());
    46. painter.drawLine(0, 0, 0, -70);
    47. painter.restore();
    48. painter.setPen(Qt::black);
    49. painter.setBrush(Qt::black);
    50. painter.drawEllipse(QPoint(0, 0), 3, 3);
    51. }

    效果图 

  • 相关阅读:
    Python接口自动化测试之详解post请求
    Python +Appium 实现app自动化测试
    baichuan-53B VS ChatGLM-6B对比
    【全开源】Java聚合跑腿系统对接云洋聚合跑腿系统源码低价快递小程序APP公众号源码
    C语言 do while循环练习 上
    C++位操作
    HDLbits exercises 3 (procedures节选题)
    基于STM32程序万年历液晶1602显示-proteus仿真-源程序
    数据库使用psql及jdbc进行远程连接,不定时自动断开的解决办法
    【c++】weak_ptr和观察者模式
  • 原文地址:https://blog.csdn.net/qq_53268516/article/details/133094724