I have a MainWindow application that I am working on to learn C ++ and Qt (C ++ and QT 4.8). I want to make HTTP requests in different objects of my application, such as Dialogs / Wizard and MainWindow. I know that basically I have to have one QNetworkAccessManager for each application. My question is how to properly pass this QNAM between classes?
At the moment I have it as a pointer, I turn to the designer of my wizard, but it seems ... inelegant and inflexible. What is the correct way to give my dialogs or any other classes that I decided to make, access my one QNetworkAccessManager? I probably have the same question about any data that I need to provide to everyone.
What is a properly designed C ++ solution? A singleton pattern seems to be an option, but bad, as I understand it. I have some code to show my question.
My MainWindow constructor and the slot that launches my wizard:
MyMainWindow::MyMainWindow { qnam = new QNetworkAccessManager(); } ... MyMainWindow::wizardStarter { mywizard = MyWizard(vari, qnam, this); }
My master constructor in which I make network requests and analyzing data after receiving data from the user, and therefore in which I need QNetworkAccessManager:
MyWizard::MyWizard(SomeOtherArgument *vari, QNetworkAccessManager *qnam, QObject *parent) { ... this->ourQnam = qnam; ... } MyWizard::launchRequest(QUrl newUrl) { ourQnam->get(QNetworkRequest(newUrl)); }
Mjboa source share