QT聊天案例

原文链接(点击原文链接获取更多学习干货): 布尔博客

form.h

#ifndef FORM_H
#define FORM_H

#include <QWidget>

namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();
    QVector<bool>isShow;
private:
    Ui::Form *ui;
};

#endif // FORM_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QUdpSocket>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

    enum MsgType {Msg,UsrEnter,UsrLeft};
public:
    Widget(QWidget *parent ,QString name);
    ~Widget();
    void sendMsg(MsgType type);
    void userEnter(QString username);
    void userleft(QString username,QString time);
    QString getuser();
    QString getMsg();

    void closeEvent(QCloseEvent *event);
signals:
    void closeWidget();

private:
    Ui::Widget *ui;
    QUdpSocket* udpSocket;
    qint16 port;
    QString username;
    void ReceiveMessage();
};
#endif // WIDGET_H

form.cpp

#include "form.h"
#include "ui_form.h"
#include<QToolButton>
#include"widget.h"
#include<QMessageBox>

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
//    for(int i=0;i<6;i++)
//    {
    int i=0;

    ui->setupUi(this);
    setWindowTitle("MyseftQQ");//设置窗口的题目
    setWindowIcon(QPixmap(":/img/2.PNG"));//打开窗口的图片
    QToolButton * btn=new QToolButton;//
    btn->setText("water");//用户名
    btn->setIcon(QPixmap(":/img/1.PNG"));//设置图片
    btn->setIconSize(QPixmap(":/img/1.PNG").size());//设置图片的大小
    btn->setAutoRaise(true);//按钮是否自动凸出
    btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);//
    ui->verticalLayout_2->addWidget(btn);//添加按钮
    isShow.push_back(false);//添加数组元素
    connect(btn,&QToolButton::clicked,[=]()mutable//点击按钮打开窗口
    {
        if(isShow[i]==true)//防止重复打开敞口
        {
            QString str=QString ("%1窗口已打开").arg(btn->text());
            QMessageBox::warning(this,"警告",str);
            return ;
        }
        isShow[i]=true;//打开标志显示打开
        Widget *widget=new Widget(0,btn->text());
        widget->setWindowTitle(btn->text());//设置窗口的主题
        widget->setWindowIcon(btn->icon());//设置头像
        widget->show();//显示窗口
        connect(widget,&Widget::closeWidget,[=]()mutable{//清除标志
            isShow[i]=false;
        });
    });

   // }
}

Form::~Form()
{
    delete ui;
}


form.ui

?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>250</width>
    <height>700</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QToolBox" name="toolBox">
     <property name="currentIndex">
      <number>0</number>
     </property>
     <widget class="QWidget" name="page">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>98</width>
        <height>28</height>
       </rect>
      </property>
      <attribute name="label">
       <string>Page 1</string>
      </attribute>
      <layout class="QVBoxLayout" name="verticalLayout_2"/>
     </widget>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

widget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>870</width>
    <height>587</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_6">
   <item>
    <widget class="QWidget" name="widget_7" native="true">
     <layout class="QVBoxLayout" name="verticalLayout_5">
      <property name="leftMargin">
       <number>0</number>
      </property>
      <property name="topMargin">
       <number>0</number>
      </property>
      <property name="rightMargin">
       <number>0</number>
      </property>
      <property name="bottomMargin">
       <number>0</number>
      </property>
      <item>
       <widget class="QWidget" name="widget_5" native="true">
        <layout class="QHBoxLayout" name="horizontalLayout_2">
         <item>
          <widget class="QWidget" name="widget_2" native="true">
           <layout class="QVBoxLayout" name="verticalLayout_3">
            <property name="leftMargin">
             <number>0</number>
            </property>
            <property name="topMargin">
             <number>0</number>
            </property>
            <property name="rightMargin">
             <number>0</number>
            </property>
            <property name="bottomMargin">
             <number>0</number>
            </property>
            <item>
             <widget class="QWidget" name="widget" native="true">
              <layout class="QVBoxLayout" name="verticalLayout">
               <property name="leftMargin">
                <number>0</number>
               </property>
               <property name="topMargin">
                <number>0</number>
               </property>
               <property name="rightMargin">
                <number>0</number>
               </property>
               <property name="bottomMargin">
                <number>0</number>
               </property>
               <item>
                <widget class="QTextBrowser" name="msgBrowser"/>
               </item>
              </layout>
             </widget>
            </item>
            <item>
             <widget class="QFrame" name="frame_2">
              <property name="frameShape">
               <enum>QFrame::Box</enum>
              </property>
              <layout class="QHBoxLayout" name="horizontalLayout">
               <item>
                <widget class="QFontComboBox" name="fontCBx"/>
               </item>
               <item>
                <widget class="QComboBox" name="sizeBox">
                 <property name="currentIndex">
                  <number>3</number>
                 </property>
                 <item>
                  <property name="text">
                   <string>8</string>
                  </property>
                 </item>
                 <item>
                  <property name="text">
                   <string>9</string>
                  </property>
                 </item>
                 <item>
                  <property name="text">
                   <string>11</string>
                  </property>
                 </item>
                 <item>
                  <property name="text">
                   <string>12</string>
                  </property>
                 </item>
                 <item>
                  <property name="text">
                   <string>13</string>
                  </property>
                 </item>
                 <item>
                  <property name="text">
                   <string>14</string>
                  </property>
                 </item>
                </widget>
               </item>
               <item>
                <widget class="QToolButton" name="BoldTbtn">
                 <property name="toolTip">
                  <string>加粗</string>
                 </property>
                 <property name="text">
                  <string>加粗</string>
                 </property>
                 <property name="iconSize">
                  <size>
                   <width>33</width>
                   <height>33</height>
                  </size>
                 </property>
                 <property name="checkable">
                  <bool>true</bool>
                 </property>
                </widget>
               </item>
               <item>
                <widget class="QToolButton" name="italicTbtn">
                 <property name="toolTip">
                  <string>倾斜</string>
                 </property>
                 <property name="text">
                  <string>倾斜</string>
                 </property>
                 <property name="iconSize">
                  <size>
                   <width>33</width>
                   <height>32</height>
                  </size>
                 </property>
                 <property name="checkable">
                  <bool>true</bool>
                 </property>
                </widget>
               </item>
               <item>
                <widget class="QToolButton" name="UnderlineTBtn">
                 <property name="toolTip">
                  <string>下滑线</string>
                 </property>
                 <property name="text">
                  <string>下滑线</string>
                 </property>
                 <property name="iconSize">
                  <size>
                   <width>33</width>
                   <height>33</height>
                  </size>
                 </property>
                 <property name="checkable">
                  <bool>true</bool>
                 </property>
                </widget>
               </item>
               <item>
                <widget class="QToolButton" name="colourbtn">
                 <property name="toolTip">
                  <string>颜色</string>
                 </property>
                 <property name="text">
                  <string>颜色</string>
                 </property>
                 <property name="iconSize">
                  <size>
                   <width>33</width>
                   <height>33</height>
                  </size>
                 </property>
                 <property name="checkable">
                  <bool>true</bool>
                 </property>
                </widget>
               </item>
               <item>
                <widget class="QToolButton" name="savebtn">
                 <property name="toolTip">
                  <string>保存</string>
                 </property>
                 <property name="text">
                  <string>保存</string>
                 </property>
                 <property name="iconSize">
                  <size>
                   <width>33</width>
                   <height>33</height>
                  </size>
                 </property>
                 <property name="checkable">
                  <bool>true</bool>
                 </property>
                </widget>
               </item>
               <item>
                <widget class="QToolButton" name="deletebtn">
                 <property name="toolTip">
                  <string>删除</string>
                 </property>
                 <property name="text">
                  <string>删除</string>
                 </property>
                 <property name="iconSize">
                  <size>
                   <width>33</width>
                   <height>33</height>
                  </size>
                 </property>
                 <property name="checkable">
                  <bool>true</bool>
                 </property>
                </widget>
               </item>
              </layout>
             </widget>
            </item>
            <item>
             <widget class="QWidget" name="widget_3" native="true">
              <layout class="QVBoxLayout" name="verticalLayout_2">
               <item>
                <widget class="QTextEdit" name="msgtextEdit"/>
               </item>
              </layout>
             </widget>
            </item>
           </layout>
          </widget>
         </item>
         <item>
          <widget class="QWidget" name="widget_4" native="true">
           <layout class="QVBoxLayout" name="verticalLayout_4">
            <item>
             <widget class="QTableWidget" name="usertableWidget">
              <property name="selectionMode">
               <enum>QAbstractItemView::SingleSelection</enum>
              </property>
              <property name="selectionBehavior">
               <enum>QAbstractItemView::SelectRows</enum>
              </property>
              <column>
               <property name="text">
                <string>用户名</string>
               </property>
              </column>
             </widget>
            </item>
           </layout>
          </widget>
         </item>
        </layout>
       </widget>
      </item>
      <item>
       <widget class="QFrame" name="frame_6">
        <layout class="QHBoxLayout" name="horizontalLayout_3">
         <item>
          <spacer name="horizontalSpacer">
           <property name="orientation">
            <enum>Qt::Horizontal</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>40</width>
             <height>20</height>
            </size>
           </property>
          </spacer>
         </item>
         <item>
          <widget class="QPushButton" name="sendButton">
           <property name="text">
            <string>发送</string>
           </property>
          </widget>
         </item>
         <item>
          <spacer name="horizontalSpacer_2">
           <property name="orientation">
            <enum>Qt::Horizontal</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>40</width>
             <height>20</height>
            </size>
           </property>
          </spacer>
         </item>
         <item>
          <widget class="QLabel" name="usernum">
           <property name="text">
            <string>在线用户量</string>
           </property>
          </widget>
         </item>
         <item>
          <spacer name="horizontalSpacer_3">
           <property name="orientation">
            <enum>Qt::Horizontal</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>40</width>
             <height>20</height>
            </size>
           </property>
          </spacer>
         </item>
         <item>
          <widget class="QPushButton" name="exitbtn">
           <property name="text">
            <string>退出</string>
           </property>
          </widget>
         </item>
         <item>
          <spacer name="horizontalSpacer_4">
           <property name="orientation">
            <enum>Qt::Horizontal</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>40</width>
             <height>20</height>
            </size>
           </property>
          </spacer>
         </item>
        </layout>
       </widget>
      </item>
     </layout>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

main.cpp

#include "widget.h"
#include"form.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    Widget w;
//    w.show();
    Form list;
    list.show();
    return a.exec();
}

编程思路
对话框列表
1.1 Toolbox群成员
1.2内部 做出一个垂直布局
1.3添加按钮
1.3.1图标
1.3.2文字
1.3.3图标大小
1.3.4风格
1.3.5文字图标都显示
1.3.6按钮保存到QVector
2.界面布局
3.点击按钮弹出窗口
3.1点击按钮弹出窗口
3.1.1Widget*widget=new Widget(0,vToolBtn[i],->text);
3.1.2修改Widget聊天窗口 构造函数 为两个参数
3.1.3设置聊天窗口的图标,标题
3.1.4show函数显示窗口
3.2
3.2.1加入bool的isShow标示
3.2.2判断标示 如果为true就不要多次打开
3.2.3聊天窗口时发生,自定义关闭信号
3.2.4监听关闭信号 更新isShow标示
4.udp通信
4.1 UdpSocket pro network
4.2 new出套接字
4.3绑定端口
4.4书写报文writeDatagram
4.5 ReadyRead监听信号
4.6读取报文
4.7报文长度
4.8 同步聊天记录
5.核心聊天功能
5.1通信套接字
5.2bind(端口,共享地址|断线重连)
5.3获取用户名
5.4sendMsg(枚举(普通聊天,用户进入,离开))
5.5发送3段数据 类型,用户名,具体内容
5.6书写报文 广播发送
5.7 接受利用数据流 做分段
5.7.1第一段 类型
5.7.2 把用户发送的类型追加到聊天记录里
6.新用户进入
6.1提供处理新用户进入函数
6.2更新右侧TableWidget
6.3更新聊天记录
6.4更新在线用户数量
7用户离开
7.1提供用户处理函数
7.2更新右侧tableWidget
7.3更新聊天记录
7.4更新在线用户人数
7.5再closeEvent事件中发送用户离开消息
7.6端口套接字
7.7离开按钮处理
8辅助功能
8.1字体设置
8.2字号设置
8.3加粗
8.4倾斜
8.5下划线
8.6清空聊天记录
8.7保存聊天记录(QIODevice::Text)
# 本案例参考B站QT聊天案例教程:https://www.bilibili.com/video/BV1SP4y1W7ST?spm_id_from=333.851.dynamic.content.click
欢迎关注技术公众号,获取更多硬件学习干货!
在这里插入图片描述
我们能为你提供什么?
技术辅导:C++、Java、嵌入式软件/硬件
项目辅导:软件/硬件项目、大厂实训项目
就业辅导:就业全流程辅导、技术创业支持
对接企业HR:培养输送优质性人才

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值