Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkAccessManager singleton class example
Forum Updated to NodeBB v4.3 + New Features

QNetworkAccessManager singleton class example

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hiren21
    wrote on last edited by
    #1

    I am new in both qt and c++, can someone give me example of QNetworkAccessManager singleton class ?

    raven-worxR 1 Reply Last reply
    0
    • H hiren21

      I am new in both qt and c++, can someone give me example of QNetworkAccessManager singleton class ?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @hiren21
      actually any singleton example on the web is applicable since the pattern is independant of the type, but here you go:

      QNetworkAccessManager* getNAM() {
          static QNetworkAccessManager* nam = new QNetworkAccessManager;
          return nam;
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      4
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        .h file

        class QNAMwrapper{
        static QNetworkAccessManager* m_qnam;
        public:
        static QNetworkAccessManager* getQNAM() {
        Q_ASSERT_X(m_qnam,"QNAMwrapper","You must create an instance of QNAMwrapper before using the QNAM");
        return m_qnam;
        }
        QNAMwrapper(){
        Q_ASSERT_X(!m_qnam,"QNAMwrapper","Only one instance of QNAMwrapper can be created");
        if(!m_qnam) m_qnam=new QNetworkAccessManager;
        }
        ~QNAMwrapper(){delete m_qnam; m_qnam=Q_NULLPTR;}
        };
        

        cpp file

        QNetworkAccessManager* QNAMwrapper::m_qnam = Q_NULLPTR;
        

        This works like QApplication and handles the lifecycle of QNetworkAccessManager. So in your main add QNAMwrapper qnamw; and then you can use the QNetworkAccessManager via QNAMwrapper::getQNAM();

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved