My program can run on a server with no GUI, or on a desktop. When it runs on a system that can display GUIs I want to instantiate QApplication, and when it is on a server I want QCoreApplication.
If I instanciate QApplication on a server, it either Segfault (at least it used to), or display an error message and exit, without letting me the chance to instantiate QCoreApplication instead:
This application failed to start because it could not find or load the Qt platform plugin "xcb". Available platform plugins are: linuxfb, minimal, offscreen. Reinstalling the application may fix this problem.
Seriously ?
Currently I just pass a -noGui argument when I run my program on a server. It works fine, but I want to detect if the system can use QApplication or not, so I can get rid of this argument.
I am sure there is already an answer to that somewhere, but I can't get a hand on it.
Just in case anyone ever wonder how I solved this issue, I intercept the SIGABRT signal sent by QApplication, and instantiate QCoreApplication instead. It works surprisingly well, and it is cross-platform.
#include <QApplication>
#include <csetjmp>
#include <csignal>
#include <cstdlib>
jmp_buf env;
void onSigabrt(int)
{
longjmp (env, 1);
}
QCoreApplication *loadQt(bool gui)
{
QCoreApplication *application = NULL;
if (gui)
{
if (setjmp(env) == 0)
{
signal(SIGABRT, &onSigabrt);
application = new QApplication();
}
signal(SIGABRT, SIG_DFL);
}
if (!application)
application = new QCoreApplication();
return (application);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With