在Pyqt窗口中使用glfw窗口 [英] Using glfw window inside Pyqt Window

查看:243
本文介绍了在Pyqt窗口中使用glfw窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用QT设计器设计的用户界面,并使用pyqt4进行了转换.在此用户界面中,我有表格,标签和更多内容...

I have a user interface that I designed using QT designer, and converted using pyqt4. In this user interface I have tables, tabs and more...

我还要在该用户界面内添加一个glfw窗口,该窗口将与这些表进行交互并绘制一些3D对象.因此,我将在此glfw窗口中使用pyopengl.我知道如何在单独的窗口中执行此操作,但是该窗口必须位于其中.有什么办法吗?

I would like to also add a glfw window inside that user interface that will interact those tables and draw some 3D objects. So i will use pyopengl in this glfw window. I am aware how to do this in seperate window, but this window has to be inside that. Is there any way to do this ?

谢谢

推荐答案

Qt具有用于OpenGL上下文的内置窗口小部件,称为 answer 应该可以帮助您正常运行.如果您已经编写了3D可视化部分,则可能需要进行一些重组,但是,由于要使用此小部件,因此您需要实现某些功能,例如initializeGLpaintGL.

Qt has a builtin widget for an OpenGL context called a QGLWidget with which you can use your typical PyOpenGL commands. You might want to try using that instead of shoehorning in a GLFW context into your app. This answer should help you get that running. It might require some restructuring if you already have the 3D visualization part written, however, since to use this widget, you need to implement certain functions, such as initializeGL and paintGL.

关于注释中有关处理鼠标事件以启用颜色选择的问题,其技巧是认识到QGLWidget继承自QWidget.这意味着您可以访问可以实现的功能,例如mousePressEvent.这是我编写的简洁演示(带有一些有用的评论),它告诉用户单击的位置以及该像素的颜色:

As for your question in the comments about processing mouse events to enable color picking, the trick for that is to realize that QGLWidget inherits from QWidget. This means that you have access to functions like mousePressEvent which you can implement. Here is a neat demo (with some helpful comments) that I wrote up that tells the user where they clicked and the color of that pixel:

from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt4 import QtGui
from PyQt4.QtGui import QColor, QStatusBar, QSizePolicy
from PyQt4.QtOpenGL import *
import sys

class MainWindow(QtGui.QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.widget = GLWidget(self)
        self.statusbar = QStatusBar()
        self.statusbar.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.statusbar.showMessage("Click anywhere on the QGLWidget to see a pixel's RGBA value!")
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.widget)
        layout.addWidget(self.statusbar)
        layout.setContentsMargins(5, 5, 5, 5)
        self.setLayout(layout)

class GLWidget(QGLWidget):

    def __init__(self, parent):
        QGLWidget.__init__(self, parent)
        self.setMinimumSize(640, 480)
        #LMB = left mouse button
        #True: fires mouseMoveEvents even when not holding down LMB
        #False: only fire mouseMoveEvents when holding down LMB
        self.setMouseTracking(False)

    def initializeGL(self):
        glClearColor(0, 0, 0, 1)
        glClearDepth(1.0)
        glEnable(GL_DEPTH_TEST)

    def resizeGL(self, width, height):
        #glViewport is needed for proper resizing of QGLWidget
        glViewport(0, 0, width, height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(0, width, 0, height, -1, 1)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

    def paintGL(self):
        #Renders a triangle... obvious (and deprecated!) stuff
        w, h = self.width(), self.height()
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glBegin(GL_TRIANGLES)
        glColor3f(1, 0, 0)
        glVertex3f(0, 0, 0)
        glColor3f(0, 1, 0)
        glVertex3f(w/2.0, h, 0)
        glColor3f(0, 0, 1)
        glVertex3f(w, 0, 0)
        glEnd()

    def mousePressEvent(self, event):
        x, y = event.x(), event.y()
        w, h = self.width(), self.height()
        #required to call this to force PyQt to read from the correct, updated buffer 
        #see issue noted by @BjkOcean in comments!!!
        glReadBuffer(GL_FRONT)
        data = self.grabFrameBuffer()#builtin function that calls glReadPixels internally
        data.save("test.png")
        rgba = QColor(data.pixel(x, y)).getRgb()#gets the appropriate pixel data as an RGBA tuple
        message = "You selected pixel ({0}, {1}) with an RGBA value of {2}.".format(x, y, rgba)
        statusbar = self.parent().statusbar#goes to the parent widget (main window QWidget) and gets its statusbar widget
        statusbar.showMessage(message)

    def mouseMoveEvent(self, event):
        pass

    def mouseReleaseEvent(self, event):
        pass

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.setWindowTitle("Color Picker Demo")
    window.show()
    app.exec_()

结果如下:

这篇关于在Pyqt窗口中使用glfw窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆