当前位置:网站首页>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



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 .

 Insert picture description here

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 .

 Insert picture description here

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 .

 Insert picture description here

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 .

 Insert picture description here

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 .

 Insert picture description here

6. Private chat interface :

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

 Insert picture description here

Two 、 Related source code

Project framework :
 Insert picture description here

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

随机推荐