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. More than one instance of QApplication?

More than one instance of QApplication?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.9k 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.
  • A Offline
    A Offline
    akelux
    wrote on last edited by
    #1

    According to the API doc, one can not create more than one instance of QApplication.
    However, the following code snippet shows one can have more than one instances
    of QApplication.
    @
    QApplication app(argc, argv);
    QApplication *pApp = new QApplication(argc, argv);
    qDebug() << (pApp == &app); // false
    qDebug() << (QApplication::instance() == &app); // false
    qDebug() << (QApplication::instance() == pApp); // true
    qDebug() << (QApplication::instance() == qApp); // true
    @

    Actually, QApplication::instance() always refers to the latest created instance.

    Why don't make QApplication a real "singleton"?

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      The documentation is correct -- if you create more than one object yourself, you get undefined behaviour (like what you're seeing).

      QApplication is a subclass of QGuiApplication, which is a subclass of QCoreApplication. qApp is a QCoreApplication pointer.

      Singletons and subclassing don't mix nicely -- QApplication's constructor will also construct a QGuiApplication and a QCoreApplication behind the scenes. Now we have 3 "objects", not a singleton.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        Hi, I think you should get a compile error if your project is in Debug mode.

        1 Reply Last reply
        0

        • Login

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