Results 1 to 6 of 6

Thread: qApp null if QCoreApplication defined in shared library

  1. #1
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default qApp null if QCoreApplication defined in shared library

    If I do:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication app(argc,argv);
    4. qDebug() << "qApp=" << qApp;
    5. ....
    6. }
    To copy to clipboard, switch view to plain text mode 

    The of course qApp is not null. But if define MyCoreApplication in a shared library (dll on windows), and I do


    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. MyCoreApplication app(argc,argv);
    4. qDebug() << "qApp=" << qApp;
    5. ....
    6. }
    To copy to clipboard, switch view to plain text mode 

    Then qApp is NULL! That is not what I would expect. The problem is that I have qApp->applicationDirPath() calls all over the place, and (qApp->setProperty()), so I can't affort to not have access to qApp in the main app and in the shared lib.

    If I do this hack...

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. MyCoreApplication app(argc,argv);
    4. QCoreApplication app2(argc,argv);
    5. qDebug() << "qApp=" << qApp;
    6. ....
    7. }
    To copy to clipboard, switch view to plain text mode 

    Then it works... however, I suspect I now have two versions of qApp, which is okay for qApp->applicationDirPath(), but not good for setProperty() on qApp.

    So, is this the expected behavior (even for a shared (non-static) link to a dll)? If so, then what's a good work around.

    Thanks!

  2. #2
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qApp null if QCoreApplication defined in shared library

    Well I suppose the answer is that QCoreApplication::instance() is static, and is therefore not shared between shared libraries. However, it still raises the question of how to best share the qApp with shared libraries. One can pass it as a parameter, but it gets messy. I have a workaround for my particular application, but I wonder if there is a concensus way to handle this.

  3. #3
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qApp null if QCoreApplication defined in shared library

    I don't think what I am seeing is the expected behavior.... shouldn't the qApp automatically point to the same instance of QCoreApplication whether I am in the main program or a shared library? I found several posts to indicate that this should be the case. But I'm the only one commenting on this thread... would appreciate some help, as I am STUCK!

    Shouldn't I be able to define "QCoreApplication app;" in the main.cpp and then access that via qApp in a shared library? (Kind of the opposite problem as outlined above) Well that doesn't work either!

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: qApp null if QCoreApplication defined in shared library

    Why do you want to create an instance on QCoreApplication in a shared library?

    I believe all you need would be few static calls, which can be always used without instance.

    I suggest to not to use
    Qt Code:
    1. qApp->applicationDirPath()
    To copy to clipboard, switch view to plain text mode 
    , instead use
    Qt Code:
    1. QCoreApplication::applicationDirPath()
    To copy to clipboard, switch view to plain text mode 
    further you can get the instance using
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    then you can use
    Qt Code:
    1. app->setProperty(..);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qApp null if QCoreApplication defined in shared library

    Thanks Santosh. But this is precisely my problem... that static functions (including QCoreApplication::applicationDirPath(), QCoreApplication::arguments(), QCoreApplication:rocessEvents()) do not seem to work in a shared a library, if the QCoreApplication was instantiated in the main().

    I really think this is a fundamental issue, and there most be a work around.. but I haven't been able to find it.

  6. #6
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qApp null if QCoreApplication defined in shared library

    Turns out that the shared library was created in debug mode, and the main executable was in release. This was apparently the SILENT cause of many terrible problems with static variables.

    If somebody would have just said something like: "No, magland, that is not the expected behavior, qApp should work perfectly in shared libraries", then it would have saved me a lot of time. Anyway, that's the way it goes.

    If somebody comes across this thread... be sure to specify CONFIG += release everywhere!

Similar Threads

  1. QCoreApplication::postEvent: Unexpected null receiver
    By mortoray in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2010, 22:14
  2. QCoreApplication... In a library.
    By hickscorp in forum Qt Programming
    Replies: 5
    Last Post: 23rd January 2010, 16:29
  3. how to use shared library
    By assismvla in forum Qt Programming
    Replies: 3
    Last Post: 25th September 2009, 21:29
  4. Qt/Mac Shared Library
    By nareshqt in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2008, 07:21
  5. QCoreApplication::postEvent: Unexpected null receiver
    By merlvingian in forum Qt Programming
    Replies: 6
    Last Post: 13th March 2007, 20:25

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.