catch that new QApplication failed and try something else

134 views Asked by At

I have two programs. One call SelectScreen and another called RadioPanel.

SelectScreen sends a message to RadioPanel telling it what screen its supposed to display the gui on.

RadioPanel uses setenv("DISPLAY", myHostslist[hostId].c_str(),true); to set the DISPLAY environment variable. Then a mQtApplication = new QApplication(mArgc, mArgv); to start the gui.

This works as long as the host info is correct. However if it is not correct QApplication causes the program to end. What I want to do is catch the fact that QApplication failed and try to run the gui on ":0"

I tried using a try catch block but it will not catch. My guess is that QApplication just ends the process and does not throw an exception in this case.

Does anyone know if there is a way to determine if QApplication is going to fail or respond to that failure.

The message I get from QApplication when it fails is:

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, xcb.```
1

There are 1 answers

0
skalloz On

I found a solution. If you use XOpenDisplay you can check the return to confirm that the X Server display is working before you attempt to create the QApplication.

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>

void myUiApplication::qtGuiThread()
{
    Display *dis;
    dis=XOpenDisplay((char *)0);
    if(dis!=nullptr)
    {
        XCloseDisplay(dis);
        mQtApplication = new QApplication(mArgc, mArgv);
    } else 
    {
        CCS_ERR("Failed to display on host:" << cds::toString(mCdsId) << " " 
                << mCdsHosts[mCdsId].c_str() << ".\nPlease edit the " 
                << getConfigFileName() << " file. Attempting to run GUI on local X Server.");
        setenv("DISPLAY",":0",true);
        mQtApplication = new QApplication(mArgc, mArgv);
    }
}