Determine if the system can create an instance of QApplication (GUI support)

My program can run on a server without a GUI or on the desktop. When it runs on a system that can display graphical interfaces, I want to create a QApplication instance, and when it is on the server, I want QCoreApplication.

If I initiate QApplication on the server, it is either Segfault (at least it used) or it displays an error message and shuts down, preventing me from creating a QCoreApplication instance instead:

This application could not be started because it could not find or download the Qt platform plugin "xcb". Platform plugins available: linuxfb, minimal, off-screen. Reinstalling the application may fix this problem.

Seriously?

Currently, I just pass the -noGui argument when I run my program on the server. It works fine, but I want to determine if the system can use QApplication or not, so I can get rid of this argument.

I am sure that there is already an answer to something, but I can’t work with him.

+4
source share
1 answer

Just in case, when someone wonders how I solved this problem, I intercept the SIGABRT signal sent by QApplication and instead create an instance of QCoreApplication. 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);
}
+3
source

All Articles