nilhcraiv
19th October 2011, 22:51
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)); 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.
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>
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::NetworkEr ror code);
void updateDataReadProgress(qint64,qint64);
};
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) :
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::NetworkE rror)),this,SLOT(replyErrorActualizaciones(QNetwor kReply::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();
.
.
.
.
.
I use that class from MainWindow.cpp
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
connect(ui->actionActualizaciones,SIGNAL(triggered()),SLOT(slo tCheckUpdates()));
}
.
.
.
.
void MainWindow::slotCheckUpdates()
{
updates a(this);
a.checkUpdates(version);
}//updates
I have thought a long time why it does not work and can not find the reason, I hope you could help me!
Thanks!
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)); 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.
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>
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::NetworkEr ror code);
void updateDataReadProgress(qint64,qint64);
};
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) :
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::NetworkE rror)),this,SLOT(replyErrorActualizaciones(QNetwor kReply::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();
.
.
.
.
.
I use that class from MainWindow.cpp
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
connect(ui->actionActualizaciones,SIGNAL(triggered()),SLOT(slo tCheckUpdates()));
}
.
.
.
.
void MainWindow::slotCheckUpdates()
{
updates a(this);
a.checkUpdates(version);
}//updates
I have thought a long time why it does not work and can not find the reason, I hope you could help me!
Thanks!