Results 1 to 4 of 4

Thread: 1 instance of QApplication in my application that uses static libraries

  1. #1
    Join Date
    Dec 2009
    Posts
    52
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 1 instance of QApplication in my application that uses static libraries

    Hello,
    the problem is "QWidget: Must construct a QApplication before a QPaintDevice".
    Please read below

    on win32/vs2008 and linux32/g++4.4,
    I build static libraries and an executable/binary that links against those static libraries.

    I have a "plot" static library( plot.lib or libplot.a ) that includes a main.cpp translation unit.
    This defines a namespace-scope variable

    Qt Code:
    1. namespace NS1 { namespace NS2 {
    2. int argc_for_qapp=0;
    3. QApplication app(argc_for_qapp, 0);
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 

    so the symbol NS1::NS2::app is the variable.

    in another translation unit in "plot", there is

    Qt Code:
    1. namespace NS1 { namespace NS2 {
    2. extern QApplication app;
    3.  
    4. void f()
    5. {
    6. ...
    7. app.exec();
    8. }
    9.  
    10. }}
    To copy to clipboard, switch view to plain text mode 


    There is the application's main.cpp that calls the function f().

    As I understand there is no guarantee of the initialization of globals between translation units, but there is a guarantee that all of them are initialized before the main function.

    In Debug there is no problem, in Release there is the problem:
    "QWidget: Must construct a QApplication before a QPaintDevice"

    In Release vs2008, I made sure the linker options for the executable that includes, in Linker/Optimization/References => "Keep Unreferenced Data"
    Linker/Optimization/Enable COMDAT Folding => "Do Not Remove Redundant COMDATs "

    so the reference to NS1::NS2::app that is in another translation unit is not removed.

    Again in debug mode, this works fine.
    in release/linux32/g++4.4, same error.

    rds,

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: 1 instance of QApplication in my application that uses static libraries

    I don't really understand why you create global QApplication object in your library.
    Can't you do it in normal way, so it is created in main() function in the executable:
    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication a(argc, argv);
    4. // ... some code calling you library functions where widgets are created ...
    5. return a.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 
    ?
    And if you want additional event loop then use QEventLoop in you lib.
    This my main.cpp works unless you want to create a global widget object, which you shouldn't. If you really want you have to create global pointer to QWidget and set it pointing to a widget created on a heap. What's more, it is highly recommended to make all widgets on heap (look into Qt docs).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Dec 2009
    Posts
    52
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: 1 instance of QApplication in my application that uses static libraries

    Thanks.
    Only part of my application uses Qt. It is able to run only in text mode without any graphics.
    I want to keep all Qt related aspects inside 1 static library.
    I may alo use write another static library that provides the same interface but using another toolkit.

    I will use then a namespace-scope QApplication* instead and will initialize it on 1st time of use explicitly.

    thanks,

  4. #4
    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: 1 instance of QApplication in my application that uses static libraries

    You should hide everything in the library implementation. You should provide an abstract class that can be implemented in a number of different ways. One of them can be to use QApplication and the implementation would be responsible for creating the instance of it and calling exec on it, although remember that by doing so you agree to specific semantics - exec() blocks linear code execution and makes your application event-driven.

    As for the error, you probably have a global or static paint device (widget, image?) somewhere. Using the "extern" modifier is probably also wrong, especially that QApplication is a singleton you can call through its instance().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Static application missing pcap functionality
    By remy06 in forum Installation and Deployment
    Replies: 3
    Last Post: 21st May 2012, 19:33
  2. Building application with static Qt libraries
    By nedlab in forum Installation and Deployment
    Replies: 1
    Last Post: 28th December 2008, 21:07
  3. Replies: 16
    Last Post: 23rd May 2008, 11:12
  4. QApplication instance
    By nile.one in forum Qt Programming
    Replies: 9
    Last Post: 5th October 2007, 13:06
  5. Replies: 2
    Last Post: 16th March 2007, 10:04

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.