简体   繁体   English

如何在PyOpenGL中使用PyQt5 .ui

[英]How to use PyQt5 .ui with PyOpenGL

如果我在Qt Designer中创建了接口,如何在python应用程序中使用Qt的OpenGLWidget?

You should have installed PyOpenGL, PyQt5, Qt5 editor (we don't need IDE, but we need Qt Designer that comes with it) 您应该已经安装了PyOpenGL,PyQt5,Qt5编辑器(我们不需要IDE,但需要它附带的Qt Designer)。

Using QtDesigner create .ui file with all necessary controls and OpenGL widget (name widgets appropriately, because in python you will use this name to access widget's objects) Name this file test.ui and place it in the same directory as you .py file You OpenGL widget has to be named "openGLWidget" 使用QtDesigner创建具有所有必要控件和OpenGL小部件的.ui文件(适当地命名小部件,因为在python中,您将使用此名称访问小部件的对象)将此文件命名为test.ui并将其放置在与.py文件相同的目录中OpenGL小部件必须命名为“ openGLWidget”

import OpenGL.GL as gl
import OpenGL.GLU as glu
import OpenGL.GLUT as glut
from PyQt5 import QtWidgets as qWidget
from PyQt5 import QtGui as qGui
from PyQt5 import QtCore as qCore
from PyQt5 import uic
import sys
import os


class mainWindow(qWidget.QMainWindow):
    """Main window class."""

    def __init__(self, *args):
        """Init."""
        super(mainWindow, self).__init__(*args)
        ui = os.path.join(os.path.dirname(__file__), 'test.ui')
        uic.loadUi(ui, self)

    def setupUI(self):
        print("\033[1;101m SETU6P UI \033[0m")
        self.windowsHeight = self.openGLWidget.height()
        self.windowsWidth = self.openGLWidget.width()

        self.openGLWidget.initializeGL()
        self.openGLWidget.resizeGL(self.windowsWidth, self.windowsHeight)
        self.openGLWidget.paintGL = self.paintGL
        self.openGLWidget.initializeGL = self.initializeGL

    def paintGL(self):
        self.loadScene()
        glut.glutWireSphere(2, 13, 13)

    def initializeGL(self):
        print("\033[4;30;102m INITIALIZE GL \033[0m")
        gl.glEnable(gl.GL_BLEND)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
        gl.glEnable(gl.GL_DEPTH_TEST)

    def loadScene(self):
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glLoadIdentity()
        x, y, width, height = gl.glGetDoublev(gl.GL_VIEWPORT)
        glu.gluPerspective(
            45,  # field of view in degrees
            width / float(height or 1),  # aspect ratio
            .25,  # near clipping plane
            200,  # far clipping plane
        )

        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glLoadIdentity()

        glu.gluLookAt(12, 12, 12, 0, 0, 0, 0, 1, 0)


app = qWidget.QApplication(sys.argv)
window = mainWindow()
window.setupUI()
window.show()
sys.exit(app.exec_())

At this moment you should see GUI windows with all elements you placed and openGLwidget with white sphere displayed in the center Now you can use all OpenGl instrumentary to create your own 3D application. 此时,您应该看到GUI窗口,其中包含所有放置的元素,以及带有白色球体的openGLwidget显示在中间,现在您可以使用所有OpenGl工具来创建自己的3D应用程序。 For mouse input and interactions you can use Widget functions from Qt5 If you need to use Qt5 event you should use "on_widgetName_clicked" for defining new function in mainWindow class. 对于鼠标输入和交互,可以使用Qt5中的Widget函数。如果需要使用Qt5事件,则应使用“ on_widgetName_clicked”在mainWindow类中定义新函数。 It will act whenever this event is occured in GUI PyOpenGl functionality is 100% accesible in this template, and you can use tutorials dedicated for default version (C/C++) as well without any issues 只要此事件在GUI中发生,PyOpenGl的功能就可以在此模板中100%访问,它将起作用,并且您也可以使用默认版本(C / C ++)专用的教程。

Python code .ui file code Python代码 .ui文件代码

Save .ui file as test.ui 将.ui文件另存为test.ui

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM