Slot connect/disconnect in network application
-
Hello!
I have an network (basically chat) application in development.
App have ApiHandler class which handles api requests. And other classes which interacts and update views after specific request has finished.
I "dynamically" connect and disconnect slots for each specific request. But only first will called.
Example of usage:void Friends::getFriends() { ApiHandler* a = new ApiHandler(); connect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*))); a->makeRequest("getFriends", m); // m - parameters }getFriendsFinished
void Friends::getFriendsFinished(QNetworkReply* reply) { // Process and update view... // Cleanup reply->deleteLater(); ApiHandler* a = new ApiHandler(); // Disconnects a slot disconnect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*))); }But when i start another request from different classes, new connected slots never called. What is the best way to do this?
-
Hello!
I have an network (basically chat) application in development.
App have ApiHandler class which handles api requests. And other classes which interacts and update views after specific request has finished.
I "dynamically" connect and disconnect slots for each specific request. But only first will called.
Example of usage:void Friends::getFriends() { ApiHandler* a = new ApiHandler(); connect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*))); a->makeRequest("getFriends", m); // m - parameters }getFriendsFinished
void Friends::getFriendsFinished(QNetworkReply* reply) { // Process and update view... // Cleanup reply->deleteLater(); ApiHandler* a = new ApiHandler(); // Disconnects a slot disconnect(a->getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*))); }But when i start another request from different classes, new connected slots never called. What is the best way to do this?
Hi! In your connect and disconnect statements, you're not using the same ApiHandler instance but two different ones. You need to store
aas a member variable ofFriendsand use only that one. Something like e.g.:class Friend : { // ... private: ApiHandler m_apiHandler; }; // ... void Friends::getFriends() { connect(m_apiHandler.getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*))); // ... void Friends::getFriendsFinished(QNetworkReply* reply) { disconnect(m_apiHandler.getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*))); -
Hi! In your connect and disconnect statements, you're not using the same ApiHandler instance but two different ones. You need to store
aas a member variable ofFriendsand use only that one. Something like e.g.:class Friend : { // ... private: ApiHandler m_apiHandler; }; // ... void Friends::getFriends() { connect(m_apiHandler.getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*))); // ... void Friends::getFriendsFinished(QNetworkReply* reply) { disconnect(m_apiHandler.getManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(getFriendsFinished(QNetworkReply*)));@Wieland thanks for reply! Already tried, but slots not called anyway. Only first connection for one time.
Here the ApiHandler classclass ApiHandler : public QObject { Q_OBJECT public: explicit ApiHandler(QObject *parent = 0); QNetworkReply* makeRequest(QString method, QMap<QString, QString> parameters); QNetworkAccessManager* getManager() { static QNetworkAccessManager* nam = new QNetworkAccessManager; return nam; } private: QUrl buildCall(QString method, QMap<QString, QString> parameters); signals: public slots: void replyFinished(QNetworkReply* reply); void slotReadyRead(); void slotError(QNetworkReply::NetworkError error); void slotSslErrors(QList<QSslError> errorList); };Here the Friends class
class Friends : public QObject { Q_OBJECT public: explicit Friends(QObject *parent = 0); void getFriends(); signals: public slots: void getFriendsFinished(QNetworkReply* reply); };And here the usage
Friends friends; friends.getFriends();