当前位置:网站首页>QT actual combat: Yunxi chat room
QT actual combat: Yunxi chat room
2022-04-23 14:28:00 【Spiritual health】
Qt actual combat : Yunxi chat room
List of articles
Preface
be based on Qt Internet chat room , Group chat available , The private chat , Add buddy , Create group chat , Add group chat and other functions
One 、 Renderings of Yunxi chat room
1. Login screen :
Pictured 1 Shown , Enter your username and password , Click login , You can enter as shown in the figure 3 The friends list interface shown in .

2. The registration screen :
Pictured 2 Shown , Enter your username and password , Click the register now button , Relevant registration functions can be performed , After successful registration , Click login , You can enter as shown in the figure 3 The friends list interface shown in .

3. Friends list interface :
Pictured 3 Shown , This is the friend list interface , Users can have private chat here , Group chat , Add friends , Create group chat , Join group chat and other functions .

4. Friends online reminder :
Pictured 4 Shown , The user can see in the figure 3 The user's related friends' online and offline reminder notice can be seen on the interface , And close the diagram 3 after , Same as in the lower left corner QQ Tray effect .

5. Private chat interface :
Pictured 5 Shown , The user can select any friend , Conduct relevant private chat , among , Include styles for fonts 、 size 、 In bold 、 tilt 、 And the preservation of chat records 、 Functions such as emptying .

6. Private chat interface :
Pictured 6 Shown , Users can conduct group chat in this interface .

Two 、 Related source code
Project framework :

1. .cpp part
privatechat:
#include "privatechat.h"
#include "ui_privatechat.h"
#include <QLabel>
#include <QPushButton>
#include <QDesktopWidget>
#include <QWidget>
#include <QToolButton>
#include <QColorDialog>
PrivateChat::PrivateChat(QTcpSocket *s, QString u, QString f, Chatlist *c, QList<ChatWidgetInfo> *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// Bottom frame .png"));
bglabel ->setScaledContents(true);
QLabel * bglabel1 = new QLabel;
bglabel1=ui ->label_2;
bglabel1 ->setPixmap(QPixmap(":images// Main navigation frame .png"));
bglabel1->setScaledContents(true);
// Control transparency
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);");
// Window border removal
this -> setWindowFlags(Qt::FramelessWindowHint);
ui->pushButton->setStyleSheet(
// Normal state style
"QPushButton{"
"border-radius:5px;"// Set the fillet radius
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->fileButton->setStyleSheet(
// Normal state style
"QPushButton{"
"border-radius:5px;"// Set the fillet radius
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->exitBtn->setStyleSheet(
// Normal state style
"QPushButton{"
//"background-color:#67a6f8;"// Set the button background color
"border-radius:5px;"// Set the fillet radius
"color:white;"// Set button font color
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->exitBtn_3->setStyleSheet(
// Normal state style
"QPushButton{"
//"background-color:#67a6f8;"// Set the button background color
"border-radius:5px;"// Set the fillet radius
"color:white;"// Set button font color
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->exitBtn_9->setStyleSheet(
// Normal state style
"QPushButton{"
//"background-color:#67a6f8;"// Set the button background color
"border-radius:5px;"// Set the fillet radius
"color:white;"// Set button font color
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->exitBtn_5->setStyleSheet(
// Normal state style
"QPushButton{"
//"background-color:#67a6f8;"// Set the button background color
"border-radius:5px;"// Set the fillet radius
"color:white;"// Set button font color
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->exitBtn_6->setStyleSheet(
// Normal state style
"QPushButton{"
//"background-color:#67a6f8;"// Set the button background color
"border-radius:5px;"// Set the fillet radius
"color:white;"// Set button font color
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->exitBtn_7->setStyleSheet(
// Normal state style
"QPushButton{"
//"background-color:#67a6f8;"// Set the button background color
"border-radius:5px;"// Set the fillet radius
"color:white;"// Set button font color
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->exitBtn_8->setStyleSheet(
// Normal state style
"QPushButton{"
//"background-color:#67a6f8;"// Set the button background color
"border-radius:5px;"// Set the fillet radius
"color:white;"// Set button font color
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->pushButton_2->setStyleSheet(
// Normal state style
"QPushButton{"
"border-radius:5px;"// Set the fillet radius
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->pushButton_3->setStyleSheet(
// Normal state style
"QPushButton{"
"border-radius:5px;"// Set the fillet radius
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->pushButton_4->setStyleSheet(
// Normal state style
"QPushButton{"
"border-radius:5px;"// Set the fillet radius
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->pushButton_5->setStyleSheet(
// Normal state style
"QPushButton{"
"border-radius:5px;"// Set the fillet radius
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->pushButton_6->setStyleSheet(
// Normal state style
"QPushButton{"
"border-radius:5px;"// Set the fillet radius
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
ui->sendButton->setStyleSheet(
// Normal state style
"QPushButton{"
"background-color:#8D8D8D;"// Set the button background color
"border-radius:15px;"// Set the fillet radius
"color:white;"// Set button font color
"}"
"QPushButton:hover{"
"background-color:#5f9ae6;"
"color:white;"
"}");
// Auxiliary function //
// typeface
connect(ui->fontComboBox,&QFontComboBox::currentFontChanged,this,[=](const QFont &font){
ui->textEdit->setCurrentFont(font);
ui->textEdit->setFocus();
});
// Font size
void (QComboBox:: * cbxSingal)(const QString &text) = &QComboBox::currentIndexChanged;
connect(ui->comboBox,cbxSingal,this,[=](const QString &text){
ui->textEdit->setFontPointSize(text.toDouble());
ui->textEdit->setFocus();
});
// In bold
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();
});
// tilt
connect(ui->pushButton_4,&QToolButton::clicked,this,[=](bool checked){
ui->textEdit->setFontItalic(checked);
ui->textEdit->setFocus();
});
// Underline
connect(ui->pushButton_5,&QToolButton::clicked,this,[=](bool checked){
ui->textEdit->setFontUnderline(checked);
ui->textEdit->setFocus();
});
// Set text color
connect(ui->pushButton_6,&QToolButton::clicked,[=](){
QColor color = QColorDialog::getColor(Qt::red); //color The object can be in widget.h Define private members in
ui->textEdit->setTextColor(color);
});
// Save the chat
connect(ui->pushButton,&QToolButton::clicked,[=](){
if( ui->textBrowser->document()->isEmpty())
{
QMessageBox::warning(this," Warning "," The content cannot be empty ");
return;
}
else
{
QString path = QFileDialog::getSaveFileName(this," Save the chat "," Chat record ","(*.txt)");
QFile file(path);
// Open mode plus line feed operation
file.open(QIODevice::WriteOnly | QFile::Text);
QTextStream stream(&file);
stream << ui->textBrowser->toPlainText();
file.close();
}
});
// Clear chat
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, " Send a file ", QCoreApplication::applicationFilePath());
if (fileName.isEmpty())
{
QMessageBox::warning(this, " Send file prompt ", " Please select a file ");
}
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 part
privatechat:
#ifndef PRIVATECHAT_H
#define PRIVATECHAT_H
#include <QWidget>
#include <QTcpSocket>
#include <QJsonObject>
#include <QJsonDocument>
#include <QList>
#include <QFileDialog>
#include <QCoreApplication>
#include <QFile>
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<ChatWidgetInfo> *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<ChatWidgetInfo> *chatWidgetList;
QPoint label;
};
#endif // PRIVATECHAT_H
summary
The above is Yunxi chat room (YXChatting) Relevant introduction and private chat code of , The detailed code and related explanations will be updated step by step later , If there is a need to use , You can download and use it in the following ways , And visit my home page Qt Series of articles on interface optimization , There are relevant details of the interface optimization code .
If there is a problem , You can also chat or comment directly below , New technology , Newly arrived , If there is something wrong , Please correct and understand .
CSDN download :
https://download.csdn.net/download/m0_54754302/85203455
WeChat official account download address :
WeChat official account “ Yunxi Zhihua ”, reply “ Yunxi Chatting”, that will do free Obtain complete source code and executable program .
版权声明
本文为[Spiritual health]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231411423053.html
边栏推荐
- c语言在结构体传参时参数压栈问题
- Introduction to the use of countdownlatch and cyclicbarrier for inter thread control
- Redis cluster 原理
- Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
- API Gateway/API 网关(四) - Kong的使用 - 集成Jwt和熔断插件
- uni-app消息推送
- 数组模拟队列进阶版本——环形队列(真正意义上的排队)
- Usage of BC
- xx项目架构随记
- 爬虫练习题(一)
猜你喜欢

uni-app消息推送

XX project structure notes

单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料

51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc

一篇博客让你学会在vscode上编写markdown

Branch statement of process control

A blog allows you to learn how to write markdown on vscode

AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc

Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc

一款不错的工具:aardio
随机推荐
SSH 通过跳板机连接远程主机
想要成为架构师?夯实基础最重要
OpenSSH的升级、版本号的修改
SED 学以致用
C语言p2选择分支语句详解
LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)
gif转为静态图片处理
c语言在结构体传参时参数压栈问题
流程控制之分支语句
Preview CSV file
Web page, adaptive, proportional scaling
解决ssh配置文件优化以及连接慢的问题
JS key value judgment
查找水仙花数-for循环实践
电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34
C语言知识点精细详解——数据类型和变量【1】——进位计数制
C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】
Usage of BC
Redis源码分析之HSET流程与ziplist