QQuickFrameBufferObject 导致 PySide2 崩溃
QQuickFrameBufferObject causes crash in PySide2
我正在尝试创建一个简单的 QQuickFrameBufferObject
以便在 Qt Quick 中渲染一些自定义 opengl 纹理。所以我决定用 PySide2 测试一下。这是一个简单的实现:
from PySide2.QtQuick import QQuickFramebufferObject
from PySide2.QtGui import QOpenGLFramebufferObjectFormat, QOpenGLFramebufferObject
from PySide2.QtCore import QSize, Qt
class FboRenderer(QQuickFramebufferObject.Renderer):
def __init__(self, parent=None):
super().__init__(parent)
print("Creating renderer")
def createFrameBufferObject(self, size):
format = QOpenGLFramebufferObjectFormat()
format.setAttachment(QOpenGLFramebufferObject.Depth)
return QOpenGLFramebufferObject(size, format)
def synchronize(self, item):
print("Synchronizing")
def render(self):
print("Rendering")
class OpenGLCanvas(QQuickFramebufferObject):
def __init__(self, parent=None):
super().__init__(parent)
def createRenderer(self):
return FboRenderer()
然后为了在 QML 中使用它,我使用了:
qmlRegisterType(OpenGLCanvas,"OpenGLCanvas", 1, 0, "OpenGLCanvas")
import QtQuick 2.12
import QtQuick.Window 2.12
import OpenGLCanvas 1.0
Window{
id: win
visible: true
OpenGLCanvas{
anchors.fill: parent
}
}
现在应用程序运行并显示 "Creating renderer" 但几秒钟后它崩溃了。
问题是FboRenderer()
是一个局部变量,会被立即销毁,因此它也会销毁C++对象,导致段错误,因为未保留内存已访问。
另一方面,你在 createFrameBufferObject
中有一个错字,因为它必须是 createFramebufferObject
,这也揭示了一个类似于初始错误的错误,为此解决方案是相似的。
class FboRenderer(QQuickFramebufferObject.Renderer):
def __init__(self):
super().__init__()
print("Creating renderer")
self._fbos = []
def createFramebufferObject(self, size):
fmt = QOpenGLFramebufferObjectFormat()
fmt.setAttachment(QOpenGLFramebufferObject.Depth)
fbo = QOpenGLFramebufferObject(size, fmt)
self._fbos.append(fbo)
return fbo
def synchronize(self, item):
print("Synchronizing")
def render(self):
print("Rendering")
class OpenGLCanvas(QQuickFramebufferObject):
def __init__(self, parent=None):
super().__init__(parent)
self._renderer = None
def createRenderer(self):
if self._renderer is None:
self.renderer = FboRenderer()
return self.renderer
我正在尝试创建一个简单的 QQuickFrameBufferObject
以便在 Qt Quick 中渲染一些自定义 opengl 纹理。所以我决定用 PySide2 测试一下。这是一个简单的实现:
from PySide2.QtQuick import QQuickFramebufferObject
from PySide2.QtGui import QOpenGLFramebufferObjectFormat, QOpenGLFramebufferObject
from PySide2.QtCore import QSize, Qt
class FboRenderer(QQuickFramebufferObject.Renderer):
def __init__(self, parent=None):
super().__init__(parent)
print("Creating renderer")
def createFrameBufferObject(self, size):
format = QOpenGLFramebufferObjectFormat()
format.setAttachment(QOpenGLFramebufferObject.Depth)
return QOpenGLFramebufferObject(size, format)
def synchronize(self, item):
print("Synchronizing")
def render(self):
print("Rendering")
class OpenGLCanvas(QQuickFramebufferObject):
def __init__(self, parent=None):
super().__init__(parent)
def createRenderer(self):
return FboRenderer()
然后为了在 QML 中使用它,我使用了:
qmlRegisterType(OpenGLCanvas,"OpenGLCanvas", 1, 0, "OpenGLCanvas")
import QtQuick 2.12
import QtQuick.Window 2.12
import OpenGLCanvas 1.0
Window{
id: win
visible: true
OpenGLCanvas{
anchors.fill: parent
}
}
现在应用程序运行并显示 "Creating renderer" 但几秒钟后它崩溃了。
问题是FboRenderer()
是一个局部变量,会被立即销毁,因此它也会销毁C++对象,导致段错误,因为未保留内存已访问。
另一方面,你在 createFrameBufferObject
中有一个错字,因为它必须是 createFramebufferObject
,这也揭示了一个类似于初始错误的错误,为此解决方案是相似的。
class FboRenderer(QQuickFramebufferObject.Renderer):
def __init__(self):
super().__init__()
print("Creating renderer")
self._fbos = []
def createFramebufferObject(self, size):
fmt = QOpenGLFramebufferObjectFormat()
fmt.setAttachment(QOpenGLFramebufferObject.Depth)
fbo = QOpenGLFramebufferObject(size, fmt)
self._fbos.append(fbo)
return fbo
def synchronize(self, item):
print("Synchronizing")
def render(self):
print("Rendering")
class OpenGLCanvas(QQuickFramebufferObject):
def __init__(self, parent=None):
super().__init__(parent)
self._renderer = None
def createRenderer(self):
if self._renderer is None:
self.renderer = FboRenderer()
return self.renderer