Qt编写的软件在线升级系统(客户端)

程序入口(main.cpp):

[cpp]  view plain copy
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include "Update.h"  
  6. int main(int argc, char *argv[])  
  7. {  
  8.     QApplication a(argc, argv);  
  9.     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));  
  10.     QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));  
  11.     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));  
  12.     Update *update=new Update();  
  13.     return a.exec();  
  14. }  

 

客户端用Qt设计师画了一个界面,有一个进度条和四个标签:

界面

 

Update类继承自Qt设计师生成的类,所以界面可以在这个类中控制。该类的定义如下:

 

[cpp]  view plain copy
  1. #ifndef UPDATE_H  
  2. #define UPDATE_H  
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. #include   
  10. #include "ui_ProgressBar.h"  
  11. class Update : public QDialog,public Ui::ProgressBarClass  
  12. {  
  13.     Q_OBJECT  
  14. public:  
  15.     Update(QWidget *parent = 0);  
  16.     ~Update();  
  17. private:  
  18.     QTcpSocket *clientSocket;  
  19.     quint64 nextBlockSize;  
  20.     QString version,version_s;  
  21.     int bytesWritten;  
  22. private slots:  
  23.     void disconnect();  
  24.     void communicate();  
  25. };  
  26. #endif // UPDATE_H  

 

类的实现:

 

[cpp]  view plain copy
  1. #include "Update.h"  
  2. Update::Update(QWidget *parent)  
  3.     : QDialog(parent)  
  4. {  
  5.     setupUi(this);  
  6.     nextBlockSize=0;  
  7.     bytesWritten=0;  
  8.     progressBar->setMaximum(0);  
  9.     QFile vfile("version.dat");  
  10.     if (vfile.open(QIODevice::ReadOnly))  
  11.     {  
  12.         QTextStream stream(&vfile);  
  13.         version = stream.readLine();  
  14.         vfile.close();  
  15.     }  
  16.     //获取服务器地址  
  17.     QString ipAdd;//服务器地址  
  18.     QFile netfile("net.dat");  
  19.     if (netfile.open(QIODevice::ReadOnly))  
  20.     {  
  21.         QTextStream stream(&netfile);  
  22.         ipAdd = stream.readLine();  
  23.         netfile.close();  
  24.     }  
  25.     //连接到服务器  
  26.     clientSocket=new QTcpSocket(0);  
  27.     clientSocket->connectToHost(ipAdd,3327);  
  28.     QObject::connect(clientSocket,SIGNAL(readyRead()),this,SLOT(communicate()));  
  29.     QObject::connect(clientSocket,SIGNAL(disconnected()),this,SLOT(disconnect()));  
  30. }  
  31. Update::~Update()  
  32. {  
  33. }  
  34. void Update::communicate()  
  35. {  
  36.     QDataStream in(clientSocket);  
  37.     in.setVersion(QDataStream::Qt_4_5);  
  38.     while(1)  
  39.     {  
  40.         int bytes=(int)(clientSocket->bytesAvailable());  
  41.         //已经接收  
  42.         bytesWritten+=bytes;  
  43.         if(progressBar->maximum())  
  44.         {  
  45.             //设置进度条当前进度  
  46.             progressBar->setValue(bytes);  
  47.             comeLabel->setText(QString::number(bytes));  
  48.         }  
  49.         if (nextBlockSize == 0)  
  50.         {  
  51.             if (quint64(bytes) < sizeof(quint64))  
  52.             {  
  53.                 break;  
  54.             }  
  55.             //总的数据大小  
  56.             in >> nextBlockSize;  
  57.             //设置进度条最大值  
  58.             progressBar->setMaximum((int)nextBlockSize);  
  59.             totleLabel->setText(QString::number((int)nextBlockSize));  
  60.             this->show();  
  61.         }  
  62.         if (quint64(bytes)
  63.         {  
  64.             break;;  
  65.         }  
  66.         //通信协议  
  67.         quint16 protocol;  
  68.         in>>protocol;  
  69.         switch(protocol)  
  70.         {  
  71.             case 0x0001:  
  72.             {  
  73.                 in>>version_s;  
  74.                 //最新版本,不用升级  
  75.                 if(version==version_s)  
  76.                 {  
  77.                     QMessageBox::information(this,tr("提示"),tr("当前版本已经是最新的了!"));  
  78.                     QProcess::startDetached("MRP.exe");  
  79.                     qApp->quit();  
  80.                 }  
  81.                 else  
  82.                 {  
  83.                     QByteArray block;  
  84.                     QDataStream out(&block, QIODevice::WriteOnly);  
  85.                     out.setVersion(QDataStream::Qt_4_5);  
  86.                     out<
  87.                     out.device()->seek(0);  
  88.                     out<sizeof(quint64));  
  89.                     clientSocket->write(block);  
  90.                 }  
  91.                 break;  
  92.             }  
  93.             case 0x0002:  
  94.             {  
  95.                 QByteArray block1;  
  96.                 QByteArray block2;  
  97.                 in>>block1>>block2;  
  98.                 //打开要更新的文件并写入数据  
  99.                 QFile file1("MRP.exe");  
  100.                 if (file1.open(QIODevice::WriteOnly)&&block1.length()!=0)  
  101.                 {  
  102.                     file1.write(block1);  
  103.                     file1.close();  
  104.                 }  
  105.                 QFile file2("common.dll");  
  106.                 if (file2.open(QIODevice::WriteOnly)&&block2.length()!=0)  
  107.                 {  
  108.                     file2.write(block2);  
  109.                     file2.close();  
  110.                 }  
  111.                 //修改版本文件  
  112.                 QFile wvfile("version.dat");  
  113.                 if (wvfile.open(QIODevice::WriteOnly))  
  114.                 {  
  115.                     wvfile.write(version_s.toUtf8());  
  116.                     wvfile.write("/r/n");  
  117.                     //写入更新日期  
  118.                     wvfile.write(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss").toUtf8());  
  119.                     wvfile.close();  
  120.                 }  
  121.                 QMessageBox::information(this,tr("升级成功"),"您的软件版本已经从"+version+"升级到"+version_s+",将重启启动软件!");  
  122.                 //启动主程序  
  123.                 QProcess::startDetached("MRP.exe");  
  124.                 qApp->quit();  
  125.                 break;  
  126.             }  
  127.         }  
  128.         nextBlockSize=0;  
  129.     }  
  130. }  
  131. void Update::disconnect()  
  132. {  
  133.     clientSocket->deleteLater();  
  134.     QMessageBox::information(this,tr("错误"),tr("网络连接中断!"));  
  135.     qApp->quit();  
  136. }  

你可能感兴趣的:(Qt,C++)