效果展示
本次項目采用的是TCP傳輸文件,UDP實現(xiàn)聊天以及聊天狀態(tài)的反饋。
一、首先運行程序會進入到這樣一個會話界面,也就是新加入一個用戶,新加入的用戶會在右側(cè)顯示其用戶名、主機名和IP地址,在消息記錄框中也會提示在線信息。
二、消息字體樣式、字體大小、加粗、斜體、下劃線、顏色的效果展示。
?三、進行文件傳輸。
1. 進行文件傳輸前要先從右側(cè)列表選擇傳輸?shù)膶ο螅缓筮x擇傳輸?shù)奈募?/p>
2. 文件選擇后會顯示將要發(fā)送的文件名,即可點擊發(fā)送。
?3. 發(fā)送端點擊發(fā)送后,接收端會彈出是否接收文件的提示框,然后進行選擇。選擇接收后會再次彈出保存文件的對話框,選擇保存路徑,就完成了文件的傳輸。文章來源:http://www.zghlxwxcb.cn/news/detail-511050.html
?四、保存聊天記錄會彈出保存文件對話框選擇保存路徑,輸入保存文件名即可,這里保存聊天記錄和清空聊天消息框內(nèi)容就不做展示了,直接上代碼。文章來源地址http://www.zghlxwxcb.cn/news/detail-511050.html
具體實現(xiàn)代碼
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTextCharFormat>
class QUdpSocket;
class TcpServer;
namespace Ui {
class Widget;
}
enum MessageType{Message, NewParticipant, ParticipantLeft, FileName, Refuse};
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected:
void newParticipant(QString userName,QString localHostName, QString ipAddress); //添加新用戶加入
void participantLeft(QString userName,QString localHostName, QString time); //用戶離開
void sendMessage(MessageType type, QString serverAddress=""); //發(fā)送UDP數(shù)據(jù)
QString getIP(); //獲取IP
QString getUserName(); //獲取用戶名
QString getMessage(); //獲取用戶聊天信息
void hasPendingFile(QString userName, QString serverAddress,QString clientAddress, QString fileName);//判斷接收文件
bool saveFile(const QString& fileName); //保存文件
void closeEvent(QCloseEvent *); //關(guān)閉事件
private:
Ui::Widget *ui;
QUdpSocket *udpSocket;
qint16 port;
QString fileName;
TcpServer *server;
QColor color;
public slots:
void processPendingDatagrams(); //接收UDP數(shù)據(jù)
void on_sendButton_clicked(); //發(fā)送
void getFileName(QString); //獲取文件名
void on_sendToolBtn_clicked(); //傳輸文件發(fā)送
void on_fontComboBox_currentFontChanged(QFont f); //更改字體
void on_sizeComboBox_currentIndexChanged(QString ); //更改字體大小
void on_boldToolBtn_clicked(bool checked); //加粗
void on_italicToolBtn_clicked(bool checked); //傾斜
void on_underlineToolBtn_clicked(bool checked); //下劃線
void on_colorToolBtn_clicked(); //顏色設(shè)置
void currentFormatChanged(const QTextCharFormat &format);
void on_saveToolBtn_clicked(); //保存聊天記錄
void on_clearToolBtn_clicked(); //清空記錄
void on_exitButton_clicked(); //退出
};
#endif // WIDGET_H
widget.cpp (主界面具體功能)
#include "widget.h"
#include "ui_widget.h"
#include <QUdpSocket>
#include <QHostInfo>
#include <QMessageBox>
#include <QScrollBar>
#include <QDateTime>
#include <QNetworkInterface>
#include <QProcess>
#include "tcpserver.h"
#include "tcpclient.h"
#include <QFileDialog>
#include <QColorDialog>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowIcon(QPixmap(":/C:/Users/liuqy/Desktop/qq.png"));
//創(chuàng)建UDP套接字,并初始化
udpSocket = new QUdpSocket(this);
port = 45454;
udpSocket->bind(port, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
sendMessage(NewParticipant);
//創(chuàng)建TCP套接字
server = new TcpServer(this);
connect(server, SIGNAL(sendFileName(QString)), this, SLOT(getFileName(QString)));
connect(ui->messageTextEdit, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
this, SLOT(currentFormatChanged(const QTextCharFormat)));
}
Widget::~Widget()
{
delete ui;
}
//發(fā)送UDP數(shù)據(jù)
void Widget::sendMessage(MessageType type, QString serverAddress)
{
QByteArray data;
QDataStream out(&data, QIODevice::WriteOnly);
QString localHostName = QHostInfo::localHostName();
QString address = getIP();
out << type << getUserName() << localHostName;
switch(type)
{
case Message :
if (ui->messageTextEdit->toPlainText() == "") {
QMessageBox::warning(0,tr("警告"),tr("發(fā)送內(nèi)容不能為空"),QMessageBox::Ok);
return;
}
out << address << getMessage();
ui->messageBrowser->verticalScrollBar()
->setValue(ui->messageBrowser->verticalScrollBar()->maximum());
break;
case NewParticipant :
out << address;
break;
到了這里,關(guān)于基于qt的簡易聊天實現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!