Last active
August 25, 2017 15:46
-
-
Save eyllanesc/cf6c61eae879b7a5a2902b49d0ed1841 to your computer and use it in GitHub Desktop.
Test of the question: https://stackoverflow.com/questions/45867580/download-public-dropbox-file-with-qt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "downloader.h" | |
Downloader::Downloader(const QUrl &url, QObject *parent) : QObject(parent) | |
{ | |
auto manager = new QNetworkAccessManager(this); | |
QNetworkRequest request(url); | |
connect(manager, &QNetworkAccessManager::finished, this, &Downloader::replyFinished); | |
manager->get(request); | |
} | |
void Downloader::replyFinished(QNetworkReply *reply) | |
{ | |
QUrl redirect= reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); | |
if (!redirect.isEmpty() && redirect.isValid() && reply->url() != redirect){ | |
qDebug()<<redirect; | |
if(redirect.isRelative()) | |
redirect = reply->url().resolved(redirect); | |
reply->manager()->get(QNetworkRequest(redirect)); | |
return; | |
} | |
qDebug()<<"finished"; | |
const auto newVersion = QString(reply->readAll()); | |
qDebug()<<newVersion; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DOWNLOADER_H | |
#define DOWNLOADER_H | |
#include <QNetworkReply> | |
#include <QObject> | |
class Downloader : public QObject | |
{ | |
Q_OBJECT | |
public: | |
explicit Downloader(const QUrl &url, QObject *parent = nullptr); | |
public slots: | |
void replyFinished(QNetworkReply *reply); | |
}; | |
#endif // DOWNLOADER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QT += core | |
QT -= gui | |
QT += network | |
CONFIG += c++11 | |
TARGET = DropboxDownload | |
CONFIG += console | |
CONFIG -= app_bundle | |
TEMPLATE = app | |
SOURCES += main.cpp \ | |
downloader.cpp | |
# The following define makes your compiler emit warnings if you use | |
# any feature of Qt which as been marked deprecated (the exact warnings | |
# depend on your compiler). Please consult the documentation of the | |
# deprecated API in order to know how to port your code away from it. | |
DEFINES += QT_DEPRECATED_WARNINGS | |
# You can also make your code fail to compile if you use deprecated APIs. | |
# In order to do so, uncomment the following line. | |
# You can also select to disable deprecated APIs only up to a certain version of Qt. | |
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 | |
HEADERS += \ | |
downloader.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include "downloader.h" | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
QUrl url("http://www.dropbox.com/s/ehoawgm89yz6pib/version.txt?raw=1"); | |
Downloader downloader(url); | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: