在非 GUI 环境中使用 QPainter

Using QPainter in non-GUI environment

在非 GUI 环境中使用 QPainter 可能会有用 f.e。在(网络)服务器上生成 PDF。

但是 QPainter 依赖于 QGuiApplication,这会导致运行时错误:

qt.qpa.screen: QXcbConnection: Could not connect to display Could not connect to any X display.

如何在非 GUI 环境中使用 QPainter(没有可用的输出屏幕)?

您应该使用以下命令行参数启动您的应用程序:-platform minimal:

minimal is provided as an examples for developers who want to write their own platform plugins. However, you can use the plugin to run GUI applications in environments without a GUI, such as servers.

注意 -platform offscreen 似乎需要支持字体,即绘制文本,否则会显示黑色方块而不是实际文本。

进一步阅读和参考