Commit 9e838ccf authored by David Gobbi's avatar David Gobbi Committed by Kitware Robot
Browse files

Merge topic 'chenjt2001-pyside6'

290a029c Link to PySide6 documentation
2566bb7c

 Add support for PySide6
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: David Gobbi's avatarDavid Gobbi <david.gobbi@gmail.com>
Merge-request: !8310
parents be97e00d 290a029c
Pipeline #243649 failed with stages
in 183 minutes and 4 seconds
......@@ -45,6 +45,9 @@ Changes by Tobias Hänel, Sep. 2018
Changes by Ruben de Bruin, Aug. 2019
Fixes to the keyPressEvent function
Changes by Chen Jintao, Aug. 2021
Support for PySide6
"""
# Check whether a specific PyQt implementation was chosen
......@@ -55,7 +58,9 @@ except ImportError:
pass
# Check whether a specific QVTKRenderWindowInteractor base
# class was chosen, can be set to "QGLWidget"
# class was chosen, can be set to "QGLWidget" in
# PyQt implementation version lower than Pyside6,
# or "QOpenGLWidget" in Pyside6
QVTKRWIBase = "QWidget"
try:
import vtkmodules.qt
......@@ -69,24 +74,50 @@ from vtkmodules.vtkRenderingUI import vtkGenericRenderWindowInteractor
if PyQtImpl is None:
# Autodetect the PyQt implementation to use
try:
import PyQt5
PyQtImpl = "PyQt5"
import PySide6
PyQtImpl = "PySide6"
except ImportError:
try:
import PySide2
PyQtImpl = "PySide2"
import PyQt5
PyQtImpl = "PyQt5"
except ImportError:
try:
import PyQt4
PyQtImpl = "PyQt4"
import PySide2
PyQtImpl = "PySide2"
except ImportError:
try:
import PySide
PyQtImpl = "PySide"
import PyQt4
PyQtImpl = "PyQt4"
except ImportError:
raise ImportError("Cannot load either PyQt or PySide")
if PyQtImpl == "PyQt5":
try:
import PySide
PyQtImpl = "PySide"
except ImportError:
raise ImportError("Cannot load either PyQt or PySide")
# Check the compatibility of PyQtImpl and QVTKRWIBase
if QVTKRWIBase != "QWidget":
if PyQtImpl in ["PySide6"] and QVTKRWIBase == "QOpenGLWidget":
pass # compatible
elif PyQtImpl in ["PyQt5", "PySide2","PyQt4", "PySide"] and QVTKRWIBase == "QGLWidget":
pass # compatible
else:
raise ImportError("Cannot load " + QVTKRWIBase + " from " + PyQtImpl)
if PyQtImpl == "PySide6":
if QVTKRWIBase == "QOpenGLWidget":
from PySide6.QtOpenGLWidgets import QOpenGLWidget
from PySide6.QtWidgets import QWidget
from PySide6.QtWidgets import QSizePolicy
from PySide6.QtWidgets import QApplication
from PySide6.QtWidgets import QMainWindow
from PySide6.QtGui import QCursor
from PySide6.QtCore import Qt
from PySide6.QtCore import QTimer
from PySide6.QtCore import QObject
from PySide6.QtCore import QSize
from PySide6.QtCore import QEvent
elif PyQtImpl == "PyQt5":
if QVTKRWIBase == "QGLWidget":
from PyQt5.QtOpenGL import QGLWidget
from PyQt5.QtWidgets import QWidget
......@@ -144,6 +175,8 @@ if QVTKRWIBase == "QWidget":
QVTKRWIBaseClass = QWidget
elif QVTKRWIBase == "QGLWidget":
QVTKRWIBaseClass = QGLWidget
elif QVTKRWIBase == "QOpenGLWidget":
QVTKRWIBaseClass = QOpenGLWidget
else:
raise ImportError("Unknown base class for QVTKRenderWindowInteractor " + QVTKRWIBase)
......@@ -264,6 +297,8 @@ class QVTKRenderWindowInteractor(QVTKRWIBaseClass):
QWidget.__init__(self, parent, wflags | Qt.MSWindowsOwnDC)
elif QVTKRWIBase == "QGLWidget":
QGLWidget.__init__(self, parent)
elif QVTKRWIBase == "QOpenGLWidget":
QOpenGLWidget.__init__(self, parent)
if rw: # user-supplied render window
self._RenderWindow = rw
......@@ -438,7 +473,7 @@ class QVTKRenderWindowInteractor(QVTKRWIBaseClass):
@staticmethod
def _getPixelRatio():
if PyQtImpl in ["PyQt5", "PySide2"]:
if PyQtImpl in ["PyQt5", "PySide2", "PySide6"]:
# Source: https://stackoverflow.com/a/40053864/3388962
pos = QCursor.pos()
for screen in QApplication.screens():
......@@ -587,7 +622,13 @@ def QVTKRenderWidgetConeExample():
widget.Start()
# start event processing
app.exec_()
# Source: https://doc.qt.io/qtforpython/porting_from2.html
# 'exec_' is deprecated and will be removed in the future.
# Use 'exec' instead.
try:
app.exec()
except AttributeError:
app.exec_()
_keysyms_for_ascii = (
......
......@@ -25,15 +25,15 @@ import sys
PyQtImpl = None
# Has an implementation has been imported yet?
for impl in ["PyQt5", "PySide2", "PyQt4", "PySide"]:
for impl in ["PySide6", "PyQt5", "PySide2", "PyQt4", "PySide"]:
if impl in sys.modules:
PyQtImpl = impl
break
# QVTKRWIBase, base class for QVTKRenderWindowInteractor,
# can be altered by the user to "QGLWidget" in case
# of rendering errors (e.g. depth check problems, readGLBuffer
# warnings...)
# can be altered by the user to "QGLWidget" or "QOpenGLWidget"
# in case of rendering errors (e.g. depth check problems,
# readGLBuffer warnings...)
QVTKRWIBase = "QWidget"
__all__ = ['QVTKRenderWindowInteractor']
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment