对于软件的下载大家肯定会非常苦恼,因为你的下载通常伴随着各种各样的插件,让人烦躁。
今天教大家实现一个简单的下载器,运行如下图所示:
源代码下载
下载器中用到的主要有QtNetwork类中的
QNetworkRequest//网络请求
void BrowseWeb::on_pushButton_clicked()
{
QString qurl = ui->lineEdit->text();//获取输入的url地址
if(ui->lineEdit->text() != "")
{
manager->get(QNetworkRequest(QUrl(qurl)));//发出请求
}
else
{
QMessageBox::warning(this,QString::fromLocal8Bit("警告对话框"),
QString::fromLocal8Bit("请输入Url地址"),QMessageBox::Abort);
}
#include
class QFile;
void httpReadyRead();//http准备读取数据
void updateDataReadProgress(qint64,qint64);//更新进度条
void httpFinished();//http完成
③添加一个公有成员
void startRequest(QUrl url);//开始请求
void imgPathToHtml(QString &path);//设置widget背景图片
④添加私有数据成员
QNetworkReply *reply;
QUrl url;
QFile *file;
⑤在构造函数中添加
/*-------------------widget背景--------------------*/
this->setAutoFillBackground(true);
QPalette palette;
palette.setBrush(QPalette::Background,QBrush(QPixmap(":/image/image/back.jpg")));//资源文件夹目录
this->setPalette(palette);
/*------------------------------------------------*/
ui->progressBar->hide();//在刚开始无需要下载的时候隐藏进度条
⑥实现函数的定义
void BrowseWeb::startRequest(QUrl url)//开始请求
{
reply = manager->get(QNetworkRequest(url));
//每当有新的数据可以读取的时都会发送readyRead()信号
connect(reply,SIGNAL(readyRead()),
this,SLOT(httpReadyRead()));
//每当网络请求的下载进度更新时都会发送downloadProgress()信号
connect(reply,SIGNAL(downloadProgress(qint64,qint64)),
this,SLOT(updateDataReadProgress(qint64,qint64)));
//每当应答处理结束时都会发送finished()信号
connect(reply,SIGNAL(finished()),
this,SLOT(httpFinished()));
}
void BrowseWeb::httpReadyRead()
{
//判断是否创建了文件,是的话将读回来的数据写入到文件中
if(file)
file->write(reply->readAll());
}
void BrowseWeb::httpReadyRead()
{
//判断是否创建了文件,是的话将读回来的数据写入到文件中
if(file)
file->write(reply->readAll());
}
void BrowseWeb::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
{
//设置最大值和当前值
ui->progressBar->setMaximum(totalBytes);
ui->progressBar->setValue(bytesRead);
}
void BrowseWeb::httpFinished()
{
//完成下载后将进度条隐藏,将缓冲数据刷新到文件中,
//然后删除掉file和reply的对象
ui->progressBar->hide();
file->flush();
file->close();
reply->deleteLater();
reply = 0;
delete file;
file = 0;
}
void BrowseWeb::on_pushButton_2_clicked()//下载按钮
{
url = ui->lineEdit->text();
QFileInfo info(url.path());
QString fileName(info.fileName());
file = new QFile(fileName);
if(!file->open(QIODevice::WriteOnly))
{
qDebug()<<"file open error!";
delete file;
file = 0;
return;
}
startRequest(url);
ui->progressBar->setValue(0);
ui->progressBar->show();
}
void BrowseWeb::imgPathToHtml(QString &path)//设置背景图片
{
path = QString(" src=\"%1\"/>").arg(path);
}
/*---------------------推荐下载实现-----------------------*/
void BrowseWeb::on_pushButton_qq_clicked()
{
ui->textBrowser->clear();
ui->lineEdit->setText("http://dldir1.qq.com/qqfile/qq/QQ8.9.1/20453/QQ8.9.1.exe");
QString imgPath = QString(":/image/image/qq.jpg");
imgPathToHtml(imgPath);
ui->textBrowser->insertHtml(imgPath);
}
void BrowseWeb::on_pushButton_360_clicked()
{
ui->textBrowser->clear();
ui->lineEdit->setText("http://221.204.7.214/softdl.360tpcdn.com/se8/360se8.1.1.248.exe");
QString imgPath = QString(":/image/image/360.jpg");
imgPathToHtml(imgPath);
ui->textBrowser->insertHtml(imgPath);
}
void BrowseWeb::on_pushButton_aqy_clicked()
{
ui->textBrowser->clear();
ui->lineEdit->setText("http://221.204.7.210/softdl.360tpcdn.com/QIYImedia/QIYImedia_5.5.33.3550_opensoft_1221.exe");
QString imgPath = QString(":/image/image/aqy.jpg");
imgPathToHtml(imgPath);
ui->textBrowser->insertHtml(imgPath);
}
void BrowseWeb::on_pushButton_wps_clicked()
{
ui->textBrowser->clear();
ui->lineEdit->setText("http://221.204.197.13/big.softdl.360tpcdn.com/WPSOffice/WPSOffice_10.1.0.6260_opensoft.exe");
QString imgPath = QString(":/image/image/wps.jpg");
imgPathToHtml(imgPath);
ui->textBrowser->insertHtml(imgPath);
}
/*------------------------------------------------------*/