Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:01:27+00:00 2026-06-07T00:01:27+00:00

I have a MainWindow application I’m working on to learn C++ and Qt (C++

  • 0

I have a MainWindow application I’m working on to learn C++ and Qt (C++ and QT 4.8).
I want to do HTTP requests in different objects of my application, such as Dialogs/Wizard and in the MainWindow. I know I’m basically supposed to have one QNetworkAccessManager per application. My question is, what is the proper way to pass this QNAM around between classes?

At the moment I have it as a pointer I pass to the constructor of my wizard but this seems… inelegant and inflexible. What is the proper way to give my Dialogs or whatever other classes I decide to make, access to my one QNetworkAccessManager? I guess I have the same question about any piece of data I need to give everything access to.

What is the properly C++-designed solution here? The singleton pattern seems like an option, but a bad one as I understand. I have a bit of code here to show my question.

My MainWindow constructor and slot which launch my wizard:

MyMainWindow::MyMainWindow
{
    qnam = new QNetworkAccessManager();
}

...

MyMainWindow::wizardStarter
{
    mywizard = MyWizard(vari, qnam, this);
}

My Wizard Constructor in which I’m making network requests and parsing data after getting data from the user, and therefore in which I need a QNetworkAccessManager:

MyWizard::MyWizard(SomeOtherArgument *vari, QNetworkAccessManager *qnam, QObject *parent)
{
    ...
    this->ourQnam = qnam;
    ...
}

MyWizard::launchRequest(QUrl newUrl)
{
    ourQnam->get(QNetworkRequest(newUrl));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:01:32+00:00Added an answer on June 7, 2026 at 12:01 am

    From your question, I think you’re really asking which form of dependency injection (i.e. injecting your dependent QNetworkAccessManager into objects) to use.

    In your case you’re using Constructor injection. This is a perfectly known and accepted form of injection. It strongly communicates your wizard class depends upon the QNetworkAccessManager which makes your code easy for people to understand. If you were to use a singleton to simply grab a static QNetworkAccessManager from inside the wizard class implementation, while it has the benefit of removing the constructor injection, it hides that your wizard class uses the QNetworkAccessManager.

    Another well known form of injection is Setter Injection i.e. setDelegate( delegate )

    Professionally speaking there is nothing wrong with your current approach as again it clearly communicates your wizard class depends upon the QNetworkAccessManager object.

    Here is a bit of reading if you if you’re interested in learning more about dependency injection.

    Dependency Injection

    Dependency Injection in C++

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF-application with a mainwindow and different pages. On one of the
Guys, I have a basic WPF application. Contains App.xaml as always and a Mainwindow.xaml.
I have MainWindow with a button, under the button click event I want MainWindow
I have the following code: <Window x:Class=kkk.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350 Width=525> <Window.Resources> <Style
i have a window which is like this <Window x:Class=pharmacy_Concept.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350
I have a Silverlight application that consists of a MainWindow and several classes which
I have MainWindow.xib in the application. In the Interface Builder, I rotate the main
I have a application which has 6 count down timers. I want the application
I have a WPF application with the main Window class called MainWindow. Since I
In WPF application I have MainWindow : Window which loads a POSView : UserControl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.