基于Qt簡單聊天程序-具體步驟-詳細(xì)注釋_第1頁
基于Qt簡單聊天程序-具體步驟-詳細(xì)注釋_第2頁
基于Qt簡單聊天程序-具體步驟-詳細(xì)注釋_第3頁
基于Qt簡單聊天程序-具體步驟-詳細(xì)注釋_第4頁
基于Qt簡單聊天程序-具體步驟-詳細(xì)注釋_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、基于qt編寫的cs模型的簡單聊天程序開發(fā)Qt工具一.創(chuàng)建工程1,選擇新建工程2 選擇Qt4 Gui Application工程。(帶UI界面編輯的工程)3 取工程名 C_S_Socket。路徑隨意,不要有任何中文 Next4 默認(rèn)即可,Next(這是為此次工程選擇要添加的頭文件。我們不需要其他的功能。默認(rèn))5 Base class 選擇QDialog。Class name 改 MainDlg。 Next(我們是基于Qt界面編程的嘛。所以就選QDialog Qt窗口類噻)6 Finish 完成(這里是編輯器告訴我們生成了如下文件)二.畫界面1 點擊maindlg.ui 進(jìn)入主界面窗體設(shè)計成如下2

2、 設(shè)置各個控件的對象名(不能同名,系統(tǒng)用對象名找到控件。命名規(guī)范方便自己識別)  服務(wù)器單選框:radioButton_Server客戶端單選框:radioButton_ClientIP地址框:lineEdit_Address用戶名框:lineEdit_Name離開按鈕:wayButton進(jìn)入按鈕:enterButton3 創(chuàng)建一個窗體類,用于發(fā)送接收信息的窗體選擇窗體樣式。修改類名 chat,下一步完成(chat:聊天的意思)4 第二窗體設(shè)計如下窗體控件對象名如下顯示信息框:showMessageEdit輸入信息框:writeMessageEdit用戶名顯示列表:onli

3、neMessageList關(guān)于按鈕:aboutButton發(fā)送按鈕:sendButton界面等操作到此結(jié)束!2013年12月8日16:47:31代碼貼在后面。注意:代碼寫完后編譯會出錯!在C_S_S 工程文件的最后加上 QT += network 編譯就不會出錯了 (不知道什么意思,難道是為Qt加入網(wǎng)絡(luò)的支持?)代碼maindlg.h#ifndef MAINDLG_H#define MAINDLG_H#include <QDialog>namespace Ui class MainDlg;class MainDlg : public QDialog Q_OBJEC

4、Tpublic: MainDlg(QWidget *parent = 0); MainDlg(); bool m_bool_server;/判斷選擇的方式protected: void changeEvent(QEvent *e);signals:/信號 void showChatWindow(); void sendEnterMessage(QString, QString);private: Ui:MainDlg *ui;public slots:/槽 void enterSlot(); void OnSelectServer(); void OnSelectClient();#endif

5、 / MAINDLG_Hmaindlg.cpp#include "maindlg.h"#include "ui_maindlg.h"#include <QMessageBox>MainDlg:MainDlg(QWidget *parent) : QDialog(parent), ui(new Ui:MainDlg) ui->setupUi(this); /離開按鈕點擊事件的連接 connect(ui->awayButton, SIGNAL(clicked(),qApp, SLOT(quit(); /進(jìn)入按鈕點擊事件的連接 conn

6、ect(ui->enterButton, SIGNAL(clicked(), this, SLOT(enterSlot(); /選擇服務(wù)器單選按鈕點擊事件的連接 connect(ui->radioButton_Server, SIGNAL(clicked(), this, SLOT(OnSelectServer(); /選擇客戶端單選按鈕點擊事件的連接 connect(ui->radioButton_Client, SIGNAL(clicked(), this, SLOT(OnSelectClient();MainDlg:MainDlg() delete ui;/* *選擇服

7、務(wù)器單選按鈕的點擊消息 */void MainDlg:OnSelectServer() m_bool_server = true; ui->lineEdit_Address->setText(""); if (ui->lineEdit_Name->text().isEmpty()| ui->lineEdit_Name->text()="Client") ui->lineEdit_Name->setText("Server");/* *選擇客戶端單選按鈕的點擊消息 */void MainD

8、lg:OnSelectClient() m_bool_server = false; ui->lineEdit_Address->setText(""); if (ui->lineEdit_Name->text().isEmpty() | ui->lineEdit_Name->text()="Server") ui->lineEdit_Name->setText("Client");void MainDlg:changeEvent(QEvent *e) QDialog:

9、changeEvent(e); switch (e->type() case QEvent:LanguageChange: ui->retranslateUi(this); break; default: break; void MainDlg:enterSlot() if (ui->lineEdit_Name->text().isEmpty() QMessageBox mess; mess.setFont(QFont("Sans Serif", 12, 50); mess.warning(this, QString:fromLocal8Bit(&q

10、uot;出錯"), QString:fromLocal8Bit("<font size=5>請輸入正確的昵稱,謝謝!</font>"); return; /發(fā)射信號 emit sendEnterMessage(ui->lineEdit_Name->text(), ui->lineEdit_Address->text(); emit showChatWindow();chat.h#ifndef CHAT_H#define CHAT_H#include <QDialog>#include "main

11、dlg.h"#include <QDateTime>#include <QtNetwork/QTcpServer>/Tcp協(xié)議的服務(wù)器監(jiān)聽連接類#include <QtNetwork/QTcpSocket>/Tcp協(xié)議的Socket類#include <QtNetwork/QHostAddress>namespace Ui class chat;class chat : public QDialog Q_OBJECTpublic: chat(QWidget *parent = 0); chat(); void socketServer(

12、);/啟動服務(wù)器監(jiān)聽連接 void socketClient(QString host);/與服務(wù)器連接 MainDlg *p_my_dlg;/主界面指針 int port;/端口號 QString m_str_userName;/用戶名 bool m_bool_server;/是服務(wù)器還是客戶端 /Tcp協(xié)議的服務(wù)器Socket類 QTcpServer *m_p_server;/Tcp協(xié)議的服務(wù)器監(jiān)聽連接類 QTcpSocket *m_p_serverSocket;/服務(wù)器Socket QTcpSocket *m_p_clientSocket;/客戶端Socket QDateTime now

13、DateTime;/時間protected: void changeEvent(QEvent *e);private: Ui:chat *ui;public slots:/槽 void showAndHideSlot();/顯示本窗體關(guān)閉主窗體 void changeButtonStateSlot();/如果輸入框為空,則發(fā)送按鈕不能使用 void createAboutSlot();/關(guān)于信息 void enterSlot(QString name, QString host);/選擇服務(wù)器或客戶端 void newConnectionSlot();/連接來時,服務(wù)器與客戶端創(chuàng)建連接 voi

14、d newDataSlot();/有數(shù)據(jù)來時 void deleNameSlot();/ void addSlot(); void appendMessageSlot();#endif / CHAT_Hchat.cpp#include "chat.h"#include "ui_chat.h"#include <QMessageBox>chat:chat(QWidget *parent) : QDialog(parent), ui(new Ui:chat) ui->setupUi(this);/設(shè)置 UI設(shè)計的界面的控件 m_p_serv

15、er = NULL; m_p_serverSocket = NULL; m_p_clientSocket = NULL; p_my_dlg = new MainDlg;/主界面 p_my_dlg->show(); /顯示主界面 /* *連接信號 */ /主窗體中顯示本窗體信號 connect(p_my_dlg, SIGNAL(showChatWindow(), this, SLOT(showAndHideSlot(); connect(p_my_dlg, SIGNAL(sendEnterMessage(QString , QString), this,SLOT(enterSlot(QSt

16、ring , QString ); connect(ui->writeMessageEdit, SIGNAL(textChanged(), this, SLOT(changeButtonStateSlot(); connect(ui->aboutButton, SIGNAL(clicked(), this, SLOT(createAboutSlot(); connect(ui->sendButton, SIGNAL(clicked(), this, SLOT(appendMessageSlot();chat:chat() delete ui;/本窗體創(chuàng)建時由系統(tǒng)自動調(diào)用voi

17、d chat:changeEvent(QEvent *e) QDialog:changeEvent(e); switch (e->type() case QEvent:LanguageChange: ui->retranslateUi(this);/設(shè)置本船體的一些屬性 break; default: break; /顯示關(guān)閉主窗體,顯示本窗體void chat:showAndHideSlot() delete p_my_dlg; this->show();/顯示關(guān)于信息void chat:createAboutSlot() QMessageBox mess; mess.se

18、tFont(QFont("Sans Serif", 12, 50); mess.warning(this, QString:fromLocal8Bit("關(guān)于"), QString:fromLocal8Bit("<font size=5>基于qt編寫的cs模型的簡單聊天程序</font>");/根據(jù)選擇p_my_dlg->m_bool_server 選擇相應(yīng)操作void chat:enterSlot(QString name, QString host) port = 22222;/設(shè)置Socket端口號

19、 m_bool_server = p_my_dlg->m_bool_server;/選擇狀態(tài)傳送 if (m_bool_server) socketServer(); else socketClient(host); ui->sendButton->setDisabled(false);/發(fā)送按鈕不能使用 m_str_userName = name; ui->onlineMessageList->addItem(name);/如果輸入框為空,則發(fā)送按鈕不能使用void chat:changeButtonStateSlot() bool boo_dis = ui-&

20、gt;writeMessageEdit->toPlainText().isEmpty(); ui->sendButton->setDisabled(boo_dis);/發(fā)送信息void chat:appendMessageSlot() /獲取輸入框內(nèi)容,然后清空 QString content = ui->writeMessageEdit->toPlainText(); if (content.isEmpty() QMessageBox:warning(this, "出錯", QString:fromLocal8Bit("發(fā)送的內(nèi)容不

21、能為空"); return; nowDateTime = QDateTime:currentDateTime();/獲取時間 /按指定格式拼合字符串 ui->showMessageEdit->append( QString("n%1 %2 %3n%4") .arg(nowDateTime.toString("yyyy-MM-dd hh:mm:ss") .arg(m_str_userName) .arg(QString:fromLocal8Bit("說道:") .arg(content) ); QDataStrea

22、m out;/流 if (m_bool_server) out.setDevice(m_p_serverSocket); else out.setDevice(m_p_clientSocket); int mark = 0;/普通信息代號 /發(fā)送代號+用戶名=信息 out << mark; out << m_str_userName << content; ui->writeMessageEdit->clear();/創(chuàng)建服務(wù)器Socket 監(jiān)聽連接void chat:socketServer() m_p_server = new QTcpSer

23、ver(this);/創(chuàng)建TCP 服務(wù)器用于監(jiān)聽socket連接 m_p_serverSocket = new QTcpSocket(this);/創(chuàng)建套接字 m_p_server->listen(QHostAddress:Any, port);/監(jiān)聽 socket連接 /設(shè)置 有新連接信號,觸發(fā)函數(shù) connect(m_p_server, SIGNAL(newConnection(), this, SLOT(newConnectionSlot();/有新的連接時。槽void chat:newConnectionSlot() m_p_serverSocket = m_p_server-&

24、gt;nextPendingConnection(); /有數(shù)據(jù)來時, connect(m_p_serverSocket, SIGNAL(readyRead(), this, SLOT(newDataSlot(); connect(m_p_serverSocket, SIGNAL(disconnected(), this, SLOT(deleNameSlot();/創(chuàng)建客戶端Socket 與服務(wù)器連接void chat:socketClient(QString host) m_p_clientSocket = new QTcpSocket(this); m_p_clientSocket->

25、;connectToHost(host, port); /當(dāng)成功連接時,名字list框中加入用戶名 connect(m_p_clientSocket, SIGNAL(connected(), this, SLOT(addSlot(); connect(m_p_clientSocket, SIGNAL(readyRead(), this, SLOT(newDataSlot(); connect(m_p_clientSocket, SIGNAL(disconnected(), this, SLOT(deleNameSlot();/在名字list框中加入用戶名void chat:addSlot()

26、QString content; QDataStream out(m_p_clientSocket);/Qt socket 數(shù)據(jù)傳輸?shù)姆绞?int mark = 11; /把socket看成流使用 out << mark;/發(fā)送代號11+用戶名, 告訴服務(wù)器,連接成功了, out << m_str_userName << content;/接收信息void chat:newDataSlot() int mark; QString hisName; QString content; QDataStream in;/流對象 if (m_bool_server) in.setDevice(m_p_serverSocket);/把服務(wù)器的Sokcet當(dāng)成流 else in.setDevice(m_p_clientSocket);/把客戶端的Sokcet當(dāng)成流 in >> mark;/讀取代號 / 如果代號是11,則是客戶端加入。添加用戶名 if (mark = 11) /addList in >> hisName >> content; i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論