基于Qt简单聊天程序-具体步骤-详细注释.docx
基于qt编写的c/s模型的简单聊天程序开发Qt工具1,选择新建工程2选择Qt4GuiApplication工程。(带Ul界面编辑的工程)3取工程名C_S_Socket。路径随意,不要有任何中文Next4默认即可,Next(这是为此次工程选择要添加的头文件。我们不需要其他的功能。默认)5 Baseclass选择QDiaIog(IClassname改MainDlgoNext(我们是基于Qt界面编程的嘛。所以就选QDialogQt窗口类嚷)6 Finish完成(这里是编辑器告诉我们生成了如下文件)1点击maindlg.ui进入主界面窗体设计成如下2设置各个控件的对象名(不能同名,系统用对象名找到控件。命名标准方便自己识别)效劳器睢项选择框:radioBu11on_Server客户端雅项选择框:radioButton_CIientIP地址框:lineEdit_Address用户名框:1IneEditName离开按钮:WayButton进入按钮:GnterButton3创立一个窗体类,用于发送接收信息的窗体选择窗体样式。修改类名chat,下一步完成(chat:聊天的意思)4第二窗体设计如下窗体控件对象名如下显示信息框:ShowMessageEdit输入信息框:WritcMessageEdit用户名显示列表:OnIineMeSSageLiSt关于按钮:aboutButton发送按钮:SendButton界面等操作到此结束!!2023 年 12 月 8 日 16:47:31代码贴在后面。7/昆:代码写完后编译会出错!在C_S_Socket.pro工程文件的最后加上QT+=network编译就不会出错了(不知道什么意思,难道是为Qt参加网络的支持?)代码#ifndefMAINDLGH#defineMAINDLG_H#includc<QDialog>namespaceUiclassMainDlg;classMainDlg:publicQDialogQ_OBJECTpublic:MainDlg(QWidget*parent=O);-MainDlgO;boolm_bool_server;判断选择的方式protected:voidchangeEvent(QEvcnt*e);voidshowChatWindow();voidsendEnterMessage(QString,QString);private:Ui:MainDlg*ui;voidenterSlot();voidOnSelectServerO;voidOnSelectClientO;;#endifMAINDLG_H#include"maindlg.h,r#include,'uLmaindlg.h,'#include<QMessageBox>MainDlg:MainDlg(QWidget*parent):QDialog(parent),ui(newUi:MainDlg)ui->setupUi(this);"离开按钮点击事件的连接connect(ui->awayButton,SIGNAL(clicked(),qApp,SLOT(quit();进入按钮点击事件的连接connect(ui->enterButton,SIGNAL(clicked(),this,SLOT(enterSlot();选择效劳器单项选择按钮点击事件的连接connect(ui->radioButton_Server,SIGNAL(clicked(),this,SLOT(OnSelectServer();选择客户端单项选择按钮点击事件的连接connect(ui->radioButton-Client,SIGNAL(clickcd(),this,SLOT(OnSelectClient();MainDlg-MainDIgOdeleteui;产*选择效劳器单项选择按钮的点击消息voidMainDlg:OnSelectServer()m_bool_server=true;ui->lineEdicAddress->setTcxt(,n,);if(ui->lineEdit-Namc->text().isEmpty()ui->lincEdit_Name->text()=,Clicnt')ui->lineEdicName->setText(,Server);*选择客户端单项选择按钮的点击消息voidMainDlg:OnSclectClient()m_booLserver=false;ui->lineEdit-Address->setText(,l27.0.0.');if(ui->lineEdit_Name->text().isEmpty()ui->lineEdit_Name->text()=',ServerM)ui->lineEdit_Name->setText('Client);voidMainDlg:changeEvent(QEvent*e)QDialog:changeEvent(e);switch(e->type()caseQEvent:LanguageChange:ui->retranslateUi(this);break;default:break;voidMainDlg:enterSlot()if(ui->lineEdit_Name->text().isEmptyO)QMessageBoxmess;mess.setFont(QFont("SansSerif12,50);mess.warning(this,QString:ifroniLocalSBitC11出错"),QString:zfronLocal8Bit(',<fontSiZe=5>请输入正确的昵称,谢谢!<font>");return;"发射信号emitsendEterMessage(iL->lieEdit-Nae->text()1ui->lineE<iit-Address->text();emitshowChatWindow();#ifndefCHAT_HdefineCHAT_H#include<QDialog>#include"maindlg.h,r#include<QDateTime>#include<QtNetworkQTcpServer>lcp协议的效劳器监听连接类#include<QtNetworkQTcpSocket>Tcp协议的Socket类#inclucle<QtNetworkQHostAddress>namespaceUiclasschat;ClaSSchat:publicQDialogQ_OBJECTpublic:chat(QWidget*parunt=0);chat();voidSOCkUtSerVCr0;启动效劳器监听连接voidsocketCliet(QStringhost)/与效劳器连接MainDlg*p_my_dlg;"主界面指针intPOrtJ/端口号QStringm_str_userName;用户名boolrn_bool_servur;"是效劳器还是客户端/"cp协议的效劳器Socket类QTcpServer*m_p_server;/Tcp协议的效劳器监听连接类QTcpSocket*m_p_serverSoket;效劳器SocketQTcpSocket*m_p_clientSOCket;客户端SocketQDateTimenowDateTie;时间protected:voidchangeEvent(QEvent*e);private:Ui:Chat*ui;voidShOWAndHideSlOt();显示本窗体关闭主窗体voidChangCBUttOnStatCSIOt();/如果输入框为空,那么发送按钮不能使用voidCreateAboUtSIOt();关于信息voidcnterSlot(QStringname,QStringhost);选择效劳器或客户端voidncwConncctionSlot()y/连接来时,效劳器与客户端创立连接voidnewDataSlot();有数据来时voiddcleNameSlot();/voidaddSlot();voidappcndMessagcSlot();;#endifCHAT-H#include"chat.h"#include"ui_chat.h"#include<QMessageBox>chat:xhat(QWidget*parent):QDialog(parent),ui(newUi:chat)Iui->setupUi(this);设置UI设计的界面的控件m_p_server=NULL;m_p_serverSocket=NULL;m_p_c1ientSocket=NULL;p_my_dlg=newMainDIg,主界面p_my_dlg->show();显示主界面*连接信号主窗体中显示本窗体信号connect(p_my_dlg,SlGNAL(ShowChatWindowO),this,SLOT(showAndHideSlotO);ConneCt(p_my_dlg,SIGNAL(sendEnterMessage(QString,QString),this,SLOT(enterSlot(QString,QString);connect(ui->writeMessageEdit,SIGNAL(IextChangedO),this,SLOT(ChangeButtonStateSIotO);COnneCt(Ui-XiboutButton,SIGNAL(CIiCkUd(),this,SLOT(createAboutSlot();connect(ui->sendButton,SlGNAL(clicked(),this,SLOT(appendMessageSlot();Chat:chat()deleteui;"本窗体创立时由系统自动调用voidchat:xhangeEvent(QEvent*e)QDialogixhangeEvent(C);switch(e->type()caseQEventziLanguageChange:ui->rctranslatcUi(this);/设置本船体的些属性break;default:break;显示关闭主窗体,显示本窗体voidChat:ShOWAndHideSIUt()deletep_my_dlg;this->show();"显示关于信息voidchat:XreateAboutSlotOQMessageBoxmess;mess.setFont(QFont(,rSansSerif,12,50);mess.warning(this,QStrirg=fromLal8Bit("关于"),QString:fromLo