python - “纹理”示例适用于 PySide2,但不适用于 PySide6
问题描述
虽然/的安装附带的原始“纹理”示例[2]与 一起正常工作,但当尝试运行与 相同/常见的示例(也附带)时,它会失败,它会引发一个错误,使其无法运行:PySide2
PySide6
PySide2
PySide6
INFO:OpenGL.acceleratesupport:OpenGL_accelerate module loaded
INFO:OpenGL.arrays.arraydatatype:Using accelerated ArrayDatatype
Traceback (most recent call last):
File "C:\Users\gxous\AppData\Local\Programs\Python\Python39\Lib\site-packages\PySide6\examples\opengl\textures\textures.py", line 62, in <module>
class GLWidget(QtOpenGL.QGLWidget):
AttributeError: module 'PySide6.QtOpenGL' has no attribute 'QGLWidget'
所以我想知道为什么以及是否有任何解决方案。
PS。该示例通常可以在 Windows 上安装 PySide 后的文件夹下找到:%LocalAppData%\Programs\Python\PythonXX\Lib\site-packages\PySideX\examples\opengl\textures
解决方案
我最终不得不做的事情比我最初想象的和预期的要多一些,所以我决定与你分享整个现已修复的示例:
我必须采取的步骤:
- 首先,我在QT 的 Documentaion中发现该
QGLWidget
模块的内容已被一个名为的新模块替换,QtOpenGLWidgets
并且该模块的许多QMouseEvent
功能已被弃用。 - 那么 的
QGLWidget
等价updateGL()
函数QtOpenGlWidgets
就是2。update()
QGLWidget
的功能bindTexture()
必须替换为QtOpenGL.QOpenGLTexture()
.QGLWidget
qglClearColor()
与glClearColor()
. _- 并对原始示例进行一些更改。
现在修复:“textures.py”
重新分发源代码必须保留上述版权声明...
############################################################################
##
## Copyright (C) 2013 Riverbank Computing Limited.
## Copyright (C) 2016 The Qt Company Ltd.
## Contact: http://www.qt.io/licensing/
##
## This file is part of the Qt for Python examples of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:BSD$
## You may use this file under the terms of the BSD license as follows:
##
## "Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are
## met:
## * Redistributions of source code must retain the above copyright
## notice, this list of conditions and the following disclaimer.
## * Redistributions in binary form must reproduce the above copyright
## notice, this list of conditions and the following disclaimer in
## the documentation and/or other materials provided with the
## distribution.
## * Neither the name of The Qt Company Ltd nor the names of its
## contributors may be used to endorse or promote products derived
## from this software without specific prior written permission.
##
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
##
## $QT_END_LICENSE$
##
############################################################################
"""PySide6 port of the opengl/textures example from Qt v5.x"""
import sys
from PySide6 import QtCore, QtGui, QtOpenGLWidgets, QtOpenGL, QtWidgets
try:
from OpenGL.GL import *
except ImportError:
app = QtWidgets.QApplication(sys.argv)
messageBox = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Critical, "OpenGL textures",
"PyOpenGL must be installed to run this example.",
QtWidgets.QMessageBox.Close)
messageBox.setDetailedText("Run:\npip install PyOpenGL PyOpenGL_accelerate")
messageBox.exec()
sys.exit(1)
import textures_rc
class GLWidget(QtOpenGLWidgets.QOpenGLWidget):
sharedObject = 0
refCount = 0
coords = (
( ( +1, -1, -1 ), ( -1, -1, -1 ), ( -1, +1, -1 ), ( +1, +1, -1 ) ),
( ( +1, +1, -1 ), ( -1, +1, -1 ), ( -1, +1, +1 ), ( +1, +1, +1 ) ),
( ( +1, -1, +1 ), ( +1, -1, -1 ), ( +1, +1, -1 ), ( +1, +1, +1 ) ),
( ( -1, -1, -1 ), ( -1, -1, +1 ), ( -1, +1, +1 ), ( -1, +1, -1 ) ),
( ( +1, -1, +1 ), ( -1, -1, +1 ), ( -1, -1, -1 ), ( +1, -1, -1 ) ),
( ( -1, -1, +1 ), ( +1, -1, +1 ), ( +1, +1, +1 ), ( -1, +1, +1 ) )
)
clicked = QtCore.Signal()
def __init__(self, parent, shareWidget):
QtOpenGLWidgets.QOpenGLWidget.__init__(self, parent)
self.clearColor = QtCore.Qt.black
self.xRot = 0
self.yRot = 0
self.zRot = 0
self.clearColor = QtGui.QColor()
self.lastPos = QtCore.QPoint()
def freeGLResources(self):
GLWidget.refCount -= 1
if GLWidget.refCount == 0:
self.makeCurrent()
glDeleteLists(self.__class__.sharedObject, 1)
def minimumSizeHint(self):
return QtCore.QSize(50, 50)
def sizeHint(self):
return QtCore.QSize(200, 200)
def rotateBy(self, xAngle, yAngle, zAngle):
self.xRot = (self.xRot + xAngle) % 5760
self.yRot = (self.yRot + yAngle) % 5760
self.zRot = (self.zRot + zAngle) % 5760
self.update()
def setClearColor(self, color):
self.clearColor = color
self.update()
def initializeGL(self):
if not GLWidget.sharedObject:
self.textures = []
for i in range(6):
self.textures.append(QtOpenGL.QOpenGLTexture(QtGui.QImage(f":/images/side{i + 1}.png")))
GLWidget.sharedObject = self.makeObject()
GLWidget.refCount += 1
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
glEnable(GL_TEXTURE_2D)
def paintGL(self):
glClearColor(self.clearColor.red()/255,self.clearColor.green()/255,self.clearColor.blue()/255,self.clearColor.alpha()/255) #self.clearColor
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslated(0.0, 0.0, -10.0)
glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0)
glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0)
glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0)
glCallList(GLWidget.sharedObject)
def resizeGL(self, width, height):
side = min(width, height)
glViewport(int((width - side) / 2), int((height - side) / 2), side, side)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0)
glMatrixMode(GL_MODELVIEW)
def mousePressEvent(self, event):
self.lastPos = QtCore.QPoint(event.position().toPoint())
def mouseMoveEvent(self, event):
dx = event.position().x() - self.lastPos.x()
dy = event.position().y() - self.lastPos.y()
if event.buttons() & QtCore.Qt.LeftButton:
self.rotateBy(8 * dy, 8 * dx, 0)
elif event.buttons() & QtCore.Qt.RightButton:
self.rotateBy(8 * dy, 0, 8 * dx)
self.lastPos = QtCore.QPoint(event.position().toPoint())
def mouseReleaseEvent(self, event):
self.clicked.emit()
def makeObject(self):
dlist = glGenLists(1)
glNewList(dlist, GL_COMPILE)
for i in range(6):
glBindTexture(GL_TEXTURE_2D,i+2) # #1
glBegin(GL_QUADS)
for j in range(4):
tx = {False: 0, True: 1}[j == 0 or j == 3]
ty = {False: 0, True: 1}[j == 0 or j == 1]
glTexCoord2d(tx, ty)
glVertex3d(0.2 * GLWidget.coords[i][j][0],
0.2 * GLWidget.coords[i][j][1],
0.2 * GLWidget.coords[i][j][2])
glEnd()
glEndList()
return dlist
class Window(QtOpenGLWidgets.QOpenGLWidget):#QtWidgets.QWidget):
NumRows = 2
NumColumns = 3
def __init__(self, parent=None):
QtOpenGLWidgets.QOpenGLWidget.__init__(self, parent) #QtWidgets.QWidget.__init__(self, parent)
mainLayout = QtWidgets.QGridLayout()
self.glWidgets = []
for i in range(Window.NumRows):
self.glWidgets.append([])
for j in range(Window.NumColumns):
self.glWidgets[i].append(None)
for i in range(Window.NumRows):
for j in range(Window.NumColumns):
clearColor = QtGui.QColor()
clearColor.setHsv(((i * Window.NumColumns) + j) * 255
/ (Window.NumRows * Window.NumColumns - 1),
255, 63)
self.glWidgets[i][j] = GLWidget(self, self.glWidgets[0][0])
self.glWidgets[i][j].setClearColor(clearColor)
self.glWidgets[i][j].rotateBy(+42 * 16, +42 * 16, -21 * 16)
mainLayout.addWidget(self.glWidgets[i][j], i, j)
self.glWidgets[i][j].clicked.connect(self.setCurrentGlWidget)
qApp.lastWindowClosed.connect(self.glWidgets[i][j].freeGLResources)
self.setLayout(mainLayout)
self.currentGlWidget = self.glWidgets[0][0]
timer = QtCore.QTimer(self)
timer.timeout.connect(self.rotateOneStep)
timer.start(20)
self.setWindowTitle(self.tr("Textures"))
def setCurrentGlWidget(self):
self.currentGlWidget = self.sender()
def rotateOneStep(self):
if self.currentGlWidget:
self.currentGlWidget.rotateBy(+2 * 16, +2 * 16, -1 * 16)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
奥特罗
尽管这些步骤看起来很简单,但实际上它们并非完全如此,因此我不得不花一整天的时间来弄清楚 [...]
推荐阅读
- html - 下拉菜单要么移动内容,要么不让我居中
- python - 有没有办法用 Python 过滤 JSON 数据
- r - 有没有办法将外部数据库连接到 R 以提取数据
- php - 找到字符串 A 后如何仅匹配字符串 B 的第一次出现
- java - 使用 JOptionPane 在代码末尾添加一条长消息
- twig - Twig 剥离 textarea 并创建数组
- filtering - 为什么在过滤数据集并基于该集合运行回归后得到 NA 值?
- laravel - Laravel Mix:使用 Mix() 在另一个助手周围缓存半身像
- javascript - 如何在 Angular 应用程序中将字符串保存到 Cloud Storage 中的文本文件?
- css - 多个引导模式滚动问题