Results 1 to 7 of 7

Thread: Qt-based dll: qApp = 0 !!

  1. #1
    Join Date
    Jan 2006
    Posts
    30
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Qt-based dll: qApp = 0 !!

    I've created Qt-based gui dll and it works good if is used with shared version of Qt. But when I try to use static Qt, qApp in dll becames 0 !.. Dll creates QApplication only if application doesn't have its own (I do it in special fuction init()). But in the case of static Qt qApp inside dll is always 0, even of app owns QApplication instance!.. What is it? strange bug or bad feature ?

  2. #2
    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: Qt-based dll: qApp = 0 !!

    I guess it might be a normal behaviour with static libraries. Maybe you could still use QCoreApplication::instance()?

  3. #3
    Join Date
    Jan 2006
    Posts
    30
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Qt-based dll: qApp = 0 !!

    Quote Originally Posted by wysota
    I guess it might be a normal behaviour with static libraries.
    But why? I just can't understand... ;(

    Maybe you could still use QCoreApplication::instance()?
    afaik qApp is just a macro for QCoreApplication::instance(). All I probably need is to give to the dll a pointer to the QApplication and somehow make QApplication::instance() inside dll to return a pointer to application's QApplication.. I've tried to undef and define qApp to return that pointer but hasn't succeed .. Maybe there is a way to use 2 instance of QApplication - one in application, other in dll ?

  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: Qt-based dll: qApp = 0 !!

    No. The problem is probably due to the fact that the library is STATIC. It doesn't have access to QCoreApplication::instance() present in the main application, because the latter uses a relocated library (meaning it resolves the symbols during dynamic link time) and the static library's QCoreApplication::instance() points to the method (and all pointers it uses) which is present inside the library code (as it is statically linked -- it only knows a permanent address of the method). It has no way of reaching any code (be it a class, a method or a global pointer) from the dynamic code -- there are two "QCoreApplication::instance()" methods in the application and each of them returns a different pointer. There is no way of getting around this but to use some relocable (dynamic) code in the "plugin" to "reach" into proper QCoreApplication::instance() -- the dynamic code has to be linked against the dynamic QtCore library or should at least be able to dlopen() it and return a pointer to its QCoreApplication::instance() method which you could then call. Due to the fact that it would reach into the same dynamic library, the pointer would be the same and you would get a correct value, pointing to the right QCoreApplication instance. At least in theory.

  5. #5
    Join Date
    Jan 2006
    Posts
    30
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Qt-based dll: qApp = 0 !!

    Quote Originally Posted by wysota
    No. The problem is probably due to the fact that the library is STATIC. It doesn't have access to QCoreApplication::instance() present in the main application, because the latter uses a relocated library (meaning it resolves the symbols during dynamic link time) and the static library's QCoreApplication::instance() points to the method (and all pointers it uses) which is present inside the library code (as it is statically linked -- it only knows a permanent address of the method). It has no way of reaching any code (be it a class, a method or a global pointer) from the dynamic code -- there are two "QCoreApplication::instance()" methods in the application and each of them returns a different pointer. There is no way of getting around this but to use some relocable (dynamic) code in the "plugin" to "reach" into proper QCoreApplication::instance() -- the dynamic code has to be linked against the dynamic QtCore library or should at least be able to dlopen() it and return a pointer to its QCoreApplication::instance() method which you could then call. Due to the fact that it would reach into the same dynamic library, the pointer would be the same and you would get a correct value, pointing to the right QCoreApplication instance. At least in theory.
    Thank you for explanation! Now I see that not all is so easy as it looks .. I'll try above approach if I understood it right and if my last crazy idea hasn't succeed. (I've thought about making custom QApplication which instance() method will return pointer which I pass from main app to dll)

  6. #6
    Join Date
    Jul 2009
    Posts
    3

    Default Re: Qt-based dll: qApp = 0 !!

    Hello,

    I know that this Thread is quite old, but I have the same problem at the moment.
    Does anyone have a solution for that issue?

  7. #7
    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: Qt-based dll: qApp = 0 !!

    The solution is to not use static linking.
    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. how to use qApp inside a dll?
    By oob2 in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2006, 22:15
  2. Sorting Tree items based on database properties
    By jnk5y in forum Qt Programming
    Replies: 3
    Last Post: 13th April 2006, 19:44
  3. Is there a Pointer Based QMap or Similar
    By vasudhamirji in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 14:34
  4. Finding QObjcects in a qApp
    By rianquinn in forum Qt Programming
    Replies: 4
    Last Post: 6th February 2006, 12:16

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.