Created
January 5, 2019 10:22
-
-
Save leogout/d76aaa0856f785021a7dd28fed4255ce to your computer and use it in GitHub Desktop.
A gist that simplifies fetching data with Qt5.12 with a JavaScript flavor.
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 <QtNetwork/QNetworkReply> | |
#include <iostream> | |
#include "Fetcher.h" | |
void Fetcher::get(QString url, std::function<void(QNetworkReply *)> callback) { | |
int id = m_id++; | |
QNetworkReply *reply = m_nam.get(QNetworkRequest(QUrl(url))); | |
m_replies.insert(id, reply); | |
connect(reply, &QNetworkReply::finished, this, [&, id, callback]() { | |
QNetworkReply *r = m_replies.take(id); | |
callback(r); | |
r->deleteLater(); | |
}); | |
} |
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 <QtNetwork/QNetworkAccessManager> | |
#include <QtCore/QHash> | |
#include <QtCore/QSignalMapper> | |
class Fetcher : public QObject { | |
Q_OBJECT | |
int m_id = 0; | |
QNetworkAccessManager m_nam; | |
QHash<int, QNetworkReply*> m_replies; | |
public: | |
void get(QString url, std::function<void(QNetworkReply *)>); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment