Не завершается консольное приложение в функции App::replyError()
, хотя при ошибке в функцию заходит, и сообщение выводится. В App::run()
все нормально.
PS: Делаю первые попытки в Qt. Сильно говнокодно?
project.pro
CONFIG += console
QT = core network
HEADERS = \
app.h
SOURCES = main.cpp \
app.cpp \
main.cpp
#include <QtCore>
#include "app.h"
int main(int argc, char *argv[])
{
QCoreApplication application(argc, argv);
App app;
QTimer::singleShot(0, &app, &App::run);
return application.exec();
}
app.h
#ifndef APP_H
#define APP_H
#include <QtCore>
#include <QtNetwork>
class App : public QObject
{
Q_OBJECT
public:
App(QObject *parent = 0);
public slots:
void run();
private:
QNetworkAccessManager *manager;
QString token;
void getToken();
void replyError(QNetworkReply::NetworkError code);
};
#endif
app.cpp
#include <QDebug>
#include "app.h"
const QString battleNetClientId = "battleNetClientId";
const QString battleNetClientSecret = "battleNetClientSecret";
App::App(QObject *parent) : QObject(parent)
{
manager = new QNetworkAccessManager(this);
}
void App::run()
{
getToken();
qDebug() << token;
QCoreApplication::quit();
}
void App::getToken()
{
const QString authorizationData = QString("%1:%2").arg(battleNetClientId).arg(battleNetClientSecret);
const QString headerData = QString("Basic %1").
arg(QString::fromStdString(authorizationData.toLatin1().toBase64().toStdString()));
QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setRawHeader("Authorization", headerData.toLatin1());
request.setUrl(QUrl("https://eu.battle.net/oauth/token"));
QUrlQuery postData;
postData.addQueryItem("grant_type", "client_credentials");
postData.addQueryItem("scope", "wow.profile");
QNetworkReply *reply = manager->post(request, postData.toString(QUrl::FullyEncoded).toLatin1());
connect(reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &App::replyError);
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
QJsonObject object = document.object();
token = object["access_token"].toString();
reply->deleteLater();
}
void App::replyError(QNetworkReply::NetworkError code)
{
qCritical() << code;
QCoreApplication::exit(1);
}