QNetworkAccessManager doesn't send signals
-
When I call fromGoogle it never goes to the finished slot.
Both connects return true, I've tried capturing QNetworkReply::readyRead and finished, they don't go to the slot either. I don't know if the signal is being send or not, I only know that it's not going in the connected slot.
My question is why is the signal not being received in the slots?
This is Qt 6.1.3networkmanager.cpp
NetworkManager::NetworkManager(IAnswerer* answerer) { manager = new QNetworkAccessManager(); connect(manager, &QNetworkAccessManager::finished, this, &NetworkManager::finished); connect(this, &NetworkManager::parsed, answerer, &IAnswerer::networkAnswer); } NetworkManager::~NetworkManager() { delete manager; } int NetworkManager::fromGoogle(QByteArray question) { manager->get(QNetworkRequest(QUrl(question))); return 0; } void NetworkManager::finished(QNetworkReply *reply) { qDebug() << "finished"; emit parsed(reply->readAll().mid(50, 20)); }
networkmanager.h
class NetworkManager : public QObject { Q_OBJECT private: QNetworkAccessManager *manager; public: explicit NetworkManager(IAnswerer * answerer); ~NetworkManager(); int fromGoogle(QByteArray question); public slots: void finished(QNetworkReply *reply); signals: void parsed(QByteArray answer); };
answerer.h
class Answerer : IAnswerer { NetworkManager netManager; ... public: Answerer(QSettings &settings); ... public slots: void networkAnswer(QByteArray answer);
Constructor in the source file:
Answerer::Answerer(QSettings &_settings) : settings(_settings), netManager(this) { ... }
ianswerer.h
class IAnswerer : public QObject { Q_OBJECT public slots: virtual void networkAnswer(QByteArray answer) = 0; };
-
Hi and welcome to devnet,
You should also connect the error related signals in case something goes wrong.
-
@SGaist , I forgot the mention that I already did that as well, and they didn't receive anything also.
-
@thecatishere said in QNetworkAccessManager doesn't send signals:
Answerer
How / where do you instantiate it?
And creating a new QNetworkManager for every request is not good at all.
-
@thecatishere said in QNetworkAccessManager doesn't send signals:
Answerer
How / where do you instantiate it?
And creating a new QNetworkManager for every request is not good at all.
@Christian-Ehrlicher The manager is created in the constructor of NetworkManager, which is a single object instantiated in the constructor of Answerer. Answerer is again a single object that doesn't get deleted throughout the whole program runtime.
reader.hclass Reader { Answerer answerer; ...
reader.cpp
Reader::Reader() : answerer(settings) { ... }
main.cpp
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Reader reader; return a.exec(); }
-
Maybe you should try with a simple objects which calls QNetworkAccessManager to see if it works correct then. Then we also have a chance to reproduce your problem.
-
You do not show how you actually use it. From what you posted it looks like do not actually make any request.
-
You do not show how you actually use it. From what you posted it looks like do not actually make any request.
@SGaist I've created an example project and copy pasted the code from the networkmanager and it works there. I use it by calling netManager.fromGoogle(QByteArray(url)) in Answerer.
Edit:
I tried calling the NetworkManager in the main function before the initialization of Reader.
When I delete 'Reader reader;' it works, otherwise it doesn't. Reader class has nothing to do with the NetworkManager. -
Well, as already written, with the bits you posted we can't determine what is going wrong as the code looks correct.
So if possible please provide a minimal compilable sample that shows your issue.