QXcbConnection: Could not connect to display, when

2019-03-03 20:10发布

I am trying to port a set of Windows applications that were built with Qt Creator to Linux. I have installed Qt Creator 4.0.2 based on Qt 5.7.0 in a 64-bit Ubuntu 16.04 virtual machine using VirtualBox. The host PC is 64-bit Windows 7 Professional.

I have successfully built one of the application projects and I am now trying to debug it. If I try to start debugging with F5, Qt insists on trying to compile one of the projects that does not yet compile so I am using "Start and Debug External Application...".

The application immediately crashes in main() at the line:

QApplication app(argc, argv);

with the error: QXcbConnection: Could not connect to display.

If I run the executable from a terminal window, the UI is displayed and the application functions normally.

What do I need to do to be able to debug a UI application with Qt.

2条回答
唯我独甜
2楼-- · 2019-03-03 20:29

When you deploy your application and run it from within the X11 system on device itself, you're finding it works because here it has access to the DISPLAY environment variable which (briefly put) tells it where to display itself. This environment variable has been set further up the process tree in your X session.

When you launch a program via the debugger, Qt Creator is connecting to the remote device (via your settings in Tools > Options > Devices), and running the program via ssh. In this context your program no longer knows where to display itself, as obviously it can't display in ssh. It faults on the line you indicated because this is where the XCB subsystem is trying to determine which X-Server to connect to for this purpose.

So to answer your question: you need to manually provide the DISPLAY environment variable when remote debugging.

An easy way to test this is to go to Projects Mode, find the kit you are using to build for the remote device, and select the Run settings. Under this, you should find an Run Environment section. Here you can add a new variable called DISPLAY and set its value to the identifier of the display you are running on (I'm guessing you'll want :0.0, indicating the first available screen on localhost, although you should read about the DISPLAY variable e.g. here or here).

A longer-term, potentially better solution would be to set the same variable in the settings of your Kit (Tools > Options > Build & Run > Kits > Environment). This will then apply for future programs you create using this.

查看更多
淡お忘
3楼-- · 2019-03-03 20:40

Turns out that all I had to do was check the "Run in terminal:" check box in the "Start Debugger" dialog window.

查看更多
登录 后发表回答