PyQt5 中包含 3D Window
Contained 3D Window in PyQt5
我正在使用 PyQt5 并尝试将包含的 3D window 实现到从 .ui 文件生成的 python 文件中。我制作了一个继承自 QWidget 的 contained3dWindow class,它只创建了一个 3D window,我正在尝试将包含的 window 添加到主 window 文件中的 VBoxLayout。但是,当我在主 window 文件中创建 contained3dWindow class 的实例时,我收到此错误消息:
Warning: Setting a new default format with a different version or profile after the global shared context is created may cause issues with context sharing.
Failed to make context current: OpenGL resources will not be destroyed
当我运行主文件时,主window显示不到一秒钟,然后程序关闭。这是包含 window 的函数,它在文件 cwindow.py:
中
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QCheckBox, QCommandLinkButton, \
QVBoxLayout
from PyQt5.Qt3DExtras import QTorusMesh, QPhongMaterial, QConeMesh, QCylinderMesh, \
QCuboidMesh, QPlaneMesh, QSphereMesh, Qt3DWindow, QFirstPersonCameraController
import sys
class contained3dWindow(QWidget):
def __init__(self):
super().__init__()
view = Qt3DWindow()
这是 window 主文件中的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import cwindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#Generated by pyuic
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1416, 1041)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(250, 160, 1051, 721))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1416, 38))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#Source of the problem. Error occurs when the contained3dWindow instance is created
c_window = cwindow.contained3dWindow()
self.verticalLayout.addWidget(c_window)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuEdit.setTitle(_translate("MainWindow", "Edit"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
在 setupUi 函数中创建了一个 contained3dWindow 对象,如果未创建该对象,则程序 运行 将正常运行。 PyQt 文档非常薄,我只找到了一个遇到此问题的人 (https://bugreports.qt.io/browse/QTBUG-60614)。非常感谢任何帮助。
我不知道错误的确切原因,我怀疑它与内存释放有关,但下面的代码不会产生那个问题,所以现在我应该解决你的问题
cwindow.py
from PyQt5.QtWidgets import QWidget, QVBoxLayout
from PyQt5.Qt3DExtras import Qt3DWindow, QFirstPersonCameraController
from PyQt5.Qt3DCore import QEntity
class contained3dWindow(QWidget):
def __init__(self):
super().__init__()
lay = QVBoxLayout(self)
self.view = Qt3DWindow()
container = QWidget.createWindowContainer(self.view)
lay.addWidget(container)
self.rootEntity = QEntity()
cameraEntity = self.view.camera()
camController = QFirstPersonCameraController(self.rootEntity)
camController.setCamera(cameraEntity)
self.view.setRootEntity(self.rootEntity)
我正在使用 PyQt5 并尝试将包含的 3D window 实现到从 .ui 文件生成的 python 文件中。我制作了一个继承自 QWidget 的 contained3dWindow class,它只创建了一个 3D window,我正在尝试将包含的 window 添加到主 window 文件中的 VBoxLayout。但是,当我在主 window 文件中创建 contained3dWindow class 的实例时,我收到此错误消息:
Warning: Setting a new default format with a different version or profile after the global shared context is created may cause issues with context sharing. Failed to make context current: OpenGL resources will not be destroyed
当我运行主文件时,主window显示不到一秒钟,然后程序关闭。这是包含 window 的函数,它在文件 cwindow.py:
中from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QCheckBox, QCommandLinkButton, \
QVBoxLayout
from PyQt5.Qt3DExtras import QTorusMesh, QPhongMaterial, QConeMesh, QCylinderMesh, \
QCuboidMesh, QPlaneMesh, QSphereMesh, Qt3DWindow, QFirstPersonCameraController
import sys
class contained3dWindow(QWidget):
def __init__(self):
super().__init__()
view = Qt3DWindow()
这是 window 主文件中的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import cwindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#Generated by pyuic
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1416, 1041)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(250, 160, 1051, 721))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1416, 38))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#Source of the problem. Error occurs when the contained3dWindow instance is created
c_window = cwindow.contained3dWindow()
self.verticalLayout.addWidget(c_window)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuEdit.setTitle(_translate("MainWindow", "Edit"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
在 setupUi 函数中创建了一个 contained3dWindow 对象,如果未创建该对象,则程序 运行 将正常运行。 PyQt 文档非常薄,我只找到了一个遇到此问题的人 (https://bugreports.qt.io/browse/QTBUG-60614)。非常感谢任何帮助。
我不知道错误的确切原因,我怀疑它与内存释放有关,但下面的代码不会产生那个问题,所以现在我应该解决你的问题
cwindow.py
from PyQt5.QtWidgets import QWidget, QVBoxLayout
from PyQt5.Qt3DExtras import Qt3DWindow, QFirstPersonCameraController
from PyQt5.Qt3DCore import QEntity
class contained3dWindow(QWidget):
def __init__(self):
super().__init__()
lay = QVBoxLayout(self)
self.view = Qt3DWindow()
container = QWidget.createWindowContainer(self.view)
lay.addWidget(container)
self.rootEntity = QEntity()
cameraEntity = self.view.camera()
camController = QFirstPersonCameraController(self.rootEntity)
camController.setCamera(cameraEntity)
self.view.setRootEntity(self.rootEntity)