Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [NEED HELP] Download file from URL
Forum Updated to NodeBB v4.3 + New Features

[NEED HELP] Download file from URL

Scheduled Pinned Locked Moved General and Desktop
17 Posts 8 Posters 9.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abcth134
    wrote on last edited by
    #1

    Hi, i'm writing this post to get some information. I need to download a file from a url, i found some examples like this: http://doc.qt.nokia.com/4.7-snapshot/network-download-main-cpp.html - but when i take the part of the source that i need i get many errors. Can someone give me short code to downlaod a file?

    [EDIT: fixed link, volker]

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thisisbhaskar
      wrote on last edited by
      #2

      This post has code "to download a file from network":http://developer.qt.nokia.com/forums/viewthread/7228/

      1 Reply Last reply
      0
      • R Offline
        R Offline
        romankr
        wrote on last edited by
        #3

        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)@

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TobbY
          wrote on last edited by
          #4

          "it may help you":http://developer.qt.nokia.com/forums/viewthread/7091/

          1 Reply Last reply
          0
          • A Offline
            A Offline
            abcth134
            wrote on last edited by
            #5

            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/

            1 Reply Last reply
            0
            • L Offline
              L Offline
              loladiro
              wrote on last edited by
              #6

              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)
              @

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                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.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  abcth134
                  wrote on last edited by
                  #8

                  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*)

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    Hard to tell what's going wrong without the source code...

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      loladiro
                      wrote on last edited by
                      #10

                      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)

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        abcth134
                        wrote on last edited by
                        #11

                        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_OBJECT

                        public:
                        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

                        @

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          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.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            abcth134
                            wrote on last edited by
                            #13

                            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

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              loladiro
                              wrote on last edited by
                              #14

                              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()));
                              }
                              @

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                goetz
                                wrote on last edited by
                                #15

                                [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...

                                http://www.catb.org/~esr/faqs/smart-questions.html

                                1 Reply Last reply
                                0
                                • K Offline
                                  K Offline
                                  kkrzewniak
                                  wrote on last edited by
                                  #16

                                  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++.

                                  Me, Grimlock, not "nice dino". ME BASH BRAINS!

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SaiyanRiku
                                    wrote on last edited by
                                    #17

                                    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());@

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved