没有显示器的Qt 5.1 QApplication-QXcbConnection:无法连接到显示器 [英] Qt 5.1 QApplication Without Display - QXcbConnection: Could not connect to display
问题描述
我正在使用Qt5.1,并且试图创建一个没有显示的QApplication。我需要使用QPainter绘制文本,所以我需要使用QApplication(或QGuiApplication),否则会出现段错误。
该应用程序在Qt4.8中运行良好,但是在无头版本的Ubuntu上的Qt5.1中失败,并显示以下错误:
QXcbConnection:无法连接到显示器。
在Qt 4.8中,我能够使用以下带有GUIenabled = false的构造函数来创建不需要显示的QApplication:
QApplication :: QApplication(int& argc,char ** argv,bool GUIenabled)
在Qt5.1中,QApplication的构造函数不再具有GUIenabled标志。
我简短地扫描了源代码,的确在QApplication构造函数中确实是一个标志,但是关于可以在该标志中使用哪些选项的信息尚未记录。使用 false不起作用。
如何在不显示的情况下创建QApplication?有没有其他方法可以告诉QApplication GUIenabled = false?或者,我可以创建在QImage上使用QPainter绘制文本时不会出现段错误的QCoreApplication吗?
是的,这是Qt 3(?)在Qt 5中消失了。请尝试使用 -platform offscreen
命令行选项运行应用程序。
请注意,您不需要 QApplication
或链接到 QtWidgets
即可使用 QImage
,使用 QGuiApplication
(并链接到 QtGui
)就足够了。 / p>
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.
The application worked fine in Qt4.8, but fails in Qt5.1 on a headless version of Ubuntu with the error:
"QXcbConnection: Could not connect to display".
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:
QApplication::QApplication ( int & argc, char ** argv, bool GUIenabled )
In Qt5.1, the constructor for QApplication no longer has the GUIenabled flag.
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.
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?
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.
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.
这篇关于没有显示器的Qt 5.1 QApplication-QXcbConnection:无法连接到显示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!