概述
程序入口(main.cpp):
- #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设计师生成的类,所以界面可以在这个类中控制。该类的定义如下:
- #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
类的实现:
- #include "Update.h"
- Update::Update(QWidget *parent)
- : QDialog(parent)
- {
- setupUi(this);
- nextBlockSize=0;
- bytesWritten=0;
- progressBar->setMaximum(0);
- QFile vfile("version.dat");
- if (vfile.open(QIODevice::ReadOnly))
- {
- QTextStream stream(&vfile);
- version = stream.readLine();
- vfile.close();
- }
- //获取服务器地址
- QString ipAdd;//服务器地址
- QFile netfile("net.dat");
- if (netfile.open(QIODevice::ReadOnly))
- {
- QTextStream stream(&netfile);
- ipAdd = stream.readLine();
- netfile.close();
- }
- //连接到服务器
- clientSocket=new QTcpSocket(0);
- clientSocket->connectToHost(ipAdd,3327);
- QObject::connect(clientSocket,SIGNAL(readyRead()),this,SLOT(communicate()));
- QObject::connect(clientSocket,SIGNAL(disconnected()),this,SLOT(disconnect()));
- }
- Update::~Update()
- {
- }
- void Update::communicate()
- {
- QDataStream in(clientSocket);
- in.setVersion(QDataStream::Qt_4_5);
- while(1)
- {
- int bytes=(int)(clientSocket->bytesAvailable());
- //已经接收
- bytesWritten+=bytes;
- if(progressBar->maximum())
- {
- //设置进度条当前进度
- progressBar->setValue(bytes);
- comeLabel->setText(QString::number(bytes));
- }
- if (nextBlockSize == 0)
- {
- if (quint64(bytes) < sizeof(quint64))
- {
- break;
- }
- //总的数据大小
- in >> nextBlockSize;
- //设置进度条最大值
- progressBar->setMaximum((int)nextBlockSize);
- totleLabel->setText(QString::number((int)nextBlockSize));
- this->show();
- }
- if (quint64(bytes)<nextBlockSize)
- {
- break;;
- }
- //通信协议
- quint16 protocol;
- in>>protocol;
- switch(protocol)
- {
- case 0x0001:
- {
- in>>version_s;
- //最新版本,不用升级
- if(version==version_s)
- {
- QMessageBox::information(this,tr("提示"),tr("当前版本已经是最新的了!"));
- QProcess::startDetached("MRP.exe");
- qApp->quit();
- }
- else
- {
- QByteArray block;
- QDataStream out(&block, QIODevice::WriteOnly);
- out.setVersion(QDataStream::Qt_4_5);
- out<<quint64(0)<<quint16(0x0001);
- out.device()->seek(0);
- out<<quint64(block.size()-sizeof(quint64));
- clientSocket->write(block);
- }
- break;
- }
- case 0x0002:
- {
- QByteArray block1;
- QByteArray block2;
- in>>block1>>block2;
- //打开要更新的文件并写入数据
- QFile file1("MRP.exe");
- if (file1.open(QIODevice::WriteOnly)&&block1.length()!=0)
- {
- file1.write(block1);
- file1.close();
- }
- QFile file2("common.dll");
- if (file2.open(QIODevice::WriteOnly)&&block2.length()!=0)
- {
- file2.write(block2);
- file2.close();
- }
- //修改版本文件
- QFile wvfile("version.dat");
- if (wvfile.open(QIODevice::WriteOnly))
- {
- wvfile.write(version_s.toUtf8());
- wvfile.write("/r/n");
- //写入更新日期
- wvfile.write(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss").toUtf8());
- wvfile.close();
- }
- QMessageBox::information(this,tr("升级成功"),"您的软件版本已经从"+version+"升级到"+version_s+",将重启启动软件!");
- //启动主程序
- QProcess::startDetached("MRP.exe");
- qApp->quit();
- break;
- }
- }
- nextBlockSize=0;
- }
- }
- void Update::disconnect()
- {
- clientSocket->deleteLater();
- QMessageBox::information(this,tr("错误"),tr("网络连接中断!"));
- qApp->quit();
- }
最后
以上就是危机眼神为你收集整理的Qt编写的软件在线升级系统(客户端)的全部内容,希望文章能够帮你解决Qt编写的软件在线升级系统(客户端)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复