There is a similar question but I did not find it useful for my problem.
In Qt documentation, they say:
One QNetworkAccessManager instance should be enough for the whole Qt application.
In my application, I was using QNetworkAccessManager
in several places (they may be called simultaneously) and each time I was creating new instance on the stack. After reading that quote I changed my code to have one static QNetworkAccessManager
and using it everywhere. After changing it to static member, I always get a warning:
QObject::connect: Cannot connect (null)::aboutToQuit() to QNativeWifiEngine::closeHandle()
In both cases, the code is working without errors, however, that documentation is confusing me a little bit. As the product is going to be commercial, I am taking this issue serious. Should I follow a documentation or avoid a warning? Or do you suggest any other ways?
What if an object on a different thread needs to use a QNetworkAccessManager
?
EDIT: adding code
Singleton settings class:
class ConnectionSettingsSingleton
{
...
// constructors = default
// copy constructor = delete
public:
static QNetworkAccessManager networkAccessManager;
}
I use networkAccessManager
in different places, but the same way:
QNetworkReply* HttpClient::makeRequest()
{
switch (this->method) {
case RequestMethod::GET:
return ConnectionSettingsSingleton::networkAccessManager.get(this->serverRequest);
case RequestMethod::POST:
return ConnectionSettingsSingleton::networkAccessManager.post(this->serverRequest, QJsonDocument(this->data).toJson());
case RequestMethod::DELETE:
return ConnectionSettingsSingleton::networkAccessManager.deleteResource(this->serverRequest);
default:
return nullptr;
}
}
EDIT-2
I was kindly using Kuba Ober's answer and at one point I got this warning:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0x26f6d0), parent's thread is QThread(0x2b73b8), current thread is QThread(0xa4f20a8)
Reason: I tried to access QNAM
from a different thread. Accordingly, I updated my question.