当前位置:编程学习 > wap >>

分享我用Qt写的游戏组队群聊系统

--------------------编程问答--------------------

#include "getserverinfo.h"
#include <QMessageBox>
#include <QHostAddress>
#include <QTimer>
#include <QTime>
#include <QNetworkInterface>
#include <QDebug>

QStringList GetServerInfo::filterChinaAreaList=QStringList()<<QString::fromLocal8Bit("云南,贵州,四川,西藏")                     //西南
                                            <<QString::fromLocal8Bit("新疆,陕西,宁夏,青海,甘肃")                  //西北
                                            <<QString::fromLocal8Bit("广东,广西,海南,香港,澳门")                  //华南
                                            <<QString::fromLocal8Bit("河南,湖北,湖南")                            //华中
                                            <<QString::fromLocal8Bit("山东,江苏,安徽,浙江,台湾,福建,江西,上海")     //华东
                                            <<QString::fromLocal8Bit("河北,山西,内蒙古,北京,天津")                //华北
                                            <<QString::fromLocal8Bit("辽宁,吉林,黑龙江");                       //东北

//=======================================================================================================================
GetServerInfo::GetServerInfo(const QString &teamid,const QString &privi,const QString &name,QObject *parent) :
    QObject(parent)
{
    teamID=teamid;
    privilege=privi;
    userName=name;
    hostAddress="113.140.83.67";

    webGet=new HttpGet(this);
    webGet->setCodec("GB2312");

}

void GetServerInfo::ConnectToHost()
{
    tcpClient=new QTcpSocket(this);

    tcpClient->connectToHost(hostAddress,8485,QIODevice::ReadWrite);
    connect(tcpClient,SIGNAL(connected()),SLOT(ConnectFinish()));
    connect(tcpClient,SIGNAL(readyRead()),SLOT(ReadMessgaeEvent()));
    connect(tcpClient,SIGNAL(error(QAbstractSocket::SocketError)),SLOT(NetworkError(QAbstractSocket::SocketError)));

}
//=======================================================================================================================
const QStringList GetServerInfo::GetLocalAndCircuit(const QString &ip)const//获取所在地区信息和线路信息
{
    QString content=webGet->get("http://www.ip138.com/ips138.asp?ip="+ip);
    uint leftIndex=content.indexOf("<li>");
    uint rightIndex=content.indexOf("</li>");
    content=content.mid(leftIndex+10,rightIndex-leftIndex-10);
    QStringList info(content.split(" ")); //0为所在地 1为线路
    QStringList temp;
    temp.append(info[0]);

    if(info.size()>1)//是否能捕获线路,防止程序出错
        temp.append(info[1]);
    else
        temp.append(tr("unknown"));

    return temp;
}


bool GetServerInfo::FilterArea(const QString local)const//过滤区域
{
    if(ChinaArea==ECA_ALL)
        return true;

    return filterChinaAreaList.at(ChinaArea-1).contains(local.left(2));
}

//=======================================================================================================================
void GetServerInfo::ConnectFinish()
{
    QStringList localAndCircuit=this->GetLocalAndCircuit(QNetworkInterface::allAddresses().first().toString());
    local=localAndCircuit[0];
    circuit=localAndCircuit[1];//获取线路信息
}

void GetServerInfo::SendServerDataType(HostInfo hi, const QStringList &list)
{
    QByteArray block;
    QDataStream out(&block,QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_7);
    out<<quint16(0)<<(quint8)hi;
    for (int i=0;i<list.size();++i)
        out<<list.at(i);

    out.device()->seek(0);
    out<<quint16(block.size()-sizeof(quint16));
    tcpClient->write(block);
}

void GetServerInfo::GetAdvertising()//获取广告
{
    SendServerDataType(HT_ADVERTISING);
}


void GetServerInfo::SendMessage(const QString &content)//发送消息
{
    QString s;
    if(privilege=="admin")
        s="<font color=red>"+content+"</font>";
    else
        s="<font color=#484891>"+content+"</font>";

    SendServerDataType(HT_MESSAGE,QStringList()<<teamID<<userName+"("+local+circuit+")"<<s);
}

void GetServerInfo::SendKick(const QString &id)//kick
{
    SendServerDataType(HT_KICK,QStringList()<<teamID<<id);
}

void GetServerInfo::SendRefreshMember()//发送刷新成员信息
{
    SendServerDataType(HT_MEMBER,QStringList()<<teamID);
}

void GetServerInfo::SendQuit()//发送退出信息
{
    if(privilege=="admin")//如果是管理员就解散该群
    {
        SendServerDataType(HT_DISSOLVE,QStringList()<<teamID);
    }
    else
    {
      SendServerDataType(HT_QUIT,QStringList()<<teamID<<userName);
    }
     tcpClient->waitForBytesWritten(200);//保证关闭时能够准确发送退出信息
}

void GetServerInfo::SendCreateTeam(const QString &teamFlag,const QString &content,const quint8 area)//发送创建群
{
     SendServerDataType(HT_CREATE,QStringList()<<teamFlag<<content<<userName<<QString::number(area));
}

void GetServerInfo::Login()//登入
{
    SendServerDataType(HT_LOGIN,QStringList()<<teamID<<userName);
}

//=====================================================================================
void GetServerInfo::ReadMessgaeEvent()
{
    QDataStream in(tcpClient);
    in.setVersion(QDataStream::Qt_4_7);
    static quint32 size=0;
    quint8 type;
    if(size==0)
        in>>size;

    qDebug()<<tcpClient->bytesAvailable();
    if(size>tcpClient->bytesAvailable())
        return;

    size=0;

    in>>type;

    switch(type)
    {

            case HT_INFO://读取团队信息
                        ReadTeamInfo(in);
                        break;

            case HT_MESSAGE://消息
                            ReadMessage(in);
                            break;

            case HT_KICK://KICK
                            ReadKick(in);
                            break;

            case HT_ADVERTISING://广告
                                ReadAdvertising(in);
                                break;
            case HT_MEMBER://读取成员
                           ReadMember(in);
                           break;

            case HT_LOGIN:
                            ReadLoginState(in);
                            break;

            case HT_DISSOLVE:// 解散
                            QMessageBox::information(0,tr("SystemInfo!"),tr("This team Group is Dissolve!"));
                            emit EmitKick();
                            break;


    }

    tcpClient->flush();
}

void GetServerInfo::ReadTeamInfo(QDataStream &in)//读取团队信息
{
    QString announcement,teamFlag,area,version;
    in>>announcement>>teamFlag>>teamID>>area>>version;

     emit EmitAnnouncement("<font color=blue>"+announcement+"</font>");

     emit EmitChangeWindowTitle(tr("  TeamFlag:")+teamFlag);
     emit EmitChangeWindowTitle(tr("  (ID:%1)").arg(teamID));

     ChinaArea=(EnumChinaArea)area.toInt();

     if(!FilterArea(local)&&privilege!="admin")//过滤区域
     {
         QMessageBox::information(0,tr("Area failed!"),tr("You Area cannot access!"));
         emit EmitKick();
     }

     QStringList list=version.split(",");//版本更新
     if(list[0]!=VERSION)
     {
         QStringList data;
         for(int i=1;i<list.size();++i)
             data<<list.at(i);
         emit EmitUpdateVersion(data);
     }

     this->SendRefreshMember();//接受完成后刷新成员
}

void GetServerInfo::ReadAdvertising(QDataStream &in)//读取广告
{
    QPixmap image;
    in>>image;
    emit EmitChangeAdvertising(image);
}

void GetServerInfo::ReadKick(QDataStream &in)//读取K掉的用户
{
    QString s;
    in>>s;
    if(s==userName&&privilege!="admin")
    {
        QMessageBox::information(0,tr("kick!"),tr("You have been kicked by administrator!"));
        emit EmitKick();
    }
}

void GetServerInfo::ReadMessage(QDataStream &in)//读取信息
{
    QString ID,content;
    in>>ID>>content;
    emit EmitMessage(tr("<font color=blue>%1</font><br>  %2<br>").arg(ID+QTime::currentTime().toString(" hh:mm:ss")).arg(content));
}

void GetServerInfo::ReadMember(QDataStream &in)//读取成员
{
    QString id;
    QStringList list;
    while(!in.atEnd())
    {
       in>>id;
       if(!id.isEmpty())
        list<<id;
    }

    emit EmitRefreshMember(list);
}

void GetServerInfo::ReadLoginState(QDataStream &in)//读取登入状态
{
    bool state;
    in>>state;
    if(!state)//登入失败提示
    {
        QMessageBox::warning(0,tr("Team Dissolve"),tr("Team group is dissolve! \n Cannot Alredy Login is same ream Group!"));
        emit EmitKick();
    }
}

//=================================================================================================================

void GetServerInfo::NetworkError(QAbstractSocket::SocketError error)
{
    if(hostAddress!="jasonsalex.gicp.net")//连接失败使用备用地址
    {
        delete tcpClient;
        hostAddress="jasonsalex.gicp.net";
        this->ConnectToHost();
    }else
    {

        QMessageBox::warning(0,tr("network error"),tcpClient->errorString());
        emit EmitKick();
    }
}
--------------------编程问答--------------------

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <getserverinfo.h>
#include <QStandardItemModel>
#include <createdialog.h>
#include <CustomGUI.h>
#include <QTimer>
#include <QProcess>
//=====================================================================================================
//WGM约战平台组队群聊系统.实现区域组队功能.获取玩家所在区域.玩家可以自己建立组队.队长踢人.解散等
//主界面的实现.基于QT代码
//程序编程人员:Jason's.Alex QQ:531401335 
//QT社区群:3855916
//日期:2012/01/14
//我会将WGM约战平台各个模块公开出来,整个平台用到了很多技术。我几乎花了半年的时间来开发这个平台,遗憾的我没有写开发文档,思路有点不清晰。虽然CS这个游戏落寞了。但是我们这些老菜鸟。希望能记起它说带来的美好
//=====================================================================================================
namespace Ui {
    class MainWindow;
}



class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void CreateAction();
    virtual void closeEvent(QCloseEvent *);

private slots:
    void ShowMember(const QStringList &);//显示成员

    void on_sendPushButton_clicked();
    void kick();
    void ShowMessageInfo(const QString &);//显示消息信息
    void ChangeWindowTitle(const QString &);//修改窗口标题
    void UpdateVersion(const QStringList &);//更新版本
    void KeyPress(QKeyEvent *e);
    void ChangeWindowIcon();//修改窗口图标

private:
    Ui::MainWindow *ui;
    GetServerInfo *ser;
    int  messageCount,timerCount;
    QStandardItemModel *memberModel;
    CreateDialog *createDialog;
    QString  titile;
    QTimer *timer;
    QProcess *updateProcess;//更新进程
};

#endif // MAINWINDOW_H

--------------------编程问答--------------------

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(this->size());
    this->setWindowTitle(tr("TT(WGM TeamTalk) ")+VERSION);
    messageCount=0;
    timerCount=0;

    updateProcess=new QProcess();

    ser=new GetServerInfo(qApp->argv()[1],qApp->argv()[2],qApp->argv()[3]);

    ser->ConnectToHost();

    connect(ui->sendTextEdit,SIGNAL(KeyPress(QKeyEvent *)),SLOT(KeyPress(QKeyEvent *)));
    memberModel=new QStandardItemModel(this);
    memberModel->setHorizontalHeaderItem(0,new QStandardItem(QIcon(":/icon/group"),tr("MemberList")));

    ui->memberTreeView->setModel(memberModel);
    ui->memberTreeView->setContextMenuPolicy(Qt::ActionsContextMenu);
    ui->memberTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    if(QString(qApp->argv()[2])=="admin")
    {
        createDialog=new CreateDialog(this);
        connect(createDialog,SIGNAL(EmitCreateTeam(QString,QString,quint8)),ser,SLOT(SendCreateTeam(QString,QString,quint8)));
        connect(createDialog,SIGNAL(rejected()),qApp,SLOT(quit()));

        QAction *action=new QAction(QIcon(":/icon/kick"),tr("kick"),this);
        connect(action,SIGNAL(triggered()),SLOT(kick()));
        ui->memberTreeView->addAction(action);

    }else
    {
        ser->Login();
    }

    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),SLOT(ChangeWindowIcon()));

    this->CreateAction();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::CreateAction()
{
    connect(ser,SIGNAL(EmitAnnouncement(QString)),ui->announcementTextEdit,SLOT(setText(QString)));
    connect(ser,SIGNAL(EmitChangeAdvertising(QPixmap)),ui->advertisingLabel,SLOT(setPixmap(QPixmap)));
    connect(ser,SIGNAL(EmitKick()),qApp,SLOT(quit()));
    connect(ser,SIGNAL(EmitMessage(QString)),SLOT(ShowMessageInfo(QString)));
    connect(ser,SIGNAL(EmitRefreshMember(QStringList)),SLOT(ShowMember(QStringList)));
    connect(ser,SIGNAL(EmitChangeWindowTitle(QString)),SLOT(ChangeWindowTitle(QString)));
    connect(ser,SIGNAL(EmitUpdateVersion(QStringList)),SLOT(UpdateVersion(QStringList)));
    connect(qApp,SIGNAL(aboutToQuit()),ser,SLOT(SendQuit()));

}

void MainWindow::ShowMember(const QStringList &list)//显示成员
{
   memberModel->removeRows(0,memberModel->rowCount());

   for(int i=0;i<list.size();++i)
   {
           memberModel->appendRow(new QStandardItem(QIcon(":/icon/admin"),list.at(i)));
   }
}

void  MainWindow::ShowMessageInfo(const QString &s)
{
    if(!this->isActiveWindow())//窗口是否活动
    {
        timer->start(500);
        messageCount++;
        this->setWindowTitle(tr("TT New Message(%1)").arg(messageCount));
    }

    ui->receiveTextBrowser->append(s);
}

void MainWindow::ChangeWindowIcon()
{

    if(this->isActiveWindow())//窗口是否活动
    {
        messageCount=0;
        this->setWindowTitle(titile);
        this->setWindowIcon(QIcon(":/icon/icon"));
        timer->stop();
    }
    else
    {
        if(++timerCount%2)
            this->setWindowIcon(QIcon(":/icon/sms"));
        else
            this->setWindowIcon(QIcon(":/icon/icon"));
    }

}

void MainWindow::kick()//踢人
{
    QString id=ui->memberTreeView->currentIndex().data().toString();
    ser->SendKick(id);
}

void MainWindow::on_sendPushButton_clicked()//发送消息
{
    if(!ui->sendTextEdit->toPlainText().isEmpty())
    {
        ser->SendMessage(ui->sendTextEdit->toPlainText());
        ui->sendTextEdit->clear();
    }else
    {
        ui->sendTextEdit->setFocus(Qt::MouseFocusReason);
    }
}

void MainWindow::ChangeWindowTitle(const QString &arg)//修改窗口标题
{
    this->setWindowTitle(this->windowTitle()+arg);
    titile=this->windowTitle();
}


void MainWindow::UpdateVersion(const QStringList &list)
{
    qApp->quit();
    updateProcess->setWorkingDirectory(qApp->applicationDirPath());
    updateProcess->start("WGMUpdate.exe",list);
}

void MainWindow::KeyPress(QKeyEvent *e)
{
    if(e->key()==Qt::Key_Return)
        this->on_sendPushButton_clicked();
}

void MainWindow::closeEvent(QCloseEvent *e)
{
    if(QMessageBox::information(this,tr("Confirm Close!"),tr("You are confirm close this?"),
                                QMessageBox::Ok,QMessageBox::Cancel)==QMessageBox::Cancel)
        e->ignore();
}

--------------------编程问答-------------------- --------------------编程问答-------------------- 怎么样? --------------------编程问答-------------------- 多谢楼主 --------------------编程问答--------------------
upup,谢谢分享。 --------------------编程问答-------------------- 非常强大........ --------------------编程问答-------------------- 呼呼...怎么样 --------------------编程问答-------------------- 发现Qt很强大啊。。。学习了。 --------------------编程问答--------------------
引用 9 楼  的回复:
呼呼...怎么样

我发现界面做的还不错,你的界面是用样式表实现的吗?你能否把你这段针对界面美化的代码贴出来给我看看,我最近正在学习这块内容,想了解下。 --------------------编程问答-------------------- 我看到图片上显示“浙江省嘉兴市电信”,莫非lz也是嘉兴学院毕业的? --------------------编程问答-------------------- 這個不錯。 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答--------------------
    if(QMessageBox::information(this,tr("Confirm Close!"),tr("You are confirm close this?"),

    You are confirm close this?  LZ这句英语好像有语法错误哦 --------------------编程问答-------------------- --------------------编程问答-------------------- 不了解QT....学习了.! 
--------------------编程问答-------------------- 只能多顶几次的 --------------------编程问答-------------------- 好长啊。。。支持下 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- nb!! --------------------编程问答-------------------- 支持一下! --------------------编程问答-------------------- 刚刚听说QT,不知道是不适合我。。不过看上去做的不错,支持一下哈
--------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 求楼主界面美化的代码分享,顶一个! --------------------编程问答-------------------- 支持一下 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 非常好的应用实例 --------------------编程问答-------------------- Thanks for share
--------------------编程问答-------------------- 感谢分享! --------------------编程问答-------------------- 不错啊,看下 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 样式确实挺漂亮的。 --------------------编程问答-------------------- --------------------编程问答-------------------- 学习了,谢谢。 --------------------编程问答-------------------- 好啊!!! --------------------编程问答-------------------- 我想了解下 --------------------编程问答-------------------- 祥云底图哇。。。 --------------------编程问答-------------------- QT是神马 --------------------编程问答-------------------- 界面图片应该是通过ps做的吧。 --------------------编程问答--------------------
upup,谢谢分享。 --------------------编程问答--------------------
引用 17 楼  的回复:
    if(QMessageBox::information(this,tr("Confirm Close!"),tr("You are confirm close this?"),

    You are confirm close this?  LZ这句英语好像有语法错误哦


Do you confirm to close this window? --------------------编程问答--------------------
引用 1 楼  的回复:
C/C++ code

#include "getserverinfo.h"
#include <QMessageBox>
#include <QHostAddress>
#include <QTimer>
#include <QTime>
#include <QNetworkInterface>
#include <QDebug>

QStringList GetServerInfo::fi……

可以吗? --------------------编程问答-------------------- 学习了,谢谢。 --------------------编程问答-------------------- 不用用你宝贵的时间,浪费时间了,lz --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 楼主,整一个源码下载吧。 --------------------编程问答-------------------- 楼主,请弄成压缩包来下载吧 --------------------编程问答-------------------- --------------------编程问答-------------------- 学习了  Qt真强大 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- LZ已经发布所有代码了 大家在CSDN下载里搜索吧http://download.csdn.net/detail/rushroom/4409207 资源开始下载于2012.07.03  在首页看到的

为作者感到遗憾。 --------------------编程问答-------------------- LZ已经发布所有代码了 大家在CSDN下载里搜索吧download.csdn.net/detail/rushroom/4409207 资源开始下载于2012.07.03  在首页看到的

为作者感到遗憾。 --------------------编程问答-------------------- 楼主流币了!! --------------------编程问答-------------------- 可以copy下来看看了,QT正在学习中,网络编程这块确实有点问题 --------------------编程问答-------------------- 好东西啊! --------------------编程问答-------------------- 貌似运行不出来,老是提示fatal error C1083: Cannot open include file: 'getserverinfo.h': No such file or directory
--------------------编程问答-------------------- 文字聊?一边打游戏一边打字聊天? --------------------编程问答--------------------
引用 17 楼  的回复:
    if(QMessageBox::information(this,tr("Confirm Close!"),tr("You are confirm close this?"),

  You are confirm close this? LZ这句英语好像有语法错误哦


确实啊 很低级的语法错误 --------------------编程问答-------------------- --------------------编程问答-------------------- 谢谢楼主啦 --------------------编程问答-------------------- QT没玩过,但是我知道LZ喜欢ALEX,最近ALEX又在TFE复出了 --------------------编程问答-------------------- 欣赏美工。。 --------------------编程问答-------------------- --------------------编程问答-------------------- 做得不错~ --------------------编程问答--------------------
引用 73 楼  的回复:
QT没玩过,但是我知道LZ喜欢ALEX,最近ALEX又在TFE复出了

这个你也知道啊.哈哈
补充:移动开发 ,  Qt
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,