Results 1 to 10 of 10

Thread: QApplication instance

  1. #1
    Join Date
    Sep 2007
    Posts
    19
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question QApplication instance

    I'm developing some Qt project.
    I have the common Qt application (exe). and it attaches plugin - Qt library (dll).
    Both are GUI.
    So i have to create QApplication instances both in exe and dll.

    Because there must be only one QApplication object, I've tried to create second instance in QThread.
    But when I create second instance of QApplication in dll, this causes error
    Qt Code:
    1. ASSERT failure in QCoreApplication: "there should be only one application object"
    To copy to clipboard, switch view to plain text mode 

    So i tried to check if qApp exists and create instance of QApplication only if it's null.
    So then i have error
    Qt Code:
    1. ASSERT failure in QWidget: "Widgets must be created in the GUI thread."
    To copy to clipboard, switch view to plain text mode 

    Does anybody know how to solve this problem?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QApplication instance

    You might have to slightly revise the architecture. You should not instantiate a QApplication in a library/plugin at all. What is the reason for doing so?
    J-P Nurmi

  3. #3
    Join Date
    Sep 2007
    Posts
    19
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Re: QApplication instance

    I need to implement Qt widgets in plugin. So using widgets requires Qt GUI thread. So the problem is I can't put my widget to GUI thread without creating QApplication

    In addition, I get it in debug mode: in qthread's run() I create local instance of QApplication and destroy it after thread working end. But in release i have messages in output:
    Qt Code:
    1. QObject::startTimer: timers cannot be started from another thread
    2. QObject::killTimer: timers cannot be stopped from another thread
    To copy to clipboard, switch view to plain text mode 
    and after closing the thread application exits itself
    Qt Code:
    1. The thread 'Win32 Thread' (0x9d4) has exited with code 0 (0x0).
    2. QWidget: Must construct a QApplication before a QPaintDevice
    3. The thread 'Win32 Thread' (0xc8c) has exited with code 1 (0x1).
    4. The thread 'Win32 Thread' (0x778) has exited with code 1 (0x1).
    5. The thread 'Win32 Thread' (0xc78) has exited with code 1 (0x1).
    6. The program '[2740] deposits_repayment.exe: Native' has exited with code 1 (0x1).
    To copy to clipboard, switch view to plain text mode 
    Last edited by nile.one; 5th October 2007 at 11:31.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QApplication instance

    Use the QApplication from the application that loads the plugins.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QApplication instance

    Why not attach to the application QApplication object? It's available as a global pointer qApp and furthermore you can change the plugin interface so that either the application object pointer is passed to the plugin or a pointer to the widget created in the plugin is passed back to the application object and then it takes control over the newly created widget (that's what Qt Designer does by the way).

  6. The following user says thank you to wysota for this useful post:

    nile.one (5th October 2007)

  7. #6
    Join Date
    Sep 2007
    Posts
    19
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QApplication instance

    Yes, of course, I can get qApp pointer. But I don't know how to pass it to the QWidget.
    Could you give me an example, please?
    Simple instantiating QWidget with having qApp causing error
    Qt Code:
    1. ASSERT failure in QWidget: "Widgets must be created in the GUI thread."
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Sep 2007
    Posts
    19
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QApplication instance

    There is code block of main QThread thread in plugin where i'm trying to instantiate QWidget (QDialog)

    I understand, that code is incorrect and even more maybe stupid, so I need help
    Qt Code:
    1. void MainThread::run()
    2. {
    3. if (!qApp)
    4. {
    5. int argc=0;
    6. char** argv=0;
    7. QApplication a(argc, argv);
    8. a.setQuitOnLastWindowClosed(true);
    9.  
    10. Gui gui;
    11. gui.show();
    12. gui.job();
    13. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    14. a.exec();
    15. }
    16. else
    17. {
    18. Gui gui;
    19. gui.show();
    20. gui.job();
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QApplication instance

    But it is not clear what are you're trying to do...
    Why are you starting an event loop in a plugin?
    The plugin should use the host applciation's event loop, not create its own.

    Ant what's with the thread?

  10. The following user says thank you to marcel for this useful post:

    nile.one (5th October 2007)

  11. #9
    Join Date
    Sep 2007
    Posts
    19
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QApplication instance

    in the example i get error
    ASSERT failure in QWidget: "Widgets must be created in the GUI thread."

    So, what's the solution? not to implement GUI in thread or what?
    But if i want to implement plugin's GUI in independent thread in case of not blocking the application while long-time work of plugin.

  12. #10
    Join Date
    Sep 2007
    Posts
    19
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Lightbulb Re: QApplication instance

    ok, finally i got it working. i've chosen the solution to run the Qt GUI not in independent thread, i'll use the threads only in core classes.

Similar Threads

  1. Calling QApplication from a Non-QT app?
    By hickscorp in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2007, 23:00
  2. Replies: 2
    Last Post: 16th March 2007, 10:04
  3. qapplication with open(/dev/name,O_RDWR) crashed???
    By ttbug in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 16th September 2006, 08:56
  4. Reponsabilities of QApplication and QMainWindow classes
    By yellowmat in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2006, 17:21
  5. creating a global instance
    By skatakam in forum General Programming
    Replies: 6
    Last Post: 24th February 2006, 17:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.