PySide2如何将现有的OpenGL窗口添加到QGLWidget中

问题描述 投票:0回答:1

我目前已经使用glfwpyopengl编写了一个简单的窗口类。现在,我想将其与qt小部件一起使用。

我发现QGLWidget提供了3个重新实现的功能:paintGL, resizeGL, initializeGL。不幸的是,诸如上下文创建之类的东西以及所有图形都是使用glfw和pyopengl创建的。

所以,可以将它与QGLWidget一起使用吗?

这是一个窗口代码:

class Viewport(object):
    def __init__(self, widht, height, title="OpenGL Window", r=0.2, g=0.3, b=0.3, a=1.0):
        super().__init__()
        self.widht = widht
        self.height = height
        self.window_title = title
        self.background_color = (r, g, b, a)

        self.__check_glfw()
        self.window = self.__create_window()


    def main_loop(self):
        while not glfw.window_should_close(self.window):
            self.processEvents(self.window)

            glClearColor(0.2, 0.3, 0.3, 1.0)
            glClear(GL_COLOR_BUFFER_BIT)

            # DO STUFF HERE
            #--------------

            glfw.swap_buffers(self.window)
            glfw.poll_events()
        glfw.terminate()

    def processEvents(self, window):
        if glfw.get_key(window, glfw.KEY_ESCAPE) is glfw.PRESS:
            glfw.set_window_should_close(window, True)

    def __create_window(self):
        window = glfw.create_window(self.widht, self.height, self.window_title, None, None)
        # check if window was created
        if not window:
            glfw.terminate()
            dialog = ExceptionDialog("GLWFError::Cannot initialize window")

        glfw.set_window_pos(window, 400, 200)
        glfw.make_context_current(window)

        return window

    def __check_glfw(self):
        # Initiallize gflw
        if not glfw.init():
            dialog = ExceptionDialog("GLFWError::The GLFW lib cannot initialized")
glfw pyside2 pyopengl
1个回答
0
投票

好吧,看来我不应该使用QGLWidget,这有点过时了。相反,我将使用QOpenGLWidget

class nViewport(QtWidgets.QOpenGLWidget):
    def __init__(self, width, height, title="Qt OpenGl Window", r=0.2, g=0.3, b=0.3, a=1.0):
        super().__init__()
        self.widht = width
        self.height = height
        self.bg_color = (r, g, b, a)

        self.setWindowTitle(title)
        self.resize(self.widht, self.height)

    def initializeGL(self):
        pass

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glClearColor(self.bg_color[0], self.bg_color[1],
                 self.bg_color[2], self.bg_color[3])

    def resizeGL(self, w:int, h:int):
        glViewport(0, 0, w, h)

    def keyPressEvent(self, event: QtGui.QKeyEvent):
        if event.key() == QtCore.Qt.Key_Escape:
            app.exit()
        event.accept()

    def printDebugInfo(self):
        print(f"QT_OPENGL_WIDGET::{self.__class__.__name__}")
        print("------------------------------>")
        print(f"INFO::GL_VERSION::{glGetString(GL_VERSION)}")
        print("------------------------------>\n")

也可以设置OpenGL版本和其他功能,这些通常是我用GLFW来完成的,我用所需的设置重新实现了QSurfaceFormat类。

class GLSurfaceFormat(QtGui.QSurfaceFormat):
    def __init__(self, major: int = 4, minor: int = 3,
             profile: QtGui.QSurfaceFormat.OpenGLContextProfile = QtGui.QSurfaceFormat.CoreProfile,
             color_space: QtGui.QSurfaceFormat.ColorSpace = QtGui.QSurfaceFormat.sRGBColorSpace):
        super().__init__()
        self.gl_major = major
        self.gl_minor = minor
        self.gl_profile = profile
        self.color_space = color_space

        self.__initSurface()

    def __initSurface(self):
        self.setRenderableType(QtGui.QSurfaceFormat.OpenGL)
        self.setMajorVersion(self.gl_major)
        self.setMinorVersion(self.gl_minor)
        self.setProfile(self.gl_profile)
        self.setColorSpace(self.color_space)
        # You can change it to TripleBuffer if your platform supports it
        self.setSwapBehavior(QtGui.QSurfaceFormat.DoubleBuffer)

之后,在主循环中的OpenGLWidget之前初始化表面

© www.soinside.com 2019 - 2024. All rights reserved.