qApp null if QCoreApplication defined in shared library
If I do:
Code:
int main(int argc, char *argv[])
{
qDebug() << "qApp=" << qApp;
....
}
The of course qApp is not null. But if define MyCoreApplication in a shared library (dll on windows), and I do
Code:
int main(int argc, char *argv[])
{
MyCoreApplication app(argc,argv);
qDebug() << "qApp=" << qApp;
....
}
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...
Code:
int main(int argc, char *argv[])
{
MyCoreApplication app(argc,argv);
qDebug() << "qApp=" << qApp;
....
}
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!
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.
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!
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
Code:
qApp->applicationDirPath()
, instead use further you can get the instance using then you can use
Code:
app->setProperty(..);
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::processEvents()) 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.
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!