[NEED HELP] Download file from URL
-
a small piece of trolling. Python is more suitable for this.
@# -- coding: utf-8 --
import urllib.request
urlopener = urllib.request.URLopener()
urlopener.retrieve(imgurl,filename)@ -
"it may help you":http://developer.qt.nokia.com/forums/viewthread/7091/
-
i don't solve my problem, i try this:
@
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass(QObject *parent = 0) : QObject(parent), manager(new QNetworkAccessManager)
{
reply = manager->get("http://example.com");
connect(reply,SIGNAL(finished()),SLOT(downloaded()));
}public slots:
void downloaded()
{
QFile file("/path/to/file");
file.open(QIODevice::WriteOnly);
file.write(reply.readAll());
file.close();
}private:
QNetworkAccessManager *manager;
QNetworkReply *reply;
}
@
i get some errors http://imageshack.us/f/28/immaginezrv.jpg/ -
One thing, I had a small mistake in my original example, it's fixed now:
@
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass(QObject *parent = 0) : QObject(parent), manager(new QNetworkAccessManager)
{
reply = manager->get(QNetworkRequest(QUrl("http://example.com")));
connect(reply,SIGNAL(finished()),SLOT(downloaded()));
}public slots:
void downloaded()
{
QFile file("/path/to/file");
file.open(QIODevice::WriteOnly);
file.write(reply.readAll());
file.close();
}private:
QNetworkAccessManager *manager;
QNetworkReply *reply;
}
@
also, you need to call
@
QMainWindow(parent)
@
instead of
@
QObject(parent)
@ -
The class you provided in your last comment is not the same as that in the screenshot.
Please provide the correct class here and paste the error messages; you can mark them in Qt Creator and copy them to the clipboard and paste here.
-
now i only get one error:
C:\Users\zanotti\aaa-build-desktop..\aaa\main.cpp:8: error: call of overloaded 'MainWindow()' is ambiguous
C:\Users\zanotti\aaa-build-desktop..\aaa\mainwindow.h:24: candidates are: MainWindow::MainWindow(QMainWindow*)
C:\Users\zanotti\aaa-build-desktop..\aaa\mainwindow.h:23: MainWindow::MainWindow(QWidget*) -
Hard to tell what's going wrong without the source code...
-
You have two overloaded functions with all default values, but you're constructing your object without parent (so the compiler doesn't know which one to use)
-
this is mainwindows.h:
@
#include <QObject>
#include <QUrl>
#include <QFile>
#include "qnetworkconfiguration.h"
#include <QNetworkRequest.h>
#include <QNetworkReply.h>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
MainWindow(QMainWindow *parent = 0) : QMainWindow(parent), manager(new QNetworkAccessManager)
{
reply = manager->get(QNetworkRequest(QUrl("http://example.com")));
connect(reply,SIGNAL(finished()),SLOT(downloaded()));
}public slots:
void buttonClickHandler();private:
Ui::MainWindow *ui;
QNetworkAccessManager *manager;
QNetworkReply *reply;};
#endif // MAINWINDOW_H
@
-
Now you have two constructors with the same signature - this must fail. On with an implementation in the header, one without.
I'd suggest you stick to the C++ coding convention and put the class declaration in the .h header file and the implementation in the .cpp file. You can put the implementation into the header but that's generally neither recommended nor needed, it has some implications and you must know what you're doing.
-
i put his in mainwindows.cpp
@
MainWindow( QMainWindow parent = 0) : QMainWindow(parent), manager(new QNetworkAccessManager)
{
reply = manager->get(QNetworkRequest(QUrl("http://example.com")));
connect(reply,SIGNAL(finished()),SLOT(downloaded()));
}
@
but not work! error:
@
C:\Users\zanotti\aaa-build-desktop..\aaa\mainwindow.cpp:29: error: expected ')' before '' token
@
i know there are some problem, but i have no time to study qt, in future I will do it -
Before you ask the same question in two threads, check if somebody hasn't already "answered":http://developer.qt.nokia.com/forums/viewthread/7091/#43864 to that (QMainWindow as a parent object would work two, but why would you want that if you derive from it):
@
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), manager(new QNetworkAccessManager)
{
reply = manager->get(QNetworkRequest(QUrl("http://example.com")));
connect(reply,SIGNAL(finished()),SLOT(downloaded()));
}
@ -
[quote author="abcth134" date="1309866897"]
i know there are some problem, but i have no time to study qt, in future I will do it[/quote]There is no problem with Qt here, but a lack of basic C++, as I understand it. I'd strongly suggest to read some basic tutorials on that...
-
Make sure to remove the " = 0" (default argument) part in your cpp file.
And as Volker said already do not start on learning Qt before you get at least some basic understanding of C++. -
I would like to post another solution that can be helpful if someone read this. This solution allows to download a file synchronously, without you need to return to the event loop.
I found it here: http://www.developer.nokia.com/Community/Wiki/How_to_wait_synchronously_for_a_Signal_in_Qt
@QNetworkAccessManager *networkMgr = new QNetworkAccessManager(this);
QNetworkReply *reply = networkMgr->get( QNetworkRequest( QUrl( "http://www.google.com" ) ) );QEventLoop loop;
QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit()));// Execute the event loop here, now we will wait here until readyRead() signal is emitted
// which in turn will trigger event loop quit.
loop.exec();// Lets print the HTTP GET response.
qDebug( reply->readAll());@