五、Qt综合项目-局域网聊天室#

1.项目介绍#

最后我们可以使用Qt做一个局域网聊天室

好友列表页面如下:

首页展示好友列表,点击好友进入聊天界面

聊天界面如下:

聊天界面可以显示展示进入局域网好友列表

有新用户进入,提示新用户进入

用户离开,提示用户离开

发送消息可以发送给所有在线的用户

2. 主界面#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    //设置大小
    setFixedSize(250, 700);
    //设置标题
    setWindowTitle("QQ");
    //设置图标
    setWindowIcon(QIcon(":/images/qq.png"));
    //初始化界面
    initUI();
}

MainWindow::~MainWindow() {

}

//初始化界面
void MainWindow::initUI() {
    auto *tb = new QToolBox;
    auto *gb = new QGroupBox;
    auto *vl = new QVBoxLayout;
    //添加条目
    tb->addItem(gb, "联系人");
    gb->setLayout(vl);
    //添加中心控件
    setCentralWidget(tb);
    //创建联系人条目
    createContacts(vl);
    //每个窗口是否显示
    for (int i = 0; i < btns.size(); ++i) {
        isShows.append(false);
    }
    //处理每一个按钮的事件
    contactsClick();
}

//创建联系人条目
void MainWindow::createContacts(QVBoxLayout *const &layout) {
    QList<QString> nameList{"Maker", "水票奇缘", "忆梦如澜", "北京出版人", "Cherry", "淡然", "娇娇girl", "落水无痕",
                            "青墨暖暖"};
    QStringList iconList{"qt", "spqy", "ymrl", "qq", "Cherry", "dr", "jj", "lswh", "qmnn"};
    for (int i = 0; i < nameList.size(); ++i) {
        //创建按钮
        auto *btn = new QToolButton();
        //添加文字
        btn->setText(nameList[i]);
        //添加图片
        auto *icon = new QPixmap(QString(":/images/%1.png").arg(iconList[i]));
        btn->setIcon(*icon);

        //设置样式
        btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        //设置按钮大小
        btn->resize(210, 70);
        //按钮图片大小
        btn->setIconSize(icon->size());
        //设置透明度
        btn->setAutoRaise(true);
        //添加到布局中
        layout->addWidget(btn);
        //添加到按钮的列表中
        btns.append(btn);
    }
}

//处理每一个按钮的点击事件
void MainWindow::contactsClick() {
    for (int i = 0; i < btns.size(); ++i) {
        QToolButton *btn = btns[i];
        connect(btn, &QToolButton::clicked, [=]() {
            if (!isShows[i]) {
                //创建会话窗口
                auto *w = new ConversationWindow(btn->text());
                //设置标题和图标
                w->setWindowTitle(btn->text());
                w->setWindowIcon(btn->icon());
                //设置显示属性
                w->setAttribute(Qt::WA_DeleteOnClose);
                w->show();
                //改变显示标记
                isShows[i] = true;
                //关闭的回调
                connect(w,&ConversationWindow::conversationClose,[=](){
                    isShows[i] = false;
                });
            }
        });
    }
}

3. 聊天界面#

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "ConversationWindow.h"

ConversationWindow::ConversationWindow(QString name, QWidget *parent) : QMainWindow(parent), name(name) {
    //设置界面大小
    this->resize(740, 450);
    //初始化界面
    initUI();
    //按钮的点击事件
    btnsClick();
    //信息相关的操作
    socketOperator();
}

ConversationWindow::~ConversationWindow() {

}

//初始化界面
void ConversationWindow::initUI() {
    //整体竖直布局
    QVBoxLayout *wholeLayout = new QVBoxLayout;
    //左侧竖直布局
    QVBoxLayout *leftLayout = new QVBoxLayout;
    //右侧TableWidget
    tw = new QTableWidget(0, 1);
    tw->setMaximumWidth(180);

    tw->setHorizontalHeaderLabels(QStringList{"用户名"});

    //上部布局
    QHBoxLayout *upLayout = new QHBoxLayout;
    //下部布局
    QHBoxLayout *downLayout = new QHBoxLayout;
    //发送按钮
    sendBtn = new QPushButton;
    sendBtn->setText("发送");
    //在线人数
    label = new QLabel;
    label->setText("在线人数:1");
    //退出按钮
    exitBtn = new QPushButton;
    exitBtn->setText("退出");


    //左侧
    //1.消息显示框
    msgEdit = new QTextEdit;
    msgEdit->setReadOnly(true);
    //2.水平布局(字体等)
    QHBoxLayout *iconLayout = new QHBoxLayout;
    //3.发送消息输入框
    sendEdit = new QTextEdit;

    //图标
    //1.宋体
    fontBox = new QFontComboBox;
    //2.字体大小
    fontSizeBox = new QComboBox;
    //3.字体加粗
    blod = new QToolButton;
    //4.斜体
    italic = new QToolButton;
    //5.下划线
    underLine = new QToolButton;
    //6.更改字体颜色
    fontColor = new QToolButton;
    //7.保存聊天记录
    saveConversation = new QToolButton;
    //8.清空聊天记录
    clearConversation = new QToolButton;

    //设置加粗、斜体和下划线可以选中
    blod->setCheckable(true);
    italic->setCheckable(true);
    underLine->setCheckable(true);

    //添加图标布局
    iconLayout->addWidget(fontBox);
    iconLayout->addWidget(fontSizeBox);
    iconLayout->addWidget(blod);
    iconLayout->addWidget(italic);
    iconLayout->addWidget(underLine);
    iconLayout->addWidget(fontColor);
    iconLayout->addWidget(saveConversation);
    iconLayout->addWidget(clearConversation);

    //添加左侧
    leftLayout->addWidget(msgEdit);
    leftLayout->addLayout(iconLayout);
    leftLayout->addWidget(sendEdit);

    //上部布局
    upLayout->addLayout(leftLayout);
    upLayout->addWidget(tw);
    //下部布局
    downLayout->addStretch(1);
    downLayout->addWidget(sendBtn);
    downLayout->addStretch(1);
    downLayout->addWidget(label);
    downLayout->addStretch(1);
    downLayout->addWidget(exitBtn);
    downLayout->addStretch(1);

    //整体布局添加
    wholeLayout->addLayout(upLayout);
    wholeLayout->addLayout(downLayout);

    //设置中心控件
    QWidget *w = new QWidget;
    w->setLayout(wholeLayout);
    setCentralWidget(w);


    //处理细节
    //1.字体大小
    fontSizeBox->addItem("12");
    fontSizeBox->addItem("13");
    fontSizeBox->addItem("14");
    fontSizeBox->addItem("15");
    //2.图标
    blod->setIconSize(QSize(26, 26));
    italic->setIconSize(QSize(26, 26));
    underLine->setIconSize(QSize(26, 26));
    fontColor->setIconSize(QSize(26, 26));
    saveConversation->setIconSize(QSize(26, 26));
    clearConversation->setIconSize(QSize(26, 26));
    //加粗
    blod->setIcon(QPixmap(":/images/bold.png"));
    italic->setIcon(QPixmap(":/images/italic.png"));
    underLine->setIcon(QPixmap(":/images/under.png"));
    fontColor->setIcon(QPixmap(":/images/color.png"));
    saveConversation->setIcon(QPixmap(":/images/save.png"));
    clearConversation->setIcon(QPixmap(":/images/clear.png"));
}

//按钮信号和槽函数
void ConversationWindow::btnsClick() {
    //字体设置
    connect(fontBox, &QFontComboBox::currentFontChanged, [=](const QFont &font) {
        sendEdit->setFont(font);
    });
    //字体大小设置
    void (QComboBox::*currentIndexChanged)(const QString &text) = &QComboBox::currentIndexChanged;
    connect(fontSizeBox, currentIndexChanged, [=](const QString &text) {
        sendEdit->setFontPointSize(text.toDouble());
    });
    //字体加粗
    connect(blod, &QToolButton::clicked, [=](bool checked = false) {
        if (checked) {
            sendEdit->setFontWeight(QFont::Bold);
        } else {
            sendEdit->setFontWeight(QFont::Normal);
        }
    });
    //斜体
    connect(italic, &QToolButton::clicked, [=](bool checked = false) {
        if (checked) {
            sendEdit->setFontItalic(true);
        } else {
            sendEdit->setFontItalic(false);
        }
    });
    //下划线
    connect(underLine, &QToolButton::clicked, [=](bool checked = false) {
        if (checked) {
            sendEdit->setFontUnderline(true);
        } else {
            sendEdit->setFontUnderline(false);
        }
    });
    //选择颜色
    connect(fontColor, &QPushButton::clicked, [=]() {
        QColor color = QColorDialog::getColor();
        sendEdit->setTextColor(color);
    });
    //保存聊天记录
    connect(saveConversation, &QToolButton::clicked, [=]() {
        //选择保存的文件名
        QString path = QFileDialog::getSaveFileName(this, "请选择保存的文件名", "/home/wt/Downloads", "TXT(*.txt*)");
        if (path.isEmpty())
            return;
        //创建文件
        QFile f(path);
        f.open(QIODevice::WriteOnly);
        QString msg = msgEdit->toPlainText();
        f.write(msg.toUtf8().data());
        f.close();
    });
    //清空聊天记录
    connect(clearConversation, &QToolButton::clicked, [=]() {
       msgEdit->clear();
    });
}

//信息相关的操作
void ConversationWindow::socketOperator() {
    socket = new QUdpSocket(this);
    socket->bind(6666, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);

    //发送消息
    connect(sendBtn, &QPushButton::clicked, [=]() {
        sendMsg(MSGTYPE::MSG);
    });

    //接收消息
    connect(socket, &QUdpSocket::readyRead, [=]() {
        receiveMsg();
    });

    //上线通知
    sendMsg(MSGTYPE::ENTER);
    //下线通知
    connect(exitBtn, &QPushButton::clicked, [=]() {
        //退出窗口
        close();
    });
}

//发送消息
void ConversationWindow::sendMsg(MSGTYPE type) {
    //要发送的消息
    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);
    //先写入消息类型和用户名
    out << type << name;
    if (type == MSGTYPE::MSG) {
        QString msg = sendEdit->toPlainText();
        out << msg;
    }
    //发送消息
    socket->writeDatagram(data, QHostAddress::Broadcast, 6666);
}

//接收消息
void ConversationWindow::receiveMsg() {
    qDebug() << "接收到消息:" << name << endl;
    QByteArray data;
    data.resize(socket->pendingDatagramSize());
    socket->readDatagram(data.data(), data.length());
    QDataStream ds(&data, QIODevice::ReadOnly);
    //读取消息类型
    int type;
    ds >> type;
    //用户名或者信息
    QString name, msg;
    ds >> name;
    //消息时间
    QString time = QDateTime().currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    //判断消息类型
    switch (type) {
        case MSGTYPE::MSG:
            ds >> msg;
            normalMsg(name, msg, time);
            break;
        case MSGTYPE::ENTER:
            enterMsg(name);
            break;
        case MSGTYPE::EXIT:
            exitMsg(name, time);
            break;
    }
}

//普通消息
void ConversationWindow::normalMsg(QString name, QString msg, QString time) {
    //设置颜色
    msgEdit->setTextColor(Qt::blue);
    msgEdit->append(QString("[%1]%2").arg(name).arg(time));
    msgEdit->append(msg);
    //清空输入框
    sendEdit->clear();
}

//进入消息
void ConversationWindow::enterMsg(QString name) {
    //是否有这个人
    if (tw->findItems(name, Qt::MatchExactly).isEmpty()) {
        //消息显示
        msgEdit->setTextColor(Qt::green);
        msgEdit->append(QString("%1 在线").arg(name));
        //列表显示
        QTableWidgetItem *item = new QTableWidgetItem(name);
        tw->insertRow(0);
        tw->setItem(0, 0, item);

        //更新在线人数
        label->setText(QString("在线人数:%1").arg(tw->rowCount()));

        //发送自己的上线信息给别人
        sendMsg(MSGTYPE::ENTER);
    }
}

//退出消息
void ConversationWindow::exitMsg(QString name, QString time) {
    //是否有这个人
    if (!tw->findItems(name, Qt::MatchExactly).isEmpty()) {
        //消息显示
        msgEdit->setTextColor(Qt::green);
        msgEdit->append(QString("%1 于%2 离开").arg(name).arg(time));
        //列表显示
        int row = tw->findItems(name, Qt::MatchExactly).first()->row();
        tw->removeRow(row);

        //更新在线人数
        label->setText(QString("在线人数:%1").arg(tw->rowCount()));
    }
}

//关闭事件
void ConversationWindow::closeEvent(QCloseEvent *event) {
    //退出
    sendMsg(MSGTYPE::EXIT);
    emit conversationClose();
}