当前位置:网站首页>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
边栏推荐
- Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
- redis的五种数据类型
- 修改Firebase Emulators的默认侦听IP
- Use cases of the arrays class
- 【Servlet】Servlet 详解(使用+原理)
- Docker (V) MySQL installation
- 单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
- API Gateway/API 网关(四) - Kong的使用 - 集成Jwt和熔断插件
- LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)
- Nacos作为配置中心(四) 使用Demo
猜你喜欢
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
XX project structure notes
Branch statement of process control
Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
TLS/SSL 协议详解 (30) SSL中的RSA、DHE、ECDHE、ECDH流程与区别
Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】
After entering the new company, the operation and maintenance engineer can understand the deployment of the system from the following items
Uni app message push
随机推荐
循环队列的基本操作,你学会了吗?
单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
Parameter stack pressing problem of C language in structure parameter transmission
微信小程序轮播图swiper
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
Redis cluster 原理
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
SSH 通过跳板机连接远程主机
AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
js 格式化时间
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等
MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)
flannel 原理 之 TUN模式
翻牌效果
Introduction to the use of countdownlatch and cyclicbarrier for inter thread control
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等