Hello! I tried to make multiple connections to QNetworkAccessManager finished() signal.
I have ApiHandler class which makes a request handling.
And need to call a specific function after request finished (make use of slots).
I tried to make realization as below. But always worked only one connect (which is first one) - validateTokenFinished.
Explain how to properly realize what i want.
Thanks!
userapi.h
// includes...
{
Q_OBJECT
public:
explicit UserApi
(QObject *parent
= 0);
void updateFriendList();
signals:
public slots:
void friendListUpdated(QNetworkReply* reply);
};
// includes...
class UserApi : public QObject
{
Q_OBJECT
public:
explicit UserApi(QObject *parent = 0);
void updateFriendList();
signals:
public slots:
void friendListUpdated(QNetworkReply* reply);
};
To copy to clipboard, switch view to plain text mode
userapi.cpp
// includes
UserApi
::UserApi(QObject *parent
) :{
}
void UserApi::updateFriendList()
{
qDebug("UserApi::updateFriendList()");
QMap<QString, QString> m;
// ...
ApiHandler *a = new ApiHandler;
connect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(friendListUpdated(QNetworkReply*)));
a->makeRequest("friends.get", m);
}
//! Not called at all
void UserApi::friendListUpdated(QNetworkReply* reply)
{
QJsonDocument j = QJsonDocument::fromJson(reply->readAll());
QJsonObject getjson = j.object();
qDebug() << "UserApi::friendListUpdated()" << getjson;
reply->deleteLater();
}
// includes
UserApi::UserApi(QObject *parent) :
QObject(parent)
{
}
void UserApi::updateFriendList()
{
qDebug("UserApi::updateFriendList()");
QMap<QString, QString> m;
// ...
ApiHandler *a = new ApiHandler;
connect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(friendListUpdated(QNetworkReply*)));
a->makeRequest("friends.get", m);
}
//! Not called at all
void UserApi::friendListUpdated(QNetworkReply* reply)
{
QJsonDocument j = QJsonDocument::fromJson(reply->readAll());
QJsonObject getjson = j.object();
qDebug() << "UserApi::friendListUpdated()" << getjson;
reply->deleteLater();
}
To copy to clipboard, switch view to plain text mode
toolsapi.h
// includes...
{
Q_OBJECT
public:
explicit ToolsApi
(QObject *parent
= 0);
void validateToken();
signals:
public slots:
void validateTokenFinished(QNetworkReply* reply);
};
// includes...
class ToolsApi : public QObject
{
Q_OBJECT
public:
explicit ToolsApi(QObject *parent = 0);
void validateToken();
signals:
public slots:
void validateTokenFinished(QNetworkReply* reply);
};
To copy to clipboard, switch view to plain text mode
toolsapi.cpp
// includes
ToolsApi
::ToolsApi(QObject *parent
) :{
}
void ToolsApi::validateToken()
{
qDebug("ToolsApi::validateToken()");
QMap<QString, QString> m;
// ...
ApiHandler *a = new ApiHandler;
connect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(validateTokenFinished(QNetworkReply*)));
a->makeRequest("tools.isTokenValid", m);
}
void ToolsApi::validateTokenFinished(QNetworkReply* reply)
{
QJsonDocument j = QJsonDocument::fromJson(reply->readAll());
QJsonObject getjson = j.object();
qDebug() << "ToolsApi::validateTokenFinished()" << getjson;
reply->deleteLater();
}
// includes
ToolsApi::ToolsApi(QObject *parent) :
QObject(parent)
{
}
void ToolsApi::validateToken()
{
qDebug("ToolsApi::validateToken()");
QMap<QString, QString> m;
// ...
ApiHandler *a = new ApiHandler;
connect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(validateTokenFinished(QNetworkReply*)));
a->makeRequest("tools.isTokenValid", m);
}
void ToolsApi::validateTokenFinished(QNetworkReply* reply)
{
QJsonDocument j = QJsonDocument::fromJson(reply->readAll());
QJsonObject getjson = j.object();
qDebug() << "ToolsApi::validateTokenFinished()" << getjson;
reply->deleteLater();
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
// includes...
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
ToolsApi c;
public slots:
void on_testButton_clicked();
private:
Ui::MainWindow *ui;
};
// includes...
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
ToolsApi c;
public slots:
void on_testButton_clicked();
private:
Ui::MainWindow *ui;
};
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
// ...
ui->setupUi(this);
c.validateToken();
}
void MainWindow::on_testButton_clicked()
{
UserApi friends;
friends.updateFriendList();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// ...
ui->setupUi(this);
c.validateToken();
}
void MainWindow::on_testButton_clicked()
{
UserApi friends;
friends.updateFriendList();
}
To copy to clipboard, switch view to plain text mode
And an ApiHandler class
apihandler.h
// includes...
{
Q_OBJECT
public:
explicit ApiHandler
(QObject *parent
= 0);
void makeRequest
(QString method, QMap<QString, QString> parameters
);
QNetworkAccessManager* getManager();
private:
QUrl buildCall
(QString method, QMap<QString, QString> parameters
);
QNetworkAccessManager* manager;
signals:
public slots:
void slotReadyRead();
void slotError(QNetworkReply::NetworkError error);
void slotSslErrors(QList<QSslError> errorList);
};
// includes...
class ApiHandler : public QObject
{
Q_OBJECT
public:
explicit ApiHandler(QObject *parent = 0);
void makeRequest(QString method, QMap<QString, QString> parameters);
QNetworkAccessManager* getManager();
private:
QUrl buildCall(QString method, QMap<QString, QString> parameters);
QNetworkAccessManager* manager;
signals:
public slots:
void slotReadyRead();
void slotError(QNetworkReply::NetworkError error);
void slotSslErrors(QList<QSslError> errorList);
};
To copy to clipboard, switch view to plain text mode
apihandler.cpp
// includes...
ApiHandler
::ApiHandler(QObject *parent
) :{
this->manager = new QNetworkAccessManager(this);
}
QUrl ApiHandler
::buildCall(QString method, QMap<QString, QString> parameters
) {
// ...
}
void ApiHandler
::makeRequest(QString method, QMap<QString, QString> parameters
) {
QUrl url
= this
->buildCall
(method, parameters
);
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("User-Agent", "Test Client");
QNetworkReply *reply = this->manager->get(request);
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
}
void ApiHandler::slotError(QNetworkReply::NetworkError error)
{
//...
}
void ApiHandler::slotSslErrors(QList<QSslError> errorList)
{
//...
}
QNetworkAccessManager* ApiHandler::getManager()
{
return this->manager;
}
// includes...
ApiHandler::ApiHandler(QObject *parent) :
QObject(parent)
{
this->manager = new QNetworkAccessManager(this);
}
QUrl ApiHandler::buildCall(QString method, QMap<QString, QString> parameters)
{
// ...
}
void ApiHandler::makeRequest(QString method, QMap<QString, QString> parameters)
{
QUrl url = this->buildCall(method, parameters);
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("User-Agent", "Test Client");
QNetworkReply *reply = this->manager->get(request);
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
}
void ApiHandler::slotError(QNetworkReply::NetworkError error)
{
//...
}
void ApiHandler::slotSslErrors(QList<QSslError> errorList)
{
//...
}
QNetworkAccessManager* ApiHandler::getManager()
{
return this->manager;
}
To copy to clipboard, switch view to plain text mode
Bookmarks