Skip to content

Instantly share code, notes, and snippets.

@vannell
Created June 29, 2014 16:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vannell/16b8d6f4dc7e21fa0431 to your computer and use it in GitHub Desktop.
Save vannell/16b8d6f4dc7e21fa0431 to your computer and use it in GitHub Desktop.
Qt download test
#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();
}
#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
#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