0基础小白关于如何在pyqt5的ui窗口中内嵌一个OpenGL窗口的方法的若干摸索。
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
loadUi('opengl2.ui', self)
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
运行出来长这样,那怎么在里面画东西呢?
天知道。困难顺利,永远都有;因为现在脑子里都是空气,所以现在的大环境是困难多顺利少,困难大于顺利……
好像把这个变为现实啊,可惜,不会啊……想放弃了。
问:为什么人有超越性?
答:正是在自我超越的过程中,人找到了做人的意义,人之所以为人。(这个是论文里看的,觉得说的挺有道理的)
相比较而存在,相斗争而发展。
要实现超越就必须要斗争呐。
整体上藐视它——OpenGL套壳GUI,不是什么千年未解的数学问题。
局部上重视它——第一画界面,第二学学怎么写界面逻辑,第三学学相关背景知识。都很重要,都要一步一步学,质量互变,pyqt就那么多函数,日积月累,总会学会的;只是时间来不来得及的问题……
百度了很久,勉强实现了:
步骤1——画ui
步骤2——写代码,确定内嵌的OpenGL里显示的内容
fromUiLoadOpengl.py的一部分
class MyGLWidget(QOpenGLWidget):#通过子例化,实现自定义
def __init__(self, parent=None):
super(MyGLWidget, self).__init__(parent)
def initializeGL(self):
version_profile = QOpenGLVersionProfile()
version_profile.setVersion(2, 0)
self.gl = self.context().versionFunctions(version_profile)
self.gl.initializeOpenGLFunctions()
#设置背景色
self.gl.glClearColor(0.2, 0.4, 0.52, 1.0)
#深度测试
self.gl.glEnable(self.gl.GL_DEPTH_TEST)
def paintGL(self):
self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT | self.gl.GL_DEPTH_BUFFER_BIT)
self.gl.glLoadIdentity()
#self.gl.glRotated(60.0, 1.0, 0.0, 0.0)
self.gl.glBegin(self.gl.GL_TRIANGLES)
self.gl.glColor3d(1.0, 0.0, 0.0)
self.gl.glVertex3d(0.0, 1.0, 0.0)
self.gl.glColor3d(0.0, 1.0, 0.0)
self.gl.glVertex3d(-1.0, -1.0, 0.0)
self.gl.glColor3d(0.0, 0.0, 1.0)
self.gl.glVertex3d(1.0, -1.0, 0.0)
self.gl.glEnd()
self.gl.glBegin(self.gl.GL_LINES)
self.gl.glVertex3d(-1.5,-1.5,0)
self.gl.glVertex3d(1.5,1.5,0)
self.gl.glEnd()
def resizeGL(self, width, height):
side = min(width, height)
if side < 0:
return
#视口
self.gl.glViewport((width - side) // 2, (height - side) // 2, side, side)
self.gl.glMatrixMode(self.gl.GL_PROJECTION)
self.gl.glLoadIdentity()
#正交投射
self.gl.glOrtho(-1.5, 1.5, -1.5, 1.5, -10, 10)
self.gl.glMatrixMode(self.gl.GL_MODELVIEW)
步骤3——设置一下提升
步骤4——补全其余代码
fromUiLoadOpengl.py的全部内容如下:
from cgi import test
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow,QOpenGLWidget
from PyQt5.uic import loadUi
from PyQt5.QtGui import QColor, QOpenGLVersionProfile
class MyGLWidget(QOpenGLWidget):#通过子例化,实现自定义
def __init__(self, parent=None):
super(MyGLWidget, self).__init__(parent)
def initializeGL(self):
version_profile = QOpenGLVersionProfile()
version_profile.setVersion(2, 0)
self.gl = self.context().versionFunctions(version_profile)
self.gl.initializeOpenGLFunctions()
#设置背景色
self.gl.glClearColor(0.2, 0.4, 0.52, 1.0)
#深度测试
self.gl.glEnable(self.gl.GL_DEPTH_TEST)
def paintGL(self):
self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT | self.gl.GL_DEPTH_BUFFER_BIT)
self.gl.glLoadIdentity()
#self.gl.glRotated(60.0, 1.0, 0.0, 0.0)
self.gl.glBegin(self.gl.GL_TRIANGLES)
self.gl.glColor3d(1.0, 0.0, 0.0)
self.gl.glVertex3d(0.0, 1.0, 0.0)
self.gl.glColor3d(0.0, 1.0, 0.0)
self.gl.glVertex3d(-1.0, -1.0, 0.0)
self.gl.glColor3d(0.0, 0.0, 1.0)
self.gl.glVertex3d(1.0, -1.0, 0.0)
self.gl.glEnd()
self.gl.glBegin(self.gl.GL_LINES)
self.gl.glVertex3d(-1.5,-1.5,0)
self.gl.glVertex3d(1.5,1.5,0)
self.gl.glEnd()
def resizeGL(self, width, height):
side = min(width, height)
if side < 0:
return
#视口
self.gl.glViewport((width - side) // 2, (height - side) // 2, side, side)
self.gl.glMatrixMode(self.gl.GL_PROJECTION)
self.gl.glLoadIdentity()
#正交投射
self.gl.glOrtho(-1.5, 1.5, -1.5, 1.5, -10, 10)
self.gl.glMatrixMode(self.gl.GL_MODELVIEW)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()#super()构造器方法返回父级的对象。
self.ui=loadUi("opengl.ui")#加载ui
app = QApplication(sys.argv)
w = MainWindow()
w.ui.show()
sys.exit(app.exec())
运行结果
在这个问题上卡了两天后,终于解决了。虽然这个真的不算啥,但是对我这种0基础的小小白来说,有那么一刹那的时间,还是比较激动的,为什么呢?因为劳动创造幸福,上网百度也算脑力劳动的一种嘛。
C++版的内嵌OpenGL
https://blog.csdn.net/markfuk996/article/details/115052239?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&utm_relevant_index=2https://blog.csdn.net/markfuk996/article/details/115052239?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_default&utm_relevant_index=2QT设计师提升的具体操作方法:
https://blog.csdn.net/g11023225/article/details/119274622?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164113024416780271976430%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164113024416780271976430&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-119274622.pc_search_insert_es_download_v2&utm_term=pyqt5%E6%8F%90%E5%8D%87%E7%AA%97%E5%8F%A3%E9%83%A8%E4%BB%B6&spm=1018.2226.3001.4187https://blog.csdn.net/g11023225/article/details/119274622?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164113024416780271976430%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164113024416780271976430&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-119274622.pc_search_insert_es_download_v2&utm_term=pyqt5%E6%8F%90%E5%8D%87%E7%AA%97%E5%8F%A3%E9%83%A8%E4%BB%B6&spm=1018.2226.3001.4187实现了一点过去实现不了的东西。也是一个小小的超越。人都有点超越性嘛。
但是离实用还差着十万八千里,再说吧~
内嵌的OpenGL窗口怎么用,现在已经知道了,那末,菜单栏怎么用?
https://blog.csdn.net/seniorwizard/article/details/109820641?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164852214216780269861393%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164852214216780269861393&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-109820641.142^v5^pc_search_insert_es_download,143^v6^register&utm_term=pyqt5%E7%9A%84qaction%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A&spm=1018.2226.3001.4187https://blog.csdn.net/seniorwizard/article/details/109820641?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164852214216780269861393%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164852214216780269861393&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-109820641.142%5Ev5%5Epc_search_insert_es_download,143%5Ev6%5Eregister&utm_term=pyqt5%E7%9A%84qaction%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A&spm=1018.2226.3001.4187关于类的函数的self参数的解释:
https://blog.csdn.net/bing900713/article/details/60884931https://blog.csdn.net/bing900713/article/details/60884931知道这么多,下面的就好理解了:
第一,画ui,画窗口。窗口的选项,对应QAction类的一个对象。
from cgi import test
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow,QOpenGLWidget
from PyQt5.uic import loadUi
from PyQt5.QtGui import QColor, QOpenGLVersionProfile
class MyGLWidget(QOpenGLWidget):#通过子例化,实现自定义
def __init__(self, parent=None):
super(MyGLWidget, self).__init__(parent)
def initializeGL(self):
version_profile = QOpenGLVersionProfile()
version_profile.setVersion(2, 0)
self.gl = self.context().versionFunctions(version_profile)
self.gl.initializeOpenGLFunctions()
#设置背景色
self.gl.glClearColor(0.2, 0.4, 0.52, 1.0)
#深度测试
self.gl.glEnable(self.gl.GL_DEPTH_TEST)
def paintGL(self):
self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT | self.gl.GL_DEPTH_BUFFER_BIT)
self.gl.glLoadIdentity()
#self.gl.glRotated(60.0, 1.0, 0.0, 0.0)
self.gl.glBegin(self.gl.GL_TRIANGLES)
self.gl.glColor3d(1.0, 0.0, 0.0)
self.gl.glVertex3d(0.0, 1.0, 0.0)
self.gl.glColor3d(0.0, 1.0, 0.0)
self.gl.glVertex3d(-1.0, -1.0, 0.0)
self.gl.glColor3d(0.0, 0.0, 1.0)
self.gl.glVertex3d(1.0, -1.0, 0.0)
self.gl.glEnd()
self.gl.glBegin(self.gl.GL_LINES)
self.gl.glVertex3d(-1.5,-1.5,0)
self.gl.glVertex3d(1.5,1.5,0)
self.gl.glEnd()
def resizeGL(self, width, height):
side = min(width, height)
if side < 0:
return
#视口
self.gl.glViewport((width - side) // 2, (height - side) // 2, side, side)
self.gl.glMatrixMode(self.gl.GL_PROJECTION)
self.gl.glLoadIdentity()
#正交投射
self.gl.glOrtho(-1.5, 1.5, -1.5, 1.5, -10, 10)
self.gl.glMatrixMode(self.gl.GL_MODELVIEW)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()#super()构造器方法返回父级的对象。
self.ui=loadUi("opengl.ui")#加载ui
self.ui.actiontest.triggered.connect(self.onTest)#测试菜单栏选项的槽函数
#def onTest():
def onTest(self):
print("菜单栏测试选项被选中")
app = QApplication(sys.argv)
w = MainWindow()
w.ui.show()
sys.exit(app.exec())
运行结果如下:
pyQT里怎么才能加上shader呢?再说吧……后话。
不行就C++的QT+shader。解决问题从来不止一条路的。