C++ Qt 5.1 没有显示器的 QApplication - QXcbConnection:无法连接到显示器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17979185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt 5.1 QApplication Without Display - QXcbConnection: Could not connect to display
提问by Vincent
I'm using Qt5.1 and I'm trying to create a QApplication without a display. I need to draw text with QPainter, so I need to use QApplication (or QGuiApplication), otherwise I get a segfault.
我正在使用 Qt5.1,我正在尝试创建一个没有显示的 QApplication。我需要用 QPainter 绘制文本,所以我需要使用 QApplication(或 QGuiApplication),否则我会出现段错误。
The application worked fine in Qt4.8, but fails in Qt5.1 on a headless version of Ubuntu with the error:
该应用程序在 Qt4.8 中运行良好,但在无头版本的 Ubuntu 上的 Qt5.1 中失败并出现错误:
"QXcbConnection: Could not connect to display".
“QXcbConnection:无法连接到显示器”。
In Qt 4.8, I was able to use the following constructor with GUIenabled = false to create a QApplication that did not require a display:
在 Qt 4.8 中,我能够使用以下带有 GUIenabled = false 的构造函数来创建不需要显示的 QApplication:
QApplication::QApplication ( int & argc, char ** argv, bool GUIenabled )
QApplication::QApplication ( int & argc, char ** argv, bool GUIenabled )
In Qt5.1, the constructor for QApplication no longer has the GUIenabled flag.
在 Qt5.1 中,QApplication 的构造函数不再具有 GUIenabled 标志。
I scanned the source code briefly, and there does seem to be a flag in the QApplication constructor, but it is undocumented as to what options can be used in that flag. Using "false" does not work.
我简要地扫描了源代码,在 QApplication 构造函数中似乎确实有一个标志,但是没有记录可以在该标志中使用哪些选项。使用“false”不起作用。
How can I create a QApplication without a display? Is there an alternative method to telling QApplication GUIenabled = false? Alternatively, can I create a QCoreApplication that will not segfault when drawing text with QPainter on a QImage?
如何在没有显示器的情况下创建 QApplication?是否有其他方法可以告诉 QApplication GUIenabled = false?或者,我可以创建一个在 QImage 上使用 QPainter 绘制文本时不会出现段错误的 QCoreApplication 吗?
回答by peppe
Yes, that's a Qt 3 (?) thing that is gone in Qt 5. Try running your application with the -platform offscreen
command line option instead.
是的,这是 Qt 3 (?) 的东西,在 Qt 5 中消失了。尝试使用-platform offscreen
命令行选项运行您的应用程序。
Note that you don't need QApplication
or linking to QtWidgets
to just draw upon a QImage
, using QGuiApplication
(and linking to QtGui
) is sufficient.
请注意,您不需要QApplication
或链接到QtWidgets
只需绘制QImage
,使用QGuiApplication
(并链接到QtGui
)就足够了。
回答by Pavel Strakhov
If you want to create an app without GUI, you need to use QCoreApplication
instead of QApplication
.
如果你想创建一个没有GUI的应用程序,你需要使用QCoreApplication
的替代QApplication
。