当前位置:网站首页>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
边栏推荐
- Gif to still image processing
- Redis源码分析之HSET流程与ziplist
- Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface
- LotusDB 设计与实现—1 基本概念
- 矩阵交换行列
- 网页自适应,等比缩放
- Quickly understand the three ways of thread implementation
- Wechat applet rotation map swiper
- kprobe 的 3 种使用
- Flop effect
猜你喜欢
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
uni-app消息推送
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】
【Servlet】Servlet 详解(使用+原理)
KVM学习资源
51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
随机推荐
sar命令详解
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
grep无法重定向到文件的问题
C语言知识点精细详解——初识C语言【1】
Sed learning for application
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
如何5分钟上手使用OCR
Solve the problem of SSH configuration file optimization and slow connection
循环队列的基本操作(实验)
电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
Flop effect
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料