Hello, I have a problem with my program, I hope you could help me!
I have written this code for download a file from internet. The problem is that it does not get download the file. I think that's code
reply = qnam.get(QNetworkRequest(url));
reply = qnam.get(QNetworkRequest(url));
To copy to clipboard, switch view to plain text mode
does not emit the signal ReadyRead, such as say the oficial documentation.
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object opened for reading which emits the readyRead() signal whenever new data arrives.
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object opened for reading which emits the readyRead() signal whenever new data arrives.
To copy to clipboard, switch view to plain text mode
Which is the problem of the code?
updates.h
#include <QString>
#include <QFile>
#include <QNetworkReply>
#include <QtNetwork/QtNetwork>
#include <QtNetwork/QNetworkAccessManager>
#include <QWidget>
#include <QObject>
#include <QMainWindow>
#include <QProgressDialog>
{
Q_OBJECT
public:
void checkUpdates
(QString version
);
private:
QString fileNameUpdate,versionInstalled,pathDownload;
QNetworkReply *reply;
QNetworkAccessManager qnam;
bool httpRequestAborted;
int iVersion;
private slots:
void httpFinished();
void httpFinishedDownload();
void httpReadyRead();
void replyErrorActualizaciones(QNetworkReply::NetworkError code);
void updateDataReadProgress(qint64,qint64);
};
#include <QString>
#include <QFile>
#include <QNetworkReply>
#include <QtNetwork/QtNetwork>
#include <QtNetwork/QNetworkAccessManager>
#include <QWidget>
#include <QObject>
#include <QMainWindow>
#include <QProgressDialog>
class updates : public QMainWindow
{
Q_OBJECT
public:
updates(QWidget *parent = 0);
void checkUpdates(QString version);
private:
QString fileNameUpdate,versionInstalled,pathDownload;
QFile *file;
QNetworkReply *reply;
QNetworkAccessManager qnam;
bool httpRequestAborted;
int iVersion;
void download(QString textUpdate);
QProgressDialog *dialog;
private slots:
void httpFinished();
void httpFinishedDownload();
void httpReadyRead();
void replyErrorActualizaciones(QNetworkReply::NetworkError code);
void updateDataReadProgress(qint64,qint64);
};
To copy to clipboard, switch view to plain text mode
updates.cpp
#include <QUrl>
#include <QFileInfo>
//#include <QObject>
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
#include <QProcess>
#include <QStringList>
#include "updates.h"
updates
::updates (QWidget *parent
) :{
iVersion =0;
}
void updates
::checkUpdates(QString version
) {
httpRequestAborted=false;
//Variables
QUrl url
("http://weblink.link/link");
QFileInfo fileInfo
(url.
path());
//url.path() devuelve a partir del .es/ .com/ .org/ fileNameUpdate.append(fileInfo.fileName());//nombre del arvhivo //.fileinfo -Returns the name of the file, excluding the path.
file = new QFile(fileNameUpdate
);
//Hasta aqui simplemente creo el archivo para poder escribir en el
versionInstalled = version;
reply = qnam.get(QNetworkRequest(url));//se supone que esto emite la readyRead(), pero no funciona!!
qDebug() << reply;
connect(reply, SIGNAL(finished()),this, SLOT(httpFinished()));
connect(reply, SIGNAL(readyRead()),this, SLOT(httpReadyRead()));
connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(replyErrorActualizaciones(QNetworkReply::NetworkError)));
}//checkUpdate
//Private slots
void updates::httpFinished()
{
//Variables
bool ok;
double versionServidor;
if (httpRequestAborted)
{
if (file)
{
file->close();
file->remove();
delete file;
file = 0;
}
reply->deleteLater();
return;
}//if
file->close();
.
.
.
.
.
void updates::httpReadyRead()
{
if (file)
file->write(reply->readAll());
qDebug() << "Ready";
}//httpReadyRead
void updates::replyErrorActualizaciones(QNetworkReply::NetworkError code)
{
qDebug() << "Download error: " << code;
httpRequestAborted = true;
}//replyErrorActualizaciones
void updates::httpFinishedDownload()
{
if (httpRequestAborted)
{
if (file)
{
file->close();
file->remove();
delete file;
file = 0;
}
reply->deleteLater();
.
.
.
.
.
#include <QUrl>
#include <QFileInfo>
//#include <QObject>
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
#include <QProcess>
#include <QStringList>
#include "updates.h"
updates::updates (QWidget *parent) :
QMainWindow(parent)
{
iVersion =0;
}
void updates::checkUpdates(QString version)
{
httpRequestAborted=false;
//Variables
QUrl url("http://weblink.link/link");
QFileInfo fileInfo(url.path());//url.path() devuelve a partir del .es/ .com/ .org/
fileNameUpdate.append(fileInfo.fileName());//nombre del arvhivo //.fileinfo -Returns the name of the file, excluding the path.
file = new QFile(fileNameUpdate);
file->open(QIODevice::WriteOnly);
//Hasta aqui simplemente creo el archivo para poder escribir en el
versionInstalled = version;
reply = qnam.get(QNetworkRequest(url));//se supone que esto emite la readyRead(), pero no funciona!!
qDebug() << reply;
connect(reply, SIGNAL(finished()),this, SLOT(httpFinished()));
connect(reply, SIGNAL(readyRead()),this, SLOT(httpReadyRead()));
connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(replyErrorActualizaciones(QNetworkReply::NetworkError)));
}//checkUpdate
//Private slots
void updates::httpFinished()
{
//Variables
QString text,auxiliar;
bool ok;
double versionServidor;
QFile fileToRead;
if (httpRequestAborted)
{
if (file)
{
file->close();
file->remove();
delete file;
file = 0;
}
reply->deleteLater();
return;
}//if
file->close();
.
.
.
.
.
void updates::httpReadyRead()
{
if (file)
file->write(reply->readAll());
qDebug() << "Ready";
}//httpReadyRead
void updates::replyErrorActualizaciones(QNetworkReply::NetworkError code)
{
qDebug() << "Download error: " << code;
httpRequestAborted = true;
}//replyErrorActualizaciones
void updates::httpFinishedDownload()
{
if (httpRequestAborted)
{
if (file)
{
file->close();
file->remove();
delete file;
file = 0;
}
reply->deleteLater();
.
.
.
.
.
To copy to clipboard, switch view to plain text mode
I use that class from MainWindow.cpp
MainWindow.cpp
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
connect(ui->actionActualizaciones,SIGNAL(triggered()),SLOT(slotCheckUpdates()));
}
.
.
.
.
void MainWindow::slotCheckUpdates()
{
updates a(this);
a.checkUpdates(version);
}//updates
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
connect(ui->actionActualizaciones,SIGNAL(triggered()),SLOT(slotCheckUpdates()));
}
.
.
.
.
void MainWindow::slotCheckUpdates()
{
updates a(this);
a.checkUpdates(version);
}//updates
To copy to clipboard, switch view to plain text mode
I have thought a long time why it does not work and can not find the reason, I hope you could help me!
Thanks!
Bookmarks