软件升级

 
#include <QtGui>
#include <QApplication>
 #include <QProcess>
 #include <QTextCodec>
 #include "Update.h"
 int main(int argc, char *argv[])
 { QApplication a(argc, argv);
 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
 QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
 QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
 Update *update=new Update();
 return a.exec();
  }
客户端用Qt设计师画了一个界面,有一个进度条和四个标签:

 

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

view plaincopy to clipboardprint?

 #ifndef UPDATE_H
#define UPDATE_H
 #include <QtGui/QDialog>
 #include <QtGui/QMessageBox>
 #include <QFile>
 #include <QTcpSocket>
 #include <QDataStream>
 #include <QProcess>
 #include <QDatetime>
 #include "ui_ProgressBar.h"
 class Update : public QDialog,public Ui::ProgressBarClass
 {
 Q_OBJECT
 public:
 Update(QWidget *parent = 0);
 ~Update();
 private: QTcpSocket *clientSocket;
 quint64 nextBlockSize;
 QString version,version_s;
  int bytesWritten;
  private slots:
  void disconnect();
  void communicate();
 };
 #endif // UPDATE_H
类的实现:

view plaincopy to clipboardprint?
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)<nextBlockSize) 
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<<quint64(0)<<quint16(0x0001); 
87.                    out.device()->seek(0); 
88.                    out<<quint64(block.size()-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.} 

 

//-----------------------------------------------

http://blog.csdn.net/aaa20090987/article/details/7013404

http://www.qtcn.org/bbs/read-htm-tid-43819.html

http://blog.chinaunix.net/uid-25098120-id-67667.html

http://www.onlinedown.net/soft/22742.htm

我用的是Qt Creator,修改的方法是:首先准备个ICO图标。例如:A.ico,网上有很多图标文件。用记事本新建个txt里面就写一行:IDI_ICON1  ICON   DISCARDABLE   "A.ico" 保存,修改后缀为.rc,例如: myapp.rc把它和图标A.ico一起复制到你的QT工程项目的目录。打开你的QT工程文件.pro(例如 "myapp.pro" ),在里面最后新添一行RC_FILE = myapp.rc 保存,重新编译你的工程。如果想换图标,就重换一个图标,重命名为A.ico替换原来的,重新编译就可以了。

 

你可能感兴趣的:(软件升级)