简体   繁体   中英

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)

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"

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. 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. 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

Python code .ui file code

Save .ui file as test.ui

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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