QT使用HTTP下载来实现程序下载自动安装退出,同时读取JSON更新信息。
最近在用QT开发一套免费的HelpDesk系统, 参考了网上的方法,实现了程序自动下载更新和程序自动退出再安装新程序,为了感谢网页的无私分享,自己也特地分享给大家,希望可以帮助到大家,如果有疑问,可以留言或者email:13692277450@139.com. 我开发的程序大家可以免费下载使用,地址: https://sourceforge.net/projects/super-helpdesk/ , 或者访问网站: www.pavogroup.top.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | <br><br>#include "dialogupgrade.h" #include "ui_dialogupgrade.h" //网络相关头文件 #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> //JSON相关头文件 #include <QJsonArray> #include <QJsonObject> #include <QJsonDocument> #include <QJsonParseError> #include <QTextStreamManipulator> #include <QMessageBox> #include <QDesktopServices> #include <QDebug> #include <QFile> #include <QTest> #include <QFileInfo> #include <unistd.h> #include <QSaveFile> #include <QThread> #include <QProcess> DialogUpgrade::DialogUpgrade(QWidget *parent) : QDialog(parent), ui( new Ui::DialogUpgrade) { ui->setupUi( this ); Init (); // loadSetting (); Receive_Json(); } DialogUpgrade::~DialogUpgrade() { delete ui; } void DialogUpgrade::Init() { manager = new QNetworkAccessManager( this ); myfile = new QFile( this ); ui->progressBar->setFixedHeight (15); ui->progressBar->setValue (0); ui->progressBar->setAlignment (Qt::AlignVCenter); ui->lineEdit_Json->setVisible ( false ); ui->lineEdit->setVisible ( false ); ui->textEdit->append ( "The Super HelpDesk current version is: Ver1.0 \n" ); //ui->pushButton_upgrade->setDisabled (true); ui->pushButton_upgrade->setVisible ( false ); } void DialogUpgrade::on_pushButton_upgrade_clicked() { if (reply->isRunning ()) { return ; } else { reply->reset (); myfile->close (); QNetworkRequest request; ui->lineEdit->setText ( "http://www.pavogroup.top/superhelpdesk/superhelpdesk.exe" ); QString Url = ui->lineEdit->text(); request.setUrl(QUrl(Url)); reply = manager-> get (request); //发送请求 connect(reply,&QNetworkReply::readyRead, this ,&DialogUpgrade::doProcessReadyRead); //可读 connect(reply,&QNetworkReply::finished, this ,&DialogUpgrade::doProcessFinished); //结束 connect(reply,&QNetworkReply::downloadProgress, this ,&DialogUpgrade::doProcessDownloadProgress); //大小 // connect(reply,QOverload<QNetworkReply::NetworkError>:: of(&QNetworkReply::error), this,&DialogUpgrade::doProcessError); //error QStringList list = Url.split( "/" ); QString filename = list.at(list.length()-1); QString file = "C:/" + filename; myfile->setFileName(file); bool ret = myfile->open(QIODevice::WriteOnly|QIODevice::Truncate); //创建文件 if (!ret) { QMessageBox::warning( this , "Warning" , "Download upgrade file failure, pls try again later!" ); return ; } downloadSetup = true ; ui->progressBar->setValue(0); //设置进度条为0 ui->progressBar->setMinimum(0); } } void DialogUpgrade::doProcessReadyRead() //读取写入 { while (!reply->atEnd()) { QByteArray ba = reply->readAll(); myfile->write(ba); } } void DialogUpgrade::doProcessFinished() { myfile->close(); if (downloadSetup == true ) { isDownloadFinished = true ; Start_Install_New_Application(); } } void DialogUpgrade::doProcessDownloadProgress(qint64 recv_total, qint64 all_total) //显示 { ui->progressBar->setMaximum(all_total); ui->progressBar->setValue(recv_total); // QStringList list = ui->lineEdit->text().split("/"); // QString filename = list.at(list.length()-1); // QString data = "--" + filename; } void DialogUpgrade::doProcessError(QNetworkReply::NetworkError code) { qDebug() << code; } void DialogUpgrade::on_pushButton_exit_clicked() { myfile->close (); this ->hide (); } void DialogUpgrade::on_pushButton_Check_Upgrade_clicked() { ui->textEdit->clear (); ui->textEdit->append ( "The Super HelpDesk current version is: Ver1.0 \n" ); ui->textEdit->append ( "The lastest upgrade infromation: \n \n" ); QFile settingFile; settingFile.setFileName ( "C:/superhelpdeskupgrade.json" ); if (settingFile.open(QIODevice::ReadOnly|QIODevice::Text)) { QByteArray setting = settingFile.readAll ().trimmed (); QJsonParseError jsonError; QJsonDocument jsonDoc(QJsonDocument::fromJson(setting, &jsonError)); QJsonObject rootObj = jsonDoc. object (); QString LatestVersion = rootObj.value( "LastestVersion" ).toString (); QString UpdateTime = rootObj.value( "UpdateTime" ).toString (); QString ReleaseNote = rootObj.value( "ReleaseNote" ).toString(); QString Url = rootObj.value( "Url" ).toString(); ui->textEdit->append ( "Lastest Version : " + LatestVersion + "\n" ); ui->textEdit->append ( "Update Time : " + UpdateTime + "\n" ); ui->textEdit->append ( "Release Notes : " + ReleaseNote + "\n" ); ui->textEdit->append ( "\nSuccess to read upgrade version file! you can click download & upgrade button to download new version application, the new application installation package will be kept in C:\. \n" ); ui->pushButton_upgrade->setVisible ( true ); isCheckUpgradeDone = true ; } else { ui->textEdit->append ( "Sorry, Open upgrade version file failure! \n \n" ); } } void DialogUpgrade::Receive_Json() { ui->textEdit->clear (); ui->textEdit->append ( "The Super HelpDesk current version is: Ver1.0 \n" ); myfile->close (); QNetworkRequest request; ui->lineEdit->setText ( "http://www.pavogroup.top/superhelpdesk/superhelpdeskupgrade.json" ); QString Url = ui->lineEdit->text(); request.setUrl(QUrl(Url)); reply = manager-> get (request); //发送请求 QTest::qSleep (100); connect(reply,&QNetworkReply::readyRead, this ,&DialogUpgrade::doProcessReadyRead); //可读 connect(reply,&QNetworkReply::finished, this ,&DialogUpgrade::doProcessFinished); //结束 connect(reply,&QNetworkReply::downloadProgress, this ,&DialogUpgrade::doProcessDownloadProgress); //大小 // connect(reply,QOverload<QNetworkReply::NetworkError>:: of(&QNetworkReply::error), this,&DialogUpgrade::doProcessError); //error QStringList list = Url.split( "/" ); QString filename = list.at(list.length()-1); QString file = "C:/" + filename; myfile->setFileName(file); bool ret = myfile->open(QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Text); //创建文件 if (!ret) { ui->textEdit->append ( "Can not connect to upgrade server, pls check your connection or try again later!" ); return ; } else { ui->textEdit->append ( "Connect Super HelpDesk home website www.pavogroup.top successed. \n \n" ); return ; } myfile->aboutToClose (); myfile->flush (); ui->progressBar->setValue(0); //设置进度条初始化为0 ui->progressBar->setMinimum(0); return ; } void DialogUpgrade::on_progressBar_valueChanged( int value) { } void DialogUpgrade::Start_Install_New_Application() { ui->textEdit->append ( "Application download finished and keep installation file is C:\superhelpdesk.exe" ); qDebug() << "Start exit program and install new...." ; QProcess process( this ); QString str = "C:/superhelpdesk.exe" ; //加可执行文件路径 //qApp->setApplicationName (str); qApp->quit (); process.startDetached(str); //启动可执行文件 // QProcess::startDetached(qApp->applicationFilePath(), QStringList()); } |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用