Qt實戰:基于Qt的聊天室——云曦Chatting

      網友投稿 1306 2025-04-01

      Qt實戰:云曦聊天室篇

      Qt實戰:基于Qt的聊天室——云曦Chatting

      前言

      基于Qt的網絡聊天室,可進行群聊,私聊,添加好友,創建群聊,添加群聊等功能

      一、云曦聊天室效果圖

      1. 登錄界面:

      如圖1所示,輸入用戶名和密碼,點擊登錄,即可進入如圖3所示的好友列表界面。

      2. 注冊界面:

      如圖2所示,輸入用戶名和密碼,點擊立即注冊按鈕,即可進行相關的注冊功能,注冊成功后,點擊登錄,即可進入如圖3所示的好友列表界面。

      3. 好友列表界面:

      如圖3所示,即為好友列表界面,用戶可在此進行私聊,群聊,好友的添加,創建群聊,加入群聊等功能。

      4. 好友上線提醒:

      如圖4所示,用戶可在圖3界面看到用戶的相關好友的上下線提醒通知,以及關閉圖3后,在左下角同QQ的托盤效果。

      5. 私聊界面:

      如圖5所示,用戶可選擇任意一個好友,進行相關的私聊,其中,包括字體的樣式、大小、加粗、傾斜、以及聊天記錄的保存、清空等功能。

      6. 私聊界面:

      如圖6所示,用戶可在此界面進行群聊。

      二、相關源代碼

      項目框架圖:

      1. .cpp部分

      privatechat:

      #include "privatechat.h" #include "ui_privatechat.h" #include #include #include #include #include #include PrivateChat::PrivateChat(QTcpSocket *s, QString u, QString f, Chatlist *c, QList *l, QWidget *parent) : QWidget(parent), ui(new Ui::PrivateChat) { ui->setupUi(this); socket = s; userName = u; friendName = f; mainWidget = c; chatWidgetList = l; connect(mainWidget, &Chatlist::signal_to_sub_widget, this, &PrivateChat::show_text_slot); connect(ui->exitBtn, &QPushButton::clicked,this, &GroupChat::close); setWindowIcon(QPixmap(":images//C3.png")); QLabel * bglabel = new QLabel; bglabel=ui ->label; bglabel ->setPixmap(QPixmap(":images//底框.png")); bglabel ->setScaledContents(true); QLabel * bglabel1 = new QLabel; bglabel1=ui ->label_2; bglabel1 ->setPixmap(QPixmap(":images//主導航框.png")); bglabel1->setScaledContents(true); //控件透明化 ui->textEdit->setStyleSheet("background-color:rgba(0,0,0,0);"); ui->listWidget->setStyleSheet("background-color:rgba(0,0,0,0);"); ui->textBrowser->setStyleSheet("background-color:rgba(0,0,0,0);"); ui->widget_5->setStyleSheet("background-color:rgba(0,0,0,0);"); //窗口去邊框 this -> setWindowFlags(Qt::FramelessWindowHint); ui->pushButton->setStyleSheet( //正常狀態樣式 "QPushButton{" "border-radius:5px;"http://設置圓角半徑 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->fileButton->setStyleSheet( //正常狀態樣式 "QPushButton{" "border-radius:5px;"http://設置圓角半徑 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->exitBtn->setStyleSheet( //正常狀態樣式 "QPushButton{" //"background-color:#67a6f8;"http://設置按鈕背景色 "border-radius:5px;"http://設置圓角半徑 "color:white;"http://設置按鈕字體顏色 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->exitBtn_3->setStyleSheet( //正常狀態樣式 "QPushButton{" //"background-color:#67a6f8;"http://設置按鈕背景色 "border-radius:5px;"http://設置圓角半徑 "color:white;"http://設置按鈕字體顏色 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->exitBtn_9->setStyleSheet( //正常狀態樣式 "QPushButton{" //"background-color:#67a6f8;"http://設置按鈕背景色 "border-radius:5px;"http://設置圓角半徑 "color:white;"http://設置按鈕字體顏色 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->exitBtn_5->setStyleSheet( //正常狀態樣式 "QPushButton{" //"background-color:#67a6f8;"http://設置按鈕背景色 "border-radius:5px;"http://設置圓角半徑 "color:white;"http://設置按鈕字體顏色 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->exitBtn_6->setStyleSheet( //正常狀態樣式 "QPushButton{" //"background-color:#67a6f8;"http://設置按鈕背景色 "border-radius:5px;"http://設置圓角半徑 "color:white;"http://設置按鈕字體顏色 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->exitBtn_7->setStyleSheet( //正常狀態樣式 "QPushButton{" //"background-color:#67a6f8;"http://設置按鈕背景色 "border-radius:5px;"http://設置圓角半徑 "color:white;"http://設置按鈕字體顏色 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->exitBtn_8->setStyleSheet( //正常狀態樣式 "QPushButton{" //"background-color:#67a6f8;"http://設置按鈕背景色 "border-radius:5px;"http://設置圓角半徑 "color:white;"http://設置按鈕字體顏色 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->pushButton_2->setStyleSheet( //正常狀態樣式 "QPushButton{" "border-radius:5px;"http://設置圓角半徑 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->pushButton_3->setStyleSheet( //正常狀態樣式 "QPushButton{" "border-radius:5px;"http://設置圓角半徑 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->pushButton_4->setStyleSheet( //正常狀態樣式 "QPushButton{" "border-radius:5px;"http://設置圓角半徑 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->pushButton_5->setStyleSheet( //正常狀態樣式 "QPushButton{" "border-radius:5px;"http://設置圓角半徑 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->pushButton_6->setStyleSheet( //正常狀態樣式 "QPushButton{" "border-radius:5px;"http://設置圓角半徑 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); ui->sendButton->setStyleSheet( //正常狀態樣式 "QPushButton{" "background-color:#8D8D8D;"http://設置按鈕背景色 "border-radius:15px;"http://設置圓角半徑 "color:white;"http://設置按鈕字體顏色 "}" "QPushButton:hover{" "background-color:#5f9ae6;" "color:white;" "}"); //////////////////////////////////////輔助功能////////////////////////////////////////// //字體 connect(ui->fontComboBox,&QFontComboBox::currentFontChanged,this,[=](const QFont &font){ ui->textEdit->setCurrentFont(font); ui->textEdit->setFocus(); }); //字號 void (QComboBox:: * cbxSingal)(const QString &text) = &QComboBox::currentIndexChanged; connect(ui->comboBox,cbxSingal,this,[=](const QString &text){ ui->textEdit->setFontPointSize(text.toDouble()); ui->textEdit->setFocus(); }); //加粗 connect(ui->pushButton_3,&QToolButton::clicked,this,[=](bool checked){ if(checked) { ui->textEdit->setFontWeight(QFont::Bold); } else { ui->textEdit->setFontWeight(QFont::Normal); } ui->textEdit->setFocus(); }); //傾斜 connect(ui->pushButton_4,&QToolButton::clicked,this,[=](bool checked){ ui->textEdit->setFontItalic(checked); ui->textEdit->setFocus(); }); //下劃線 connect(ui->pushButton_5,&QToolButton::clicked,this,[=](bool checked){ ui->textEdit->setFontUnderline(checked); ui->textEdit->setFocus(); }); //設置文本顏色 connect(ui->pushButton_6,&QToolButton::clicked,[=](){ QColor color = QColorDialog::getColor(Qt::red); //color對象可以在widget.h中定義私有成員 ui->textEdit->setTextColor(color); }); //保存聊天記錄 connect(ui->pushButton,&QToolButton::clicked,[=](){ if( ui->textBrowser->document()->isEmpty()) { QMessageBox::warning(this,"警告","內容不能為空"); return; } else { QString path = QFileDialog::getSaveFileName(this,"保存聊天記錄","聊天記錄","(*.txt)"); QFile file(path); //打開模式加換行操作 file.open(QIODevice::WriteOnly | QFile::Text); QTextStream stream(&file); stream << ui->textBrowser->toPlainText(); file.close(); } }); //清空聊天記錄 connect(ui->pushButton_2,&QToolButton::clicked,[=](){ ui->textBrowser->clear(); }); } PrivateChat::~PrivateChat() { delete ui; } void PrivateChat::on_sendButton_clicked() { QString text = ui->textEdit->toPlainText(); QJsonObject obj; obj.insert("cmd", "private_chat"); obj.insert("user_from", userName); obj.insert("user_to", friendName); obj.insert("text", text); QByteArray ba = QJsonDocument(obj).toJson(); socket->write(ba); ui->textEdit->clear(); ui->textBrowser->append(text); ui->textBrowser->append("\n"); } void PrivateChat::show_text_slot(QJsonObject obj) { if (obj.value("cmd").toString() == "private_chat") { if (obj.value("user_from").toString() == friendName) { if (this->isMinimized()) { this->showNormal(); } this->activateWindow(); ui->textBrowser->append(obj.value("text").toString()); ui->textBrowser->append("\n"); } } } void PrivateChat::closeEvent(QCloseEvent *event) { for (int i = 0; i < chatWidgetList->size(); i++) { if (chatWidgetList->at(i).name == friendName) { chatWidgetList->removeAt(i); break; } } event->accept(); } void PrivateChat::on_fileButton_clicked() { QString fileName = QFileDialog::getOpenFileName(this, "發送文件", QCoreApplication::applicationFilePath()); if (fileName.isEmpty()) { QMessageBox::warning(this, "發送文件提示", "請選擇一個文件"); } else { QFile file(fileName); file.open(QIODevice::ReadOnly); QJsonObject obj; obj.insert("cmd", "send_file"); obj.insert("from_user", userName); obj.insert("to_user", friendName); obj.insert("length", file.size()); obj.insert("filename", fileName); QByteArray ba = QJsonDocument(obj).toJson(); socket->write(ba); } }

      2. .h部分

      privatechat:

      #ifndef PRIVATECHAT_H #define PRIVATECHAT_H #include #include #include #include #include #include #include #include class Chatlist; struct ChatWidgetInfo; #include "chatlist.h" namespace Ui { class PrivateChat; } class PrivateChat : public QWidget { Q_OBJECT public: explicit PrivateChat(QTcpSocket *s, QString u, QString f, Chatlist *c, QList *l, QWidget *parent = 0); ~PrivateChat(); void closeEvent(QCloseEvent *event); private slots: void on_sendButton_clicked(); void show_text_slot(QJsonObject); void on_fileButton_clicked(); private: Ui::PrivateChat *ui; QTcpSocket *socket; QString userName; QString friendName; Chatlist *mainWidget; QList *chatWidgetList; QPoint label; }; #endif // PRIVATECHAT_H

      總結一

      以上是云曦聊天室(YXChatting)的相關簡介和私聊部分代碼,詳細代碼與相關講解會后續逐步更新,如有問題,也可直接私聊或在下方評論,技術萌新,初來乍到,如有不妥之處,還請各位大佬進行指正與諒解。

      Qt

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:WPS表格辦公DGET函數的用法(wps表格的功能介紹)
      下一篇:WPS文檔中有一個包含許多數據列的表 如何快速計算有多少行和列(wps中一列表格添加相同的內容)
      相關文章
      亚洲小说图区综合在线| 亚洲国产AV无码专区亚洲AV| 久久亚洲AV无码精品色午夜麻| 国产精品无码亚洲精品2021| 亚洲中文字幕一二三四区| 亚洲午夜久久久久久尤物| 亚洲欧洲国产日韩精品| 亚洲va在线va天堂va888www| 欧洲亚洲国产清在高| 久久国产亚洲精品麻豆| 国产成人无码综合亚洲日韩| 亚洲va久久久噜噜噜久久男同| 亚洲精品无码不卡在线播HE| 亚洲精品午夜无码专区| 亚洲成A人片777777| 久久久久亚洲av无码专区蜜芽| 亚洲Aⅴ无码专区在线观看q| 亚洲图片在线观看| 久久久亚洲欧洲日产国码二区| 亚洲高清中文字幕综合网| 亚洲精品在线播放| 亚洲另类春色校园小说| 亚洲综合一区无码精品| 亚洲精品第一国产综合亚AV| 爱情岛论坛亚洲品质自拍视频网站| 久久亚洲中文无码咪咪爱| 天堂亚洲免费视频| 亚洲午夜精品一级在线播放放| 色噜噜亚洲精品中文字幕| 亚洲国产精品乱码一区二区| 亚洲人成在线影院| 亚洲区视频在线观看| 亚洲中文字幕无码av永久| 亚洲av成人一区二区三区观看在线 | 国产亚洲精品无码成人| 亚洲AV无码精品色午夜在线观看| 亚洲VA中文字幕不卡无码| 亚洲图片中文字幕| 亚洲无码一区二区三区| 午夜亚洲乱码伦小说区69堂| 国产亚洲精久久久久久无码AV|