Reputation: 413
I'm using PyQt5 and trying to implement a contained 3D window into a python file generated from a .ui file. I made a contained3dWindow class which inherits from QWidget that just creates a 3D window, and I am trying to add the contained window to a VBoxLayout in the main window file. But, when I create an instance of my contained3dWindow class in my main window file, I get this error message:
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
When I run the main file, the main window displays for less than a second, and the program closes. This is the function for the contained window, which is in the file 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()
This is the code in the main window file:
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_())
A contained3dWindow object is created in the setupUi function, and the program runs as expected if the object is not created. The PyQt documentation is very slim, and I only found one other person who had this issue (https://bugreports.qt.io/browse/QTBUG-60614). Any help is greatly appreciated.
Upvotes: 3
Views: 2813
Reputation: 243975
I do not know exactly what is the cause of the error, I suspect it has to do with the release of memory but the following code does not generate that problem, so for now I should solve your problem
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)
Upvotes: 4