使用 Docker 使用 QOffscreenSurface 进行离屏渲染

Offscreen render with QOffscreenSurface using Docker

我正在使用 Qt5 进行离屏渲染,当 运行在 Docker 内时出现分段错误。

我将代码放在 c++ 中,并使用 pybind11

通过 python 调用函数

这是导致段错误的代码

QSurfaceFormat glFormat;
glFormat.setVersion(3, 3);
glFormat.setProfile(QSurfaceFormat::CoreProfile);
glFormat.setRenderableType(QSurfaceFormat::OpenGL);

surface = new QOffscreenSurface();
surface->setFormat(glFormat);
surface->create(); // <-- Here 

准确的错误是:

QObject::connect: Cannot connect (null)::destroyed(QObject*) to QOffscreenSurface::screenDestroyed(QObject*) 

我试图通过 xvfb-run -a python prg.py 运行 它并得到同样的错误

有任何调试建议吗?

问题的更多上下文,如果我在包裹在 QApplication 中的主线程中使用上面的代码,它就可以正常工作。只有当我将它移动到一个函数中以便我可以在 python 中调用它时才会出现此问题。

关于 运行 在主线程之外的最后一条线索可能就是你 运行 遇到的问题。

正在查看文档 https://doc.qt.io/qt-5/qoffscreensurface.html#details:

Note: Due to the fact that QOffscreenSurface is backed by a QWindow on some platforms, cross-platform applications must ensure that create() is only called on the main (GUI) thread. The QOffscreenSurface is then safe to be used with makeCurrent() on other threads, but the initialization and destruction must always happen on the main (GUI) thread.

所以解决方案可能是在主线程中创建表面,然后通过 pybind11 像您需要的那样使用它。