Created
June 29, 2014 16:12
-
-
Save vannell/16b8d6f4dc7e21fa0431 to your computer and use it in GitHub Desktop.
Qt download test
This file contains hidden or 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.hpp" | |
#include <QNetworkRequest> | |
#include <QNetworkReply> | |
#include <QUrl> | |
#include <QVariant> | |
#include <QDebug> | |
Downloader::Downloader(QObject *parent) : | |
QObject(parent) | |
{ | |
connect(&m_manager, SIGNAL(finished(QNetworkReply*)), SLOT(handleReply(QNetworkReply*))); | |
} | |
void Downloader::test() { | |
QNetworkRequest request; | |
QUrl url("http://www.google.com"); | |
url.addQueryItem("q", "42"); | |
request.setUrl(url); | |
QNetworkReply* rep = m_manager.get(request); | |
rep->setProperty("url", QVariant(url)); | |
qDebug() << "Get" << rep << rep->url(); | |
} | |
void Downloader::handleReply(QNetworkReply *reply) { | |
qDebug() << "Handle" << reply << reply->url(); | |
qDebug() << "Dynamic property" << reply->property("url").isValid() << reply->property("url"); | |
reply->deleteLater(); | |
} |
This file contains hidden or 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_HPP | |
#define DOWNLOADER_HPP | |
#include <QObject> | |
#include <QNetworkAccessManager> | |
class QNetworkReply; | |
class Downloader : public QObject | |
{ | |
Q_OBJECT | |
public: | |
Downloader(QObject* parent=0); | |
void test(); | |
public slots: | |
void handleReply(QNetworkReply* reply); | |
protected: | |
QNetworkAccessManager m_manager; | |
}; | |
#endif // DOWNLOADER_HPP |
This file contains hidden or 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.hpp" | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
Downloader down; | |
down.test(); | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment