qq客户端

发布时间:2012-05-29   来源:文档文库   
字号:

#ifndef QQ_H #define QQ_H
#include #include #include "qq2012.h"
namespace Ui { class QQ; }
class QQ : public QWidget { Q_OBJECT
public: explicit QQ(QWidget *parent = 0; ~QQ(; private: Ui::QQ *ui; QTcpSocket *socket; QString name; QQ2012 *qq2012; private slots: void on_loginButton_clicked(; void on_newButton_clicked(; void on_pushButton_3_clicked(;
void SendLoginMsgToserver(; void ReadReturnLoginMsg(QString; };
#endif // QQ_H
#include "qq.h" #include "ui_qq.h" #include "qq2012.h" #include "set.h" #include #include #include
QQ::QQ(QWidget *parent :
QWidget(parent,



ui(new Ui::QQ {
ui->setupUi(this; ui->groupBox->hide(; //showTable(; }
QQ::~QQ( { delete ui; }
void QQ::on_pushButton_3_clicked( { Set *set = new Set(this;
set->show(; }
void QQ::on_newButton_clicked( { //注册账号
}
void QQ::on_loginButton_clicked( { //登录

this->socket = new QTcpSocket; qq2012 = new QQ2012(socket;

QObject::connect(this->socket,SIGNAL(connected(,this,SLOT(SendLoginMsgToserver(;

QObject::connect(qq2012,SIGNAL(LogMsg(QString,this,SLOT(ReadReturnLoginMsg(QString;
// QHostAddress *address;
//address->setAddress("127.0.0.1"; this->socket->connectToHost("127.0.0.1",8000; }
//发送登录信息给服务器 void QQ::SendLoginMsgToserver( { QString msg;
QString account;
account = ui->loginaccountlineEdit->text(;





QString password; password = ui->loginpasswordlineEdit->text(; QString ip;
ip = ui->iplineEdit->text(;
QString port;
port = ui->portlineEdit->text(;

msg.append("login".append(";".append(account.append(";".append(password.append(";".append(ip.append(";".append(port; QByteArray buf;
buf = msg.toUtf8(; //QMessageBox::about(0,"error",msg; this->socket->write(buf; }
void QQ::ReadReturnLoginMsg(QString buf { if (QString::compare(buf,"true",Qt::CaseSensitive == 0
{ //登录成功
qq2012->setWindowTitle("qq2012 毁灭版"; qq2012->show(; this->close(; } }

#ifndef QQ2012_H #define QQ2012_H
#include #include #include #include "friend.h" #include #include #include "personchat.h"
namespace Ui {
class QQ2012; }
class QQ2012 : public QDialog



{ Q_OBJECT
public: explicit QQ2012(QTcpSocket *socket; ~QQ2012(; personChat *person; private: QListlists; int widget; private: Ui::QQ2012 *ui; QTcpSocket *socket; QString MyName; QSqlTableModel *model; QString chatroommessage; QString personmessage;
signals: void LogMsg(QString;


private slots:
void on_onlinequitButton_clicked(; void on_UserList_itemDoubleClicked(QListWidgetItem* item; void on_chatroomButton_clicked(; void ReadMsgFromServer(; };
#endif // QQ2012_H

#include "qq2012.h" #include "ui_qq2012.h" #include "qq.h" #include #include #include #include #include "mychatroom.h" //#include "personchat.h"
int flags = 0;



int people = 0; int privateflag = 0; int chatroomflag = 0; QQ2012::QQ2012( QTcpSocket *socket :
ui(new Ui::QQ2012 { ui->setupUi(this;
this->socket = socket; QObject::connect(this->socket,SIGNAL(readyRead(,this,SLOT(ReadMsgFromServer(;
}

QQ2012::~QQ2012( {
delete ui;
} void QQ2012::ReadMsgFromServer(//从服务器读取数据
{ int length = this->socket->bytesAvailable(;
if (length > 0
{ QByteArray buff;
buff.resize(length; buff = this->socket->readAll(;//读取服务器数据

QString string = QString::fromUtf8(buff,length;

//解析出登录信息
QStringList list; list = string.split(";"; QString total; total = list.at(0; QString buf;
buf = list.at(1;

//接收的登录信息
if (QString::compare(total,"login",Qt::CaseSensitive == 0
{ if (QString::compare(buf,"true",Qt::CaseSensitive==0
{ ui->namelabel->setText(list.at(2;//自己的昵称
MyName = list.at(2;




emit LogMsg(buf; } else
{ QMessageBox::about(0,"Error","你输入的账号或密码有误,请重新输入";
}


int i = 3; while (QString::compare(list.at(i++,"friend",Qt::CaseSensitive == 0//好友列表
{ QString cont = list.at(i++; QString nam = list.at(i++; QString stat = list.at(i++;
QString message; message.append(cont.append(" ".append(stat; ui->UserList->addItem(message; ui->UserList->show(; }
}

if (QString::compare(total,"chatroom",Qt::CaseSensitive == 0//接收的聊天室的内容 {


chatroommessage = list.at(1;
}
if(QString::compare(total,"person",Qt::CaseSensitive == 0 {

if (QString::compare("persondata",list.at(1,Qt::CaseSensitive ==0
{ this->person->GetData(list.at(2; } else
{ for (int i = 0;i < lists.size(; i++
{
personChat *newchat = lists.at(i; if(QString::compare(newchat->permessage,list.at(2 == 0
{ //QMessageBox::about(0,"列表值",list.at(2;
QString data;

data.append(list.at(2.append(list.at(3.append("\n".append(list.at(4;
this->person->GetMessageFromPerson(data;

".append(nam.append("




} } }
} if(QString::compare(total,"request",Qt::CaseSensitive == 0
{ QMessageBox::about(0,"提示",list.at(1; } } }
void QQ2012::on_chatroomButton_clicked(
{
QString msg; msg.append("chatroom".append(";".append(MyName.append(":".append("".append(";"; QByteArray buf; buf = msg.toUtf8(;
this->socket->write(buf; MychatRoom *chat = new MychatRoom(this,&chatroommessage,socket;
chat->show(; //打开一个聊天室界面
}

void QQ2012::on_UserList_itemDoubleClicked(QListWidgetItem* item { QString str = item->text(;
QStringList list; list = str.split(" ";

QString buf;

buf.append("personchat".append(";".append(list.at(0.append(";".append(MyName.append(";";//把自己的名字发过去请求通话

QByteArray msg; msg = buf.toUtf8(; this->socket->write(msg;
person = new personChat(socket;
QString name;



name.append("".append(list.at(5.append("对话中";
}

void QQ2012::on_onlinequitButton_clicked( { QByteArray array; QString buf = "quit";
array = buf.toUtf8(; this->socket->write(array; }

#ifndef PERSONCHAT_H #define PERSONCHAT_H
#include
#include
namespace Ui { class personChat; }
class personChat : public QDialog { Q_OBJECT
public: explicit personChat(QTcpSocket *; ~personChat(;
void GetMessageFromPerson(QString message; void GetData(QString datamessage; void GetName(QString msgname; QString perdata; QString permessage; int account; private:
Ui::personChat *ui;

this->person->GetName(list.at(5; person->setWindowTitle(name; this->person->permessage = list.at(5; lists.append(person;
person->show(;

QTcpSocket *socket; //QString permessage; QString myname; private slots:
private slots: void on_personchatpushButton_clicked(; // void ReadPerrsonMessage(; };
#endif // PERSONCHAT_H

#ifndef PERSONCHAT_H #define PERSONCHAT_H
#include
#include
namespace Ui { class personChat; }
class personChat : public QDialog { Q_OBJECT
public: explicit personChat(QTcpSocket *;
~personChat(;

void GetMessageFromPerson(QString message; void GetData(QString datamessage; void GetName(QString msgname; QString perdata; QString permessage; int account; private: Ui::personChat *ui;
QTcpSocket *socket;



//QString permessage; QString myname; private slots:
private slots: void on_personchatpushButton_clicked(; // void ReadPerrsonMessage(; };
#endif // PERSONCHAT_H


#include "personchat.h" #include "ui_personchat.h"
#include #include #include #include #include #include
extern int people;
personChat::personChat(QTcpSocket *socket :
ui(new Ui::personChat { ui->setupUi(this; this->socket = socket; }
void personChat::on_personchatpushButton_clicked( { QString msg;

msg.append("person".append(";".append(perdata.append(";".append(myname.append(";".append("˵:".append(";".append(ui->personsendlineEdit->text(.append(";"; QByteArray buf;
buf = msg.toUtf8(;



this->socket->write(buf; ui->personsendlineEdit->clear(; }
void personChat::GetData(QString datamessage {
//QStringList fd; //fd = datamessage.split(";"; perdata = datamessage;
} void personChat::GetName(QString msgname {
myname = msgname; }
void personChat::GetMessageFromPerson(QString message { ui->personchatlistWidget->addItem(message; ui->personchatlistWidget->show(; } personChat::~personChat( { delete ui; }

#ifndef MYCHATROOM_H #define MYCHATROOM_H
#include #include "qq2012.h" #include
namespace Ui { class MychatRoom; }
class MychatRoom : public QDialog { Q_OBJECT



public: explicit MychatRoom(QWidget *parent ,QString *message ,QTcpSocket *; ~MychatRoom(;
private: Ui::MychatRoom *ui; QString *message; QTcpSocket *socket; QString chatroommsg; private slots: void on_chatroomsendButton_clicked(; void ChatroomToRserve(; void showMessageFromServer(; };
#endif // MYCHATROOM_H

#include "mychatroom.h" #include "ui_mychatroom.h" #include #include #include #include #include #include extern int flags; MychatRoom::MychatRoom(QWidget *parent, QString *message, QTcpSocket *socket : QDialog(parent, ui(new Ui::MychatRoom { ui->setupUi(this;
this->message = message; this->socket = socket;

QTimer *timer = new QTimer; QObject::connect(timer,SIGNAL(timeout(,this,SLOT(showMessageFromServer(;
timer->start(50; QObject::connect(socket,SIGNAL(readyRead(,this,SLOT(ChatroomToRserve(; }
void MychatRoom::showMessageFromServer(



{ //if(chatroommsg.isEmpty(&& if (flags == 1
{ ui->chatroomlistWidget->addItem(*message; ui->chatroomlistWidget->show(;
flags = 0; } } void MychatRoom::ChatroomToRserve(//没实现从同一个socket读取数据
{ int length = this->socket->bytesAvailable(; if (length > 0
{ QByteArray msg;
msg.resize(length;
msg = this->socket->readAll(; QString room = QString::fromUtf8(msg,length; ui->chatroomlistWidget->addItem(room; ui->chatroomlistWidget->show(;
} }
MychatRoom::~MychatRoom( { delete ui; }

void MychatRoom::on_chatroomsendButton_clicked( {
QString msg; msg.append("chatroom".append(";".append("".append(ui->messagelineEdit->text(.append(";"; QByteArray buf; buf = msg.toUtf8(; this->socket->write(buf; }

本文来源:https://www.2haoxitong.net/k/doc/fa365c5cbe23482fb4da4cf1.html

《qq客户端.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式