enthought / mayavi

3D visualization of scientific data in Python
http://docs.enthought.com/mayavi/mayavi/
Other
1.3k stars 284 forks source link

Problem when using QGLWidget #969

Open wmvanvliet opened 3 years ago

wmvanvliet commented 3 years ago

When setting QVTKRWIBase = "QGLWidget" in vtkmodules/qt/__init__.py to work around rendering glitches, no graphics are being shown and the following error is generated:

QPainter::begin: Paint device returned engine == 0, type: 1

In tvtk/pyface/ui/qt4/QVTKRenderWindowInteractor.py, there is the following construct:

if PyQtImpl == "PyQt5":
    if QVTKRWIBase == "QGLWidget":
        try:
            from PyQt5.QtWidgets import QOpenGLWidget as QGLWidget
        except:
            from PyQt5.QtOpenGL import QGLWidget

Removing the try/catch black to force using the deprecated QGLWidget instead of the more modern QOpenGLWidget fixes the problem and everything runs smoothly. However, we should of course use QOpenGLWidget. But there must be some difference between the two that is causing the error.

I tested this on multiple devices with different types of video cards, I consistently get the error every time.

@larsoner

larsoner commented 3 years ago

The upstream version of this code does not appear to use QOpenGLWidget at all:

https://gitlab.kitware.com/vtk/vtk/blob/741fffbf6490c34228dfe437f330d854b2494adc/Wrapping/Python/vtkmodules/qt/QVTKRenderWindowInteractor.py#L77

I have no idea why this is the case. The Mayavi code looks a lot like the upstream code, but there are some differences (such as this one).

I can replicate the problem with this code:

>>> import vtk.qt
>>> vtk.qt.QVTKRWIBase
'QWidget'
>>> vtkmodules.qt.QVTKRWIBase = "QGLWidget"
>>> from mayavi import mlab
>>> mlab.test_plot3d()
QPainter::begin: Paint device returned engine == 0, type: 1

So this does appear to be a legitimate bug, assuming we are supposed to be allowed to set this value. @prabhuramachandran it looks like you added this in https://github.com/enthought/mayavi/commit/36d4bd7cf95b60df53584a21add019af44a0c2b7 / https://github.com/enthought/mayavi/pull/528. Any chance you remember testing this mode or not?

Also possibly related to this:

https://github.com/pyvista/pyvista/pull/498 https://github.com/pyvista/pyvista/issues/473 https://gitlab.kitware.com/vtk/vtk/-/issues/17867 https://discourse.vtk.org/t/vtk-9-pyqt-macos-no-rendering/3358/6

I still don't really understand how these things are supposed to work. :( It will take some reading to figure it out -- no time right now for me to do it but maybe in a few weeks.

adam-grant-hendry commented 2 years ago

@larsoner After debugging in VSCode with the following launch.json so it will go into packages:

{
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": false
        }
    ],
}

the culprit is rwi.py::QVTKRenderWindowInteractor.paintEngine() getting called when a Paint event occurs:

def paintEngine(self):
    return None

From the PySide6 QPaintEngine docs (it has nearly 2 years since the OP posted the issue and PySide6 is now supported),

My guess is paintEngine was overriden to return None (NULL in C++) to avoid the error:

QWidget::paintEngine: Should no longer be called

since when the paintEngine method is removed from QVTKRenderWindowInteractor, this happens.

adam-grant-hendry commented 2 years ago

@larsoner @wmvanvliet Linking also to pyvistaqt discussion QPainter::begin: Paint device returned engine == 0, type: 1.

The culprit appears to be QVTKRenderWindowInteractor.paintEngine()

adam-grant-hendry commented 2 years ago

@larsoner @wmvanvliet The following widget attribute is set in pyvistaqt.rwi::QVTKRenderWindowInteractor.__init__():

self.setAttribute(WidgetAttribute.WA_PaintOnScreen)

The PySide6.QtCore.Qt docs state this attribute is only available in X11 and I think paintEngine() was set to return None (NULL) on purpose for X11 systems:

image

Hence this clearly causes a problem on non-X11 systems. An alternative is required for non-X11 systems.

adam-grant-hendry commented 2 years ago

@larsoner @wmvanvliet If either of you are attending SciPy this year, it might be a good opportunity to debug this in a sprint session (July 16-17).

I, unfortunately, will be away on vacation, but since it starts tomorrow and most of the developer community will be there, it would be a good opportunity to debug this.

adam-grant-hendry commented 2 years ago

Linking as well the VTK issue I posted for this: QPainter::begin: Paint device returned engine == 0, type: 1 #18606

prabhuramachandran commented 2 years ago

I am around at SciPy this year and will be here for the sprints too. I am planning on pushing a quick release soon though before the tutorials.

prabhuramachandran commented 2 years ago

The upstream version of this code does not appear to use QOpenGLWidget at all:

So this does appear to be a legitimate bug, assuming we are supposed to be allowed to set this value. @prabhuramachandran it looks like you added this in 36d4bd7 / #528. Any chance you remember testing this mode or not?

Unfortunately I do not remember. I suspect I just pulled over code changes from VTK and added them into the Mayavi version which has diverged a bit from VTK. The problem for me of course is that we need to support different VTK versions. I need to revisit the Mayavi version.

wmvanvliet commented 2 years ago

@larsoner @wmvanvliet If either of you are attending SciPy this year, it might be a good opportunity to debug this in a sprint session (July 16-17).

I should start attending SciPy... Anyway, great to see this old thread being dug up. If this bug could be fixed, that would finally allow us to comfortably use non-nVidia graphic cards with mayavi.

prabhuramachandran commented 2 years ago

I intend to put out a release fixing some other issues today but can address this by end of SciPy. My time is extremely limited right now as I rush to prepare for a couple of tutorials, it would really help me if you could summarize how I can reproduce the problem on a conda environment (right now I am on a linux laptop but do have an nvidia card), then I could debug the issue and implement a fix. Some details on what OS and hardware you are looking at would help.

prabhuramachandran commented 2 years ago

If this is just a matter of merging with upstream VTK changes (https://gitlab.kitware.com/vtk/vtk/-/merge_requests/9048) then that makes my life easy too. Thanks. Please let me know.

wmvanvliet commented 2 years ago

@larsoner Posted a minimum example to demonstrate the issue in the second comment in this tread ^^

wmvanvliet commented 2 years ago

Merging the upstream VTK changes might solve the issue already.

adam-grant-hendry commented 2 years ago

@prabhuramachandran @wmvanvliet Thank you for your help!

I am available today and tomorrow, should you need any help testing on a Windows 10 machine with an NVIDIA graphics card.

adam-grant-hendry commented 2 years ago

BTW, @prabhuramachandran , I was scratching my head trying to remember where I had seen you before, and then your SciPy 2018 presentation popped up!

3D Visualization with Mayavi | SciPy 2018 Tutorial | Prabhu Ramachandran

Thanks for the great tutorials!

prabhuramachandran commented 2 years ago

@prabhuramachandran @wmvanvliet Thank you for your help!

I am available today and tomorrow, should you need any help testing on a Windows 10 machine with an NVIDIA graphics card.

Unfortunately, I am swamped today and tomorrow, (I have two tutorials which I am teaching and need to work on the content) we could try tomorrow evening after the back to back tutorials but I will probably be pretty dead by then. I am here all week and also at the sprints. It would be awesome to have this resolved by the end of the conference. Many thanks for offering to help with this, it is much appreciated.

prabhuramachandran commented 2 years ago

FWIW, pushed a 4.8.0 release just now, will roll the fix for this in the next one.

adam-grant-hendry commented 2 years ago

@prabhuramachandran @wmvanvliet Would I be able to join a sprint via Zoom? I will be in Tahoe with limited Internet connectivity and I'm unsure what my family schedule will be, but I can certainly try to join. Do either of you have a time in mind?

Regardless, I will attempt to provide whatever assistance I can within comments here. This is still fairly new territory for me, so please take any potential "fixes" here with a grain of salt (of course, would be FANTASTIC to follow up with some good unit tests, esp. for regressions).

  1. The pyvistaqt repo already has pulled in the vtk upstream changes, but the problem persists (at least on my machine). You can find pyvistaqt's variation of QVTKRenderWindowInteractor here: pyvistaqt/rwi.py

  2. Considering WA_PaintOnScreen is claimed only to work for X11, I modified (the pyvistaqt) rwi.py slightly to:

...
59: import os
...
373: if os.name == 'posix':
374:     self.setAttribute(WidgetAttribute.WA_PaintOnScreen)

Re-running my MRE:

from pyvistaqt import MainWindow, QtInteractor
from qtpy import QtWidgets

class Window(MainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Example')

        self.layout_ = QtWidgets.QVBoxLayout()

        self.container = QtWidgets.QFrame()
        self.container.setLayout(self.layout_)
        self.setCentralWidget(self.container)

        self.plotter = QtInteractor(parent=self.container)
        self.layout_.addWidget(self.plotter)
        self.plotter.show()

        self.signal_close.connect(self.plotter.close)

app = QtWidgets.QApplication([])
window = Window()
window.show()
app.exec_()

gives the following error:

QWindowsGLContext::makeCurrent: SetPixelFormat() failed (The pixel format is invalid.)
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed

which tells me Qt isn't picking up on the fact that we are trying to use OpenGL. From the PySide6 QBackingStore docs:

QBackingStore enables the use of QPainter to paint on a QWindow with type RasterSurface. The other way of rendering to a QWindow is through the use of OpenGL with QOpenGLContext .

A QBackingStore contains a buffered representation of the window contents, and thus supports partial updates by using QPainter to only update a sub region of the window contents.

QBackingStore might be used by an application that wants to use QPainter without OpenGL acceleration and without the extra overhead of using the QWidget or QGraphicsView UI stacks. For an example of how to use QBackingStore see the Raster Window Example .

Indeed, PySide provides an example of how to pull Context Info from the running program. NOTE: This would be good to leverage for future unit tests.

If I change rwi.py to the following:

rwi.py # modified from: https://gitlab.kitware.com/vtk/vtk # under the OSI-approved BSD 3-clause License # TODO: Mayavi has a potentially different version that might be better or worse # coding=utf-8 """ A simple VTK widget for PyQt or PySide. See http://www.trolltech.com for Qt documentation, http://www.riverbankcomputing.co.uk for PyQt, and http://pyside.github.io for PySide. This class is based on the vtkGenericRenderWindowInteractor and is therefore fairly powerful. It should also play nicely with the vtk3DWidget code. Created by Prabhu Ramachandran, May 2002 Based on David Gobbi's QVTKRenderWidget.py Changes by Gerard Vermeulen Feb. 2003 Win32 support. Changes by Gerard Vermeulen, May 2003 Bug fixes and better integration with the Qt framework. Changes by Phil Thompson, Nov. 2006 Ported to PyQt v4. Added support for wheel events. Changes by Phil Thompson, Oct. 2007 Bug fixes. Changes by Phil Thompson, Mar. 2008 Added cursor support. Changes by Rodrigo Mologni, Sep. 2013 (Credit to Daniele Esposti) Bug fix to PySide: Converts PyCObject to void pointer. Changes by Greg Schussman, Aug. 2014 The keyPressEvent function now passes keysym instead of None. Changes by Alex Tsui, Apr. 2015 Port from PyQt4 to PyQt5. Changes by Fabian Wenzel, Jan. 2016 Support for Python3 Changes by Tobias Hänel, Sep. 2018 Support for PySide2 Changes by Ruben de Bruin, Aug. 2019 Fixes to the keyPressEvent function Changes by Chen Jintao, Aug. 2021 Support for PySide6 Changes by Eric Larson and Guillaume Favelier, Apr. 2022 Support for PyQt6 """ import os import sys # Check whether a specific PyQt implementation was chosen try: import vtkmodules.qt PyQtImpl = vtkmodules.qt.PyQtImpl except ImportError: pass # Check whether a specific QVTKRenderWindowInteractor base # 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 QVTKRWIBase = vtkmodules.qt.QVTKRWIBase except ImportError: pass from vtkmodules.vtkRenderingCore import vtkRenderWindow from vtkmodules.vtkRenderingUI import vtkGenericRenderWindowInteractor if PyQtImpl is None: # Autodetect the PyQt implementation to use try: import PyQt6 PyQtImpl = "PyQt6" except ImportError: try: import PySide6 PyQtImpl = "PySide6" except ImportError: try: import PyQt5 PyQtImpl = "PyQt5" except ImportError: try: import PySide2 PyQtImpl = "PySide2" except ImportError: try: import PyQt4 PyQtImpl = "PyQt4" except ImportError: 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 ["PyQt6", "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 == "PyQt6": if QVTKRWIBase == "QOpenGLWidget": from PyQt6.QtOpenGLWidgets import QOpenGLWidget from PyQt6.QtCore import QEvent, QObject, QSize, Qt, QTimer from PyQt6.QtGui import QCursor from PyQt6.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget elif PyQtImpl == "PySide6": if QVTKRWIBase == "QOpenGLWidget": from PySide6.QtOpenGLWidgets import QOpenGLWidget from PySide6.QtCore import QEvent, QLibraryInfo, QObject, QSize, Qt, QTimer from PySide6.QtGui import QCursor, QOpenGLContext, QSurfaceFormat, QWindow from PySide6.QtWidgets import ( QApplication, QMainWindow, QMessageBox, QSizePolicy, QWidget, ) elif PyQtImpl == "PyQt5": if QVTKRWIBase == "QGLWidget": from PyQt5.QtOpenGL import QGLWidget from PyQt5.QtCore import QEvent, QObject, QSize, Qt, QTimer from PyQt5.QtGui import QCursor from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget elif PyQtImpl == "PySide2": if QVTKRWIBase == "QGLWidget": from PySide2.QtOpenGL import QGLWidget from PySide2.QtCore import QEvent, QObject, QSize, Qt, QTimer from PySide2.QtGui import QCursor from PySide2.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget elif PyQtImpl == "PyQt4": if QVTKRWIBase == "QGLWidget": from PyQt4.QtOpenGL import QGLWidget from PyQt4.QtCore import QEvent, QObject, QSize, Qt, QTimer from PyQt4.QtGui import QApplication, QMainWindow, QSizePolicy, QWidget elif PyQtImpl == "PySide": if QVTKRWIBase == "QGLWidget": from PySide.QtOpenGL import QGLWidget from PySide.QtCore import QEvent, QObject, QSize, Qt, QTimer from PySide.QtGui import QApplication, QMainWindow, QSizePolicy, QWidget else: raise ImportError("Unknown PyQt implementation " + repr(PyQtImpl)) # Define types for base class, based on string if QVTKRWIBase == "QWidget": QVTKRWIBaseClass = QWidget elif QVTKRWIBase == "QGLWidget": QVTKRWIBaseClass = QGLWidget elif QVTKRWIBase == "QOpenGLWidget": QVTKRWIBaseClass = QOpenGLWidget else: raise ImportError("Unknown base class for QVTKRenderWindowInteractor " + QVTKRWIBase) if PyQtImpl == 'PyQt6': CursorShape = Qt.CursorShape MouseButton = Qt.MouseButton WindowType = Qt.WindowType WidgetAttribute = Qt.WidgetAttribute KeyboardModifier = Qt.KeyboardModifier FocusPolicy = Qt.FocusPolicy ConnectionType = Qt.ConnectionType Key = Qt.Key SizePolicy = QSizePolicy.Policy EventType = QEvent.Type else: CursorShape = ( MouseButton ) = ( WindowType ) = WidgetAttribute = KeyboardModifier = FocusPolicy = ConnectionType = Key = Qt SizePolicy = QSizePolicy EventType = QEvent if PyQtImpl in ('PyQt4', 'PySide'): MiddleButton = MouseButton.MidButton else: MiddleButton = MouseButton.MiddleButton try: from OpenGL import GL except ImportError: app = QApplication(sys.argv) message_box = QMessageBox( QMessageBox.Critical, "ContextInfo", "PyOpenGL must be installed to run this example.", QMessageBox.Close, ) message_box.setDetailedText("Run:\npip install PyOpenGL PyOpenGL_accelerate") message_box.exec() sys.exit(1) def _get_event_pos(ev): try: # Qt6+ return ev.position().x(), ev.position().y() except AttributeError: # Qt5 return ev.x(), ev.y() def print_surface_format(surface_format): profile_name = ( 'core' if surface_format.profile() == QSurfaceFormat.CoreProfile else 'compatibility' ) major = surface_format.majorVersion() minor = surface_format.minorVersion() return f"{profile_name} version {major}.{minor}" class QVTKRenderWindowInteractor(QVTKRWIBaseClass): """A QVTKRenderWindowInteractor for Python and Qt. Uses a vtkGenericRenderWindowInteractor to handle the interactions. Use GetRenderWindow() to get the vtkRenderWindow. Create with the keyword stereo=1 in order to generate a stereo-capable window. The user interface is summarized in vtkInteractorStyle.h: - Keypress j / Keypress t: toggle between joystick (position sensitive) and trackball (motion sensitive) styles. In joystick style, motion occurs continuously as long as a mouse button is pressed. In trackball style, motion occurs when the mouse button is pressed and the mouse pointer moves. - Keypress c / Keypress o: toggle between camera and object (actor) modes. In camera mode, mouse events affect the camera position and focal point. In object mode, mouse events affect the actor that is under the mouse pointer. - Button 1: rotate the camera around its focal point (if camera mode) or rotate the actor around its origin (if actor mode). The rotation is in the direction defined from the center of the renderer's viewport towards the mouse position. In joystick mode, the magnitude of the rotation is determined by the distance the mouse is from the center of the render window. - Button 2: pan the camera (if camera mode) or translate the actor (if object mode). In joystick mode, the direction of pan or translation is from the center of the viewport towards the mouse position. In trackball mode, the direction of motion is the direction the mouse moves. (Note: with 2-button mice, pan is defined as -Button 1.) - Button 3: zoom the camera (if camera mode) or scale the actor (if object mode). Zoom in/increase scale if the mouse position is in the top half of the viewport; zoom out/decrease scale if the mouse position is in the bottom half. In joystick mode, the amount of zoom is controlled by the distance of the mouse pointer from the horizontal centerline of the window. - Keypress 3: toggle the render window into and out of stereo mode. By default, red-blue stereo pairs are created. Some systems support Crystal Eyes LCD stereo glasses; you have to invoke SetStereoTypeToCrystalEyes() on the rendering window. Note: to use stereo you also need to pass a stereo=1 keyword argument to the constructor. - Keypress e: exit the application. - Keypress f: fly to the picked point - Keypress p: perform a pick operation. The render window interactor has an internal instance of vtkCellPicker that it uses to pick. - Keypress r: reset the camera view along the current view direction. Centers the actors and moves the camera so that all actors are visible. - Keypress s: modify the representation of all actors so that they are surfaces. - Keypress u: invoke the user-defined function. Typically, this keypress will bring up an interactor that you can type commands in. - Keypress w: modify the representation of all actors so that they are wireframe. """ # Map between VTK and Qt cursors. _CURSOR_MAP = { 0: CursorShape.ArrowCursor, # VTK_CURSOR_DEFAULT 1: CursorShape.ArrowCursor, # VTK_CURSOR_ARROW 2: CursorShape.SizeBDiagCursor, # VTK_CURSOR_SIZENE 3: CursorShape.SizeFDiagCursor, # VTK_CURSOR_SIZENWSE 4: CursorShape.SizeBDiagCursor, # VTK_CURSOR_SIZESW 5: CursorShape.SizeFDiagCursor, # VTK_CURSOR_SIZESE 6: CursorShape.SizeVerCursor, # VTK_CURSOR_SIZENS 7: CursorShape.SizeHorCursor, # VTK_CURSOR_SIZEWE 8: CursorShape.SizeAllCursor, # VTK_CURSOR_SIZEALL 9: CursorShape.PointingHandCursor, # VTK_CURSOR_HAND 10: CursorShape.CrossCursor, # VTK_CURSOR_CROSSHAIR } def __init__(self, parent=None, **kw): # the current button self._ActiveButton = MouseButton.NoButton # private attributes self.__saveX = 0 self.__saveY = 0 self.__saveModifiers = KeyboardModifier.NoModifier self.__saveButtons = MouseButton.NoButton self.__wheelDelta = 0 # do special handling of some keywords: # stereo, rw try: stereo = bool(kw['stereo']) except KeyError: stereo = False try: rw = kw['rw'] except KeyError: rw = None # create base qt-level widget if QVTKRWIBase == "QWidget": if "wflags" in kw: wflags = kw['wflags'] else: wflags = Qt.WindowType.Widget QWidget.__init__(self, parent, wflags | WindowType.MSWindowsOwnDC) elif QVTKRWIBase == "QGLWidget": QGLWidget.__init__(self, parent) elif QVTKRWIBase == "QOpenGLWidget": QOpenGLWidget.__init__(self, parent) if rw: # user-supplied render window self._RenderWindow = rw else: self._RenderWindow = vtkRenderWindow() WId = self.winId() # Python2 if type(WId).__name__ == 'PyCObject': from ctypes import c_void_p, py_object, pythonapi pythonapi.PyCObject_AsVoidPtr.restype = c_void_p pythonapi.PyCObject_AsVoidPtr.argtypes = [py_object] WId = pythonapi.PyCObject_AsVoidPtr(WId) # Python3 elif type(WId).__name__ == 'PyCapsule': from ctypes import c_char_p, c_void_p, py_object, pythonapi pythonapi.PyCapsule_GetName.restype = c_char_p pythonapi.PyCapsule_GetName.argtypes = [py_object] name = pythonapi.PyCapsule_GetName(WId) pythonapi.PyCapsule_GetPointer.restype = c_void_p pythonapi.PyCapsule_GetPointer.argtypes = [py_object, c_char_p] WId = pythonapi.PyCapsule_GetPointer(WId, name) self._RenderWindow.SetWindowInfo(str(int(WId))) if stereo: # stereo mode self._RenderWindow.StereoCapableWindowOn() self._RenderWindow.SetStereoTypeToCrystalEyes() try: self._Iren = kw['iren'] except KeyError: self._Iren = vtkGenericRenderWindowInteractor() self._Iren.SetRenderWindow(self._RenderWindow) # do all the necessary qt setup self.setAttribute(WidgetAttribute.WA_OpaquePaintEvent) if os.name == 'posix': self.setAttribute(WidgetAttribute.WA_PaintOnScreen) self.setMouseTracking(True) # get all mouse events self.setFocusPolicy(FocusPolicy.WheelFocus) self.setSizePolicy(QSizePolicy(SizePolicy.Expanding, SizePolicy.Expanding)) self._Timer = QTimer(self) self._Timer.timeout.connect(self.TimerEvent) self._Iren.AddObserver('CreateTimerEvent', self.CreateTimer) self._Iren.AddObserver('DestroyTimerEvent', self.DestroyTimer) self._Iren.GetRenderWindow().AddObserver( 'CursorChangedEvent', self.CursorChangedEvent ) # If we've a parent, it does not close the child when closed. # Connect the parent's destroyed signal to this widget's close # slot for proper cleanup of VTK objects. if self.parent(): self.parent().destroyed.connect(self.close, ConnectionType.DirectConnection) def glInfo(self): if not self.context.makeCurrent(self): raise Exception("makeCurrent() failed") functions = self.context.functions() gl_vendor = functions.glGetString(GL.GL_VENDOR) gl_renderer = functions.glGetString(GL.GL_RENDERER) gl_version = functions.glGetString(GL.GL_VERSION) gl_lang_version = functions.glGetString(GL.GL_SHADING_LANGUAGE_VERSION) context_surface_format = print_surface_format(self.context.format()) surface_format = print_surface_format(self.format()) text = ( f"Vendor: {gl_vendor}\n" f"Renderer: {gl_renderer}\n" f"Version: {gl_version}\n" f"Shading language: {gl_lang_version}\n" f"Context Format: {context_surface_format}\n\n" f"Surface Format: {surface_format}" ) self.context.doneCurrent() return text def __getattr__(self, attr): """Makes the object behave like a vtkGenericRenderWindowInteractor""" if attr == '__vtk__': return lambda t=self._Iren: t elif hasattr(self._Iren, attr): return getattr(self._Iren, attr) else: raise AttributeError( self.__class__.__name__ + " has no attribute named " + attr ) def Finalize(self): ''' Call internal cleanup method on VTK objects ''' self._RenderWindow.Finalize() def CreateTimer(self, obj, evt): self._Timer.start(10) def DestroyTimer(self, obj, evt): self._Timer.stop() return 1 def TimerEvent(self): self._Iren.TimerEvent() def CursorChangedEvent(self, obj, evt): """Called when the CursorChangedEvent fires on the render window.""" # This indirection is needed since when the event fires, the current # cursor is not yet set so we defer this by which time the current # cursor should have been set. QTimer.singleShot(0, self.ShowCursor) def HideCursor(self): """Hides the cursor.""" self.setCursor(Qt.BlankCursor) def ShowCursor(self): """Shows the cursor.""" vtk_cursor = self._Iren.GetRenderWindow().GetCurrentCursor() qt_cursor = self._CURSOR_MAP.get(vtk_cursor, Qt.ArrowCursor) self.setCursor(qt_cursor) def closeEvent(self, evt): self.Finalize() def sizeHint(self): return QSize(400, 400) def paintEngine(self): return None def glInfo(self): build = QLibraryInfo.build() print(f'{build}\n\nPython {sys.version}') context = self.context() # if not context.makeCurrent(self): # raise Exception("makeCurrent() failed") functions = context.functions() gl_vendor = functions.glGetString(GL.GL_VENDOR) gl_renderer = functions.glGetString(GL.GL_RENDERER) gl_version = functions.glGetString(GL.GL_VERSION) gl_lang_version = functions.glGetString(GL.GL_SHADING_LANGUAGE_VERSION) context_surface_format = print_surface_format(context.format()) surface_format = print_surface_format(self.format()) text = ( f"Vendor: {gl_vendor}\n" f"Renderer: {gl_renderer}\n" f"Version: {gl_version}\n" f"Shading language: {gl_lang_version}\n" f"Context Format: {context_surface_format}\n\n" f"Surface Format: {surface_format}\n\n" ) context.doneCurrent() print(text) def paintEvent(self, ev): self.glInfo() self._Iren.Render() def resizeEvent(self, ev): scale = self._getPixelRatio() w = int(round(scale * self.width())) h = int(round(scale * self.height())) self._RenderWindow.SetDPI(int(round(72 * scale))) vtkRenderWindow.SetSize(self._RenderWindow, w, h) self._Iren.SetSize(w, h) self._Iren.ConfigureEvent() self.update() def _GetKeyCharAndKeySym(self, ev): """Convert a Qt key into a char and a vtk keysym. This is essentially copied from the c++ implementation in GUISupport/Qt/QVTKInteractorAdapter.cxx. """ # if there is a char, convert its ASCII code to a VTK keysym try: keyChar = ev.text()[0] keySym = _keysyms_for_ascii[ord(keyChar)] except IndexError: keyChar = '\0' keySym = None # next, try converting Qt key code to a VTK keysym if keySym is None: try: keySym = _keysyms[ev.key()] except KeyError: keySym = None # use "None" as a fallback if keySym is None: keySym = "None" return keyChar, keySym def _GetCtrlShift(self, ev): ctrl = shift = False if hasattr(ev, 'modifiers'): if ev.modifiers() & KeyboardModifier.ShiftModifier: shift = True if ev.modifiers() & KeyboardModifier.ControlModifier: ctrl = True else: if self.__saveModifiers & KeyboardModifier.ShiftModifier: shift = True if self.__saveModifiers & KeyboardModifier.ControlModifier: ctrl = True return ctrl, shift @staticmethod def _getPixelRatio(): if PyQtImpl in ["PyQt5", "PySide2", "PySide6"]: # Source: https://stackoverflow.com/a/40053864/3388962 pos = QCursor.pos() for screen in QApplication.screens(): rect = screen.geometry() if rect.contains(pos): return screen.devicePixelRatio() # Should never happen, but try to find a good fallback. return QApplication.instance().devicePixelRatio() else: # Qt4 seems not to provide any cross-platform means to get the # pixel ratio. return 1.0 def _setEventInformation(self, x, y, ctrl, shift, key, repeat=0, keysum=None): scale = self._getPixelRatio() self._Iren.SetEventInformation( int(round(x * scale)), int(round((self.height() - y - 1) * scale)), ctrl, shift, key, repeat, keysum, ) def enterEvent(self, ev): ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation( self.__saveX, self.__saveY, ctrl, shift, chr(0), 0, None ) self._Iren.EnterEvent() def leaveEvent(self, ev): ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation( self.__saveX, self.__saveY, ctrl, shift, chr(0), 0, None ) self._Iren.LeaveEvent() def mousePressEvent(self, ev): pos_x, pos_y = _get_event_pos(ev) ctrl, shift = self._GetCtrlShift(ev) repeat = 0 if ev.type() == EventType.MouseButtonDblClick: repeat = 1 self._setEventInformation(pos_x, pos_y, ctrl, shift, chr(0), repeat, None) self._ActiveButton = ev.button() if self._ActiveButton == MouseButton.LeftButton: self._Iren.LeftButtonPressEvent() elif self._ActiveButton == MouseButton.RightButton: self._Iren.RightButtonPressEvent() elif self._ActiveButton == MiddleButton: self._Iren.MiddleButtonPressEvent() def mouseReleaseEvent(self, ev): pos_x, pos_y = _get_event_pos(ev) ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation(pos_x, pos_y, ctrl, shift, chr(0), 0, None) if self._ActiveButton == MouseButton.LeftButton: self._Iren.LeftButtonReleaseEvent() elif self._ActiveButton == MouseButton.RightButton: self._Iren.RightButtonReleaseEvent() elif self._ActiveButton == MiddleButton: self._Iren.MiddleButtonReleaseEvent() def mouseMoveEvent(self, ev): pos_x, pos_y = _get_event_pos(ev) self.__saveModifiers = ev.modifiers() self.__saveButtons = ev.buttons() self.__saveX = pos_x self.__saveY = pos_y ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation(pos_x, pos_y, ctrl, shift, chr(0), 0, None) self._Iren.MouseMoveEvent() def keyPressEvent(self, ev): key, keySym = self._GetKeyCharAndKeySym(ev) ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation(self.__saveX, self.__saveY, ctrl, shift, key, 0, keySym) self._Iren.KeyPressEvent() self._Iren.CharEvent() def keyReleaseEvent(self, ev): key, keySym = self._GetKeyCharAndKeySym(ev) ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation(self.__saveX, self.__saveY, ctrl, shift, key, 0, keySym) self._Iren.KeyReleaseEvent() def wheelEvent(self, ev): if hasattr(ev, 'delta'): self.__wheelDelta += ev.delta() else: self.__wheelDelta += ev.angleDelta().y() if self.__wheelDelta >= 120: self._Iren.MouseWheelForwardEvent() self.__wheelDelta = 0 elif self.__wheelDelta <= -120: self._Iren.MouseWheelBackwardEvent() self.__wheelDelta = 0 def GetRenderWindow(self): return self._RenderWindow def Render(self): self.update() def QVTKRenderWidgetConeExample(block=False): """A simple example that uses the QVTKRenderWindowInteractor class.""" import vtkmodules.vtkInteractionStyle # load implementations for rendering and interaction factory classes import vtkmodules.vtkRenderingOpenGL2 from vtkmodules.vtkFiltersSources import vtkConeSource from vtkmodules.vtkRenderingCore import vtkActor, vtkPolyDataMapper, vtkRenderer # every QT app needs an app app = QApplication.instance() if not app: # pragma: no cover app = QApplication(["PyVista"]) window = QMainWindow() # create the widget widget = QVTKRenderWindowInteractor(window) window.setCentralWidget(widget) # if you don't want the 'q' key to exit comment this. widget.AddObserver("ExitEvent", lambda o, e, a=app: a.quit()) ren = vtkRenderer() widget.GetRenderWindow().AddRenderer(ren) cone = vtkConeSource() cone.SetResolution(8) coneMapper = vtkPolyDataMapper() coneMapper.SetInputConnection(cone.GetOutputPort()) coneActor = vtkActor() coneActor.SetMapper(coneMapper) ren.AddActor(coneActor) # show the widget window.show() widget.Initialize() widget.Start() # start event processing # Source: https://doc.qt.io/qtforpython/porting_from2.html # 'exec_' is deprecated and will be removed in the future. # Use 'exec' instead. if block: try: app.exec() except AttributeError: app.exec_() _keysyms_for_ascii = ( None, None, None, None, None, None, None, None, None, "Tab", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, "space", "exclam", "quotedbl", "numbersign", "dollar", "percent", "ampersand", "quoteright", "parenleft", "parenright", "asterisk", "plus", "comma", "minus", "period", "slash", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "colon", "semicolon", "less", "equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft", "backslash", "bracketright", "asciicircum", "underscore", "quoteleft", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", "Delete", ) _keysyms = { Key.Key_Backspace: 'BackSpace', Key.Key_Tab: 'Tab', Key.Key_Backtab: 'Tab', # Key.Key_Clear : 'Clear', Key.Key_Return: 'Return', Key.Key_Enter: 'Return', Key.Key_Shift: 'Shift_L', Key.Key_Control: 'Control_L', Key.Key_Alt: 'Alt_L', Key.Key_Pause: 'Pause', Key.Key_CapsLock: 'Caps_Lock', Key.Key_Escape: 'Escape', Key.Key_Space: 'space', # Key.Key_Prior : 'Prior', # Key.Key_Next : 'Next', Key.Key_End: 'End', Key.Key_Home: 'Home', Key.Key_Left: 'Left', Key.Key_Up: 'Up', Key.Key_Right: 'Right', Key.Key_Down: 'Down', Key.Key_SysReq: 'Snapshot', Key.Key_Insert: 'Insert', Key.Key_Delete: 'Delete', Key.Key_Help: 'Help', Key.Key_0: '0', Key.Key_1: '1', Key.Key_2: '2', Key.Key_3: '3', Key.Key_4: '4', Key.Key_5: '5', Key.Key_6: '6', Key.Key_7: '7', Key.Key_8: '8', Key.Key_9: '9', Key.Key_A: 'a', Key.Key_B: 'b', Key.Key_C: 'c', Key.Key_D: 'd', Key.Key_E: 'e', Key.Key_F: 'f', Key.Key_G: 'g', Key.Key_H: 'h', Key.Key_I: 'i', Key.Key_J: 'j', Key.Key_K: 'k', Key.Key_L: 'l', Key.Key_M: 'm', Key.Key_N: 'n', Key.Key_O: 'o', Key.Key_P: 'p', Key.Key_Q: 'q', Key.Key_R: 'r', Key.Key_S: 's', Key.Key_T: 't', Key.Key_U: 'u', Key.Key_V: 'v', Key.Key_W: 'w', Key.Key_X: 'x', Key.Key_Y: 'y', Key.Key_Z: 'z', Key.Key_Asterisk: 'asterisk', Key.Key_Plus: 'plus', Key.Key_Minus: 'minus', Key.Key_Period: 'period', Key.Key_Slash: 'slash', Key.Key_F1: 'F1', Key.Key_F2: 'F2', Key.Key_F3: 'F3', Key.Key_F4: 'F4', Key.Key_F5: 'F5', Key.Key_F6: 'F6', Key.Key_F7: 'F7', Key.Key_F8: 'F8', Key.Key_F9: 'F9', Key.Key_F10: 'F10', Key.Key_F11: 'F11', Key.Key_F12: 'F12', Key.Key_F13: 'F13', Key.Key_F14: 'F14', Key.Key_F15: 'F15', Key.Key_F16: 'F16', Key.Key_F17: 'F17', Key.Key_F18: 'F18', Key.Key_F19: 'F19', Key.Key_F20: 'F20', Key.Key_F21: 'F21', Key.Key_F22: 'F22', Key.Key_F23: 'F23', Key.Key_F24: 'F24', Key.Key_NumLock: 'Num_Lock', Key.Key_ScrollLock: 'Scroll_Lock', } if __name__ == "__main__": print(PyQtImpl) QVTKRenderWidgetConeExample()

I get the following printout:

Qt 6.3.1 (x86_64-little_endian-llp64 shared (dynamic) release build; by MSVC 2019) [limited API]

Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
Vendor: NVIDIA Corporation
Renderer: Quadro RTX 8000/PCIe/SSE2
Version: 4.6.0 NVIDIA 516.59
Shading language: 4.60 NVIDIA
Context Format: compatibility version 4.6

Surface Format: compatibility version 4.6
Qt 6.3.1 (x86_64-little_endian-llp64 shared (dynamic) release build; by MSVC 2019) [limited API]

Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
Vendor: NVIDIA Corporation
Renderer: Quadro RTX 8000/PCIe/SSE2
Version: 4.5.0 NVIDIA 516.59
Shading language: 4.50 NVIDIA
Context Format: compatibility version 4.6

Surface Format: compatibility version 4.6
QWindowsGLContext::makeCurrent: SetPixelFormat() failed (The pixel format is invalid.)
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed
adam-grant-hendry commented 2 years ago

Adding also the following examples:

adam-grant-hendry commented 2 years ago

I believe with respect to the SetPixelFormat() error above, the pixel format will have to be properly set per platform. On Windows:

Correspondingly in vtkWin32OpenGLRenderWindow:

virtual void SetupPixelFormatPaletteAndContext (HDC hDC, DWORD dwFlags, int debug, int bpp=16, int zbpp=16)
adam-grant-hendry commented 2 years ago

The pixel format error comes from here:

qtbase/qwindowsglcontext.cpp, Line 1287

adam-grant-hendry commented 2 years ago

An important note on differences between QGLWidget and QOpenGLWidget:

The key takeaway is this:

While the API is very similar, there is an important difference between the two: QOpenGLWidget always renders offscreen, using framebuffer objects. QGLWidget on the other hand uses a native window and surface. The latter causes issues when using it in complex user interfaces since, depending on the platform, such native child widgets may have various limitations, regarding stacking orders for example. QOpenGLWidget avoids this by not creating a separate native window.

@wmvanvliet I suspect this is why switching back to QGLWidget worked for you. The QVTKRenderWindowInteractor appears designed around this old architecture.

@prabhuramachandran @larsoner After investigating, it looks like QVTKRenderWindowInteractor will require a rewrite to support the newer QOpenGLWidget. pyvistaqt has already pulled in the vtk upstream changes, and these do not solve the problem (at least on my Windows 10 machine with an NVIDIA gpu).

Since the QGL classes are obsolete or near deprecation and the vtk folks have previously explained they don't have time to maintain this, I think it will become more important for mayavi, pyvista, and other packages using Qt to work together towards a solution.

adam-grant-hendry commented 2 years ago

@prabhuramachandran @wmvanvliet @larsoner Digging deeper, I found this interesting article, which may prove valuable when supporting both vtk <= 8 and vkt 9+:

Widget Use Case
QVTKWidget Used to display vtkRenderWindow in Qt's QWidget
QVTKWidget2 Used to display vtkRenderWindow in Qt's QGLWidget
QVTKOpenGLWidget Used to display vtkRenderWindow in Qt's QWidget
QVTKOpenGLNativeWidget Used to display vtkGenericOpenGLRenderWindow in Qt's QOpenGLWidget

However, in truth, I don't think any of these widgets are available from the vtk 9.1.0 PyPI package.

@prabhuramachandran I'm seeing your name here as a maintainer (you're quite the popular developer!). We may need to have these extras built and added to the vtk PyPI repo so they can be used in upgrading the QVTKRenderWindowInteractor.

prabhuramachandran commented 2 years ago

Thanks @adam-grant-hendry for all the additional information!

We could try a zoom meeting during the sprints but I am not sure how effective it will be. Are you around tomorrow evening? We could at least discuss this then.

I think building the extras is not a scalable option as a PyPI wheel would then be built against a very specific Qt version and that would be a nightmare to maintain or even install given the different kinds of environments users have. One option is to decide to not support VTK <= 8.

adam-grant-hendry commented 2 years ago

@prabhuramachandran You're a stud! Can't believe you're still up!

Yes, agreed...I think Zoom won't be very effective. I am around tomorrow evening (only online though).

I think supporting VTK >=9 makes sense. The only reason is the majority of open source package maintainers are worried by the licensing terms of the PyQt libraries, which is why they want to move to PySide. However, additionally there is the issue that the QGL classes are now (or soon becoming) obsolete, which goes beyond PyQt vs PySide.

I haven't heard back from anyone on the pyvista side, but you could certainly do it for mayavi if you get consensus. I guess/believe pyvista might be of the same opinion, but I'm of the opinion that rising tides raise all boats. If you come up with a working solution for mayavi, pyvista and others are soon to follow.

What are your opinions? And what time would you like to meet tomorrow? I'm in California, so our time zones (relative to Texas) won't be too far off.

adam-grant-hendry commented 2 years ago

@prabhuramachandran Actually, instead, I recommend we should look at the source code for

in

GUISupport/Qt

and leverage what source code we can there for QVTKRenderWindowInteractor.py to make it work.

ASIDE: It also looks like pieces of these classes are deprecated in vtk 9.2, but replaced with different functions.

adam-grant-hendry commented 2 years ago

@prabhuramachandran Another alternative would be to create another PyPI repo for additional dependencies, including these QVTK classes.

e.g. If you use pylint and put your configuration settings in a pyproject.toml, you need the toml library and the additional way of specifying that as an extra dependency is to add the name of the library in square brackets.

On Windows in PowerShell, this is:

PS> py -m pip install pylint[toml]

Hence we could add/remove pieces as needed. This is similar to how vtkmodules separated pieces of vtk into separate modules.

adam-grant-hendry commented 2 years ago

@prabhuramachandran @wmvanvliet @larsoner If I try to switch

if rw:  # user-supplied render window
    self._RenderWindow = rw
else:
    self._RenderWindow = vtkGenericOpenGLRenderWindow()

I get the error:

2022-07-12 11:35:49.389 (   1.031s) [                ]vtkOpenGLRenderWindow.c:493    ERR| vtkGenericOpenGLRenderWindow (00000233F9899E90): GLEW could not be initialized: Missing GL version
ERROR:root:GLEW could not be initialized: Missing GL version

Even though vtkmodules ships with vtkglew-9.1dll . If I run

PS> glewinfo

I get the following glewinfo.txt, which seems to indicate everything is in order:

glewinfo.txt

I've also run vtkProbeOpenGLVersion-9.1.exe

image

Do you have any idea what's going wrong here?

adam-grant-hendry commented 2 years ago

@prabhuramachandran @wmvanvliet @larsoner I stumbled upon a bit of blind luck today. When I change rwi.py to do this:

if rw:  # user-supplied render window
    self._RenderWindow = rw
else:
    self._RenderWindow = vtkRenderWindow()

WId = self.winId()

...

# self._RenderWindow.SetWindowInfo(str(int(WId)))  # Comment this out

Here's the full file:

pyvistaqt/rwi.py ```python # modified from: https://gitlab.kitware.com/vtk/vtk # under the OSI-approved BSD 3-clause License # TODO: Mayavi has a potentially different version that might be better or worse # coding=utf-8 """ A simple VTK widget for PyQt or PySide. See http://www.trolltech.com for Qt documentation, http://www.riverbankcomputing.co.uk for PyQt, and http://pyside.github.io for PySide. This class is based on the vtkGenericRenderWindowInteractor and is therefore fairly powerful. It should also play nicely with the vtk3DWidget code. Created by Prabhu Ramachandran, May 2002 Based on David Gobbi's QVTKRenderWidget.py Changes by Gerard Vermeulen Feb. 2003 Win32 support. Changes by Gerard Vermeulen, May 2003 Bug fixes and better integration with the Qt framework. Changes by Phil Thompson, Nov. 2006 Ported to PyQt v4. Added support for wheel events. Changes by Phil Thompson, Oct. 2007 Bug fixes. Changes by Phil Thompson, Mar. 2008 Added cursor support. Changes by Rodrigo Mologni, Sep. 2013 (Credit to Daniele Esposti) Bug fix to PySide: Converts PyCObject to void pointer. Changes by Greg Schussman, Aug. 2014 The keyPressEvent function now passes keysym instead of None. Changes by Alex Tsui, Apr. 2015 Port from PyQt4 to PyQt5. Changes by Fabian Wenzel, Jan. 2016 Support for Python3 Changes by Tobias Hänel, Sep. 2018 Support for PySide2 Changes by Ruben de Bruin, Aug. 2019 Fixes to the keyPressEvent function Changes by Chen Jintao, Aug. 2021 Support for PySide6 Changes by Eric Larson and Guillaume Favelier, Apr. 2022 Support for PyQt6 """ import os # Check whether a specific PyQt implementation was chosen try: import vtkmodules.qt PyQtImpl = vtkmodules.qt.PyQtImpl except ImportError: pass # Check whether a specific QVTKRenderWindowInteractor base # 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 QVTKRWIBase = vtkmodules.qt.QVTKRWIBase except ImportError: pass from vtkmodules.vtkRenderingCore import vtkRenderWindow from vtkmodules.vtkRenderingUI import vtkGenericRenderWindowInteractor if PyQtImpl is None: # Autodetect the PyQt implementation to use try: import PyQt6 PyQtImpl = "PyQt6" except ImportError: try: import PySide6 PyQtImpl = "PySide6" except ImportError: try: import PyQt5 PyQtImpl = "PyQt5" except ImportError: try: import PySide2 PyQtImpl = "PySide2" except ImportError: try: import PyQt4 PyQtImpl = "PyQt4" except ImportError: 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 ["PyQt6", "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 == "PyQt6": if QVTKRWIBase == "QOpenGLWidget": from PyQt6.QtOpenGLWidgets import QOpenGLWidget from PyQt6.QtCore import QEvent, QObject, QSize, Qt, QTimer from PyQt6.QtGui import QCursor from PyQt6.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget elif PyQtImpl == "PySide6": if QVTKRWIBase == "QOpenGLWidget": from PySide6.QtOpenGLWidgets import QOpenGLWidget from PySide6.QtCore import QEvent, QObject, QSize, Qt, QTimer from PySide6.QtGui import QCursor from PySide6.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget elif PyQtImpl == "PyQt5": if QVTKRWIBase == "QGLWidget": from PyQt5.QtOpenGL import QGLWidget from PyQt5.QtCore import QEvent, QObject, QSize, Qt, QTimer from PyQt5.QtGui import QCursor from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget elif PyQtImpl == "PySide2": if QVTKRWIBase == "QGLWidget": from PySide2.QtOpenGL import QGLWidget from PySide2.QtCore import QEvent, QObject, QSize, Qt, QTimer from PySide2.QtGui import QCursor from PySide2.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget elif PyQtImpl == "PyQt4": if QVTKRWIBase == "QGLWidget": from PyQt4.QtOpenGL import QGLWidget from PyQt4.QtCore import QEvent, QObject, QSize, Qt, QTimer from PyQt4.QtGui import QApplication, QMainWindow, QSizePolicy, QWidget elif PyQtImpl == "PySide": if QVTKRWIBase == "QGLWidget": from PySide.QtOpenGL import QGLWidget from PySide.QtCore import QEvent, QObject, QSize, Qt, QTimer from PySide.QtGui import QApplication, QMainWindow, QSizePolicy, QWidget else: raise ImportError("Unknown PyQt implementation " + repr(PyQtImpl)) # Define types for base class, based on string if QVTKRWIBase == "QWidget": QVTKRWIBaseClass = QWidget elif QVTKRWIBase == "QGLWidget": QVTKRWIBaseClass = QGLWidget elif QVTKRWIBase == "QOpenGLWidget": QVTKRWIBaseClass = QOpenGLWidget else: raise ImportError("Unknown base class for QVTKRenderWindowInteractor " + QVTKRWIBase) if PyQtImpl == 'PyQt6': CursorShape = Qt.CursorShape MouseButton = Qt.MouseButton WindowType = Qt.WindowType WidgetAttribute = Qt.WidgetAttribute KeyboardModifier = Qt.KeyboardModifier FocusPolicy = Qt.FocusPolicy ConnectionType = Qt.ConnectionType Key = Qt.Key SizePolicy = QSizePolicy.Policy EventType = QEvent.Type else: CursorShape = ( MouseButton ) = ( WindowType ) = WidgetAttribute = KeyboardModifier = FocusPolicy = ConnectionType = Key = Qt SizePolicy = QSizePolicy EventType = QEvent if PyQtImpl in ('PyQt4', 'PySide'): MiddleButton = MouseButton.MidButton else: MiddleButton = MouseButton.MiddleButton def _get_event_pos(ev): try: # Qt6+ return ev.position().x(), ev.position().y() except AttributeError: # Qt5 return ev.x(), ev.y() class QVTKRenderWindowInteractor(QVTKRWIBaseClass): """A QVTKRenderWindowInteractor for Python and Qt. Uses a vtkGenericRenderWindowInteractor to handle the interactions. Use GetRenderWindow() to get the vtkRenderWindow. Create with the keyword stereo=1 in order to generate a stereo-capable window. The user interface is summarized in vtkInteractorStyle.h: - Keypress j / Keypress t: toggle between joystick (position sensitive) and trackball (motion sensitive) styles. In joystick style, motion occurs continuously as long as a mouse button is pressed. In trackball style, motion occurs when the mouse button is pressed and the mouse pointer moves. - Keypress c / Keypress o: toggle between camera and object (actor) modes. In camera mode, mouse events affect the camera position and focal point. In object mode, mouse events affect the actor that is under the mouse pointer. - Button 1: rotate the camera around its focal point (if camera mode) or rotate the actor around its origin (if actor mode). The rotation is in the direction defined from the center of the renderer's viewport towards the mouse position. In joystick mode, the magnitude of the rotation is determined by the distance the mouse is from the center of the render window. - Button 2: pan the camera (if camera mode) or translate the actor (if object mode). In joystick mode, the direction of pan or translation is from the center of the viewport towards the mouse position. In trackball mode, the direction of motion is the direction the mouse moves. (Note: with 2-button mice, pan is defined as -Button 1.) - Button 3: zoom the camera (if camera mode) or scale the actor (if object mode). Zoom in/increase scale if the mouse position is in the top half of the viewport; zoom out/decrease scale if the mouse position is in the bottom half. In joystick mode, the amount of zoom is controlled by the distance of the mouse pointer from the horizontal centerline of the window. - Keypress 3: toggle the render window into and out of stereo mode. By default, red-blue stereo pairs are created. Some systems support Crystal Eyes LCD stereo glasses; you have to invoke SetStereoTypeToCrystalEyes() on the rendering window. Note: to use stereo you also need to pass a stereo=1 keyword argument to the constructor. - Keypress e: exit the application. - Keypress f: fly to the picked point - Keypress p: perform a pick operation. The render window interactor has an internal instance of vtkCellPicker that it uses to pick. - Keypress r: reset the camera view along the current view direction. Centers the actors and moves the camera so that all actors are visible. - Keypress s: modify the representation of all actors so that they are surfaces. - Keypress u: invoke the user-defined function. Typically, this keypress will bring up an interactor that you can type commands in. - Keypress w: modify the representation of all actors so that they are wireframe. """ # Map between VTK and Qt cursors. _CURSOR_MAP = { 0: CursorShape.ArrowCursor, # VTK_CURSOR_DEFAULT 1: CursorShape.ArrowCursor, # VTK_CURSOR_ARROW 2: CursorShape.SizeBDiagCursor, # VTK_CURSOR_SIZENE 3: CursorShape.SizeFDiagCursor, # VTK_CURSOR_SIZENWSE 4: CursorShape.SizeBDiagCursor, # VTK_CURSOR_SIZESW 5: CursorShape.SizeFDiagCursor, # VTK_CURSOR_SIZESE 6: CursorShape.SizeVerCursor, # VTK_CURSOR_SIZENS 7: CursorShape.SizeHorCursor, # VTK_CURSOR_SIZEWE 8: CursorShape.SizeAllCursor, # VTK_CURSOR_SIZEALL 9: CursorShape.PointingHandCursor, # VTK_CURSOR_HAND 10: CursorShape.CrossCursor, # VTK_CURSOR_CROSSHAIR } def __init__(self, parent=None, **kw): # the current button self._ActiveButton = MouseButton.NoButton # private attributes self.__saveX = 0 self.__saveY = 0 self.__saveModifiers = KeyboardModifier.NoModifier self.__saveButtons = MouseButton.NoButton self.__wheelDelta = 0 # do special handling of some keywords: # stereo, rw try: stereo = bool(kw['stereo']) except KeyError: stereo = False try: rw = kw['rw'] except KeyError: rw = None # create base qt-level widget if QVTKRWIBase == "QWidget": if "wflags" in kw: wflags = kw['wflags'] else: wflags = Qt.WindowType.Widget QWidget.__init__(self, parent, wflags | WindowType.MSWindowsOwnDC) elif QVTKRWIBase == "QGLWidget": QGLWidget.__init__(self, parent) elif QVTKRWIBase == "QOpenGLWidget": QOpenGLWidget.__init__(self, parent) if rw: # user-supplied render window self._RenderWindow = rw else: self._RenderWindow = vtkRenderWindow() WId = self.winId() # Python2 if type(WId).__name__ == 'PyCObject': from ctypes import c_void_p, py_object, pythonapi pythonapi.PyCObject_AsVoidPtr.restype = c_void_p pythonapi.PyCObject_AsVoidPtr.argtypes = [py_object] WId = pythonapi.PyCObject_AsVoidPtr(WId) # Python3 elif type(WId).__name__ == 'PyCapsule': from ctypes import c_char_p, c_void_p, py_object, pythonapi pythonapi.PyCapsule_GetName.restype = c_char_p pythonapi.PyCapsule_GetName.argtypes = [py_object] name = pythonapi.PyCapsule_GetName(WId) pythonapi.PyCapsule_GetPointer.restype = c_void_p pythonapi.PyCapsule_GetPointer.argtypes = [py_object, c_char_p] WId = pythonapi.PyCapsule_GetPointer(WId, name) # self._RenderWindow.SetWindowInfo(str(int(WId))) if stereo: # stereo mode self._RenderWindow.StereoCapableWindowOn() self._RenderWindow.SetStereoTypeToCrystalEyes() try: self._Iren = kw['iren'] except KeyError: self._Iren = vtkGenericRenderWindowInteractor() self._Iren.SetRenderWindow(self._RenderWindow) # do all the necessary qt setup self.setAttribute(WidgetAttribute.WA_OpaquePaintEvent) if os.name == 'posix': self.setAttribute(WidgetAttribute.WA_PaintOnScreen) self.setMouseTracking(True) # get all mouse events self.setFocusPolicy(FocusPolicy.WheelFocus) self.setSizePolicy(QSizePolicy(SizePolicy.Expanding, SizePolicy.Expanding)) self._Timer = QTimer(self) self._Timer.timeout.connect(self.TimerEvent) self._Iren.AddObserver('CreateTimerEvent', self.CreateTimer) self._Iren.AddObserver('DestroyTimerEvent', self.DestroyTimer) self._Iren.GetRenderWindow().AddObserver( 'CursorChangedEvent', self.CursorChangedEvent ) # If we've a parent, it does not close the child when closed. # Connect the parent's destroyed signal to this widget's close # slot for proper cleanup of VTK objects. if self.parent(): self.parent().destroyed.connect(self.close, ConnectionType.DirectConnection) def __getattr__(self, attr): """Makes the object behave like a vtkGenericRenderWindowInteractor""" if attr == '__vtk__': return lambda t=self._Iren: t elif hasattr(self._Iren, attr): return getattr(self._Iren, attr) else: raise AttributeError( self.__class__.__name__ + " has no attribute named " + attr ) def Finalize(self): ''' Call internal cleanup method on VTK objects ''' self._RenderWindow.Finalize() def CreateTimer(self, obj, evt): self._Timer.start(10) def DestroyTimer(self, obj, evt): self._Timer.stop() return 1 def TimerEvent(self): self._Iren.TimerEvent() def CursorChangedEvent(self, obj, evt): """Called when the CursorChangedEvent fires on the render window.""" # This indirection is needed since when the event fires, the current # cursor is not yet set so we defer this by which time the current # cursor should have been set. QTimer.singleShot(0, self.ShowCursor) def HideCursor(self): """Hides the cursor.""" self.setCursor(Qt.BlankCursor) def ShowCursor(self): """Shows the cursor.""" vtk_cursor = self._Iren.GetRenderWindow().GetCurrentCursor() qt_cursor = self._CURSOR_MAP.get(vtk_cursor, Qt.ArrowCursor) self.setCursor(qt_cursor) def closeEvent(self, evt): self.Finalize() def sizeHint(self): return QSize(400, 400) def paintEngine(self): return None def paintEvent(self, ev): self._Iren.Render() def resizeEvent(self, ev): scale = self._getPixelRatio() w = int(round(scale * self.width())) h = int(round(scale * self.height())) self._RenderWindow.SetDPI(int(round(72 * scale))) vtkRenderWindow.SetSize(self._RenderWindow, w, h) self._Iren.SetSize(w, h) self._Iren.ConfigureEvent() self.update() def _GetKeyCharAndKeySym(self, ev): """Convert a Qt key into a char and a vtk keysym. This is essentially copied from the c++ implementation in GUISupport/Qt/QVTKInteractorAdapter.cxx. """ # if there is a char, convert its ASCII code to a VTK keysym try: keyChar = ev.text()[0] keySym = _keysyms_for_ascii[ord(keyChar)] except IndexError: keyChar = '\0' keySym = None # next, try converting Qt key code to a VTK keysym if keySym is None: try: keySym = _keysyms[ev.key()] except KeyError: keySym = None # use "None" as a fallback if keySym is None: keySym = "None" return keyChar, keySym def _GetCtrlShift(self, ev): ctrl = shift = False if hasattr(ev, 'modifiers'): if ev.modifiers() & KeyboardModifier.ShiftModifier: shift = True if ev.modifiers() & KeyboardModifier.ControlModifier: ctrl = True else: if self.__saveModifiers & KeyboardModifier.ShiftModifier: shift = True if self.__saveModifiers & KeyboardModifier.ControlModifier: ctrl = True return ctrl, shift @staticmethod def _getPixelRatio(): if PyQtImpl in ["PyQt5", "PySide2", "PySide6"]: # Source: https://stackoverflow.com/a/40053864/3388962 pos = QCursor.pos() for screen in QApplication.screens(): rect = screen.geometry() if rect.contains(pos): return screen.devicePixelRatio() # Should never happen, but try to find a good fallback. return QApplication.instance().devicePixelRatio() else: # Qt4 seems not to provide any cross-platform means to get the # pixel ratio. return 1.0 def _setEventInformation(self, x, y, ctrl, shift, key, repeat=0, keysum=None): scale = self._getPixelRatio() self._Iren.SetEventInformation( int(round(x * scale)), int(round((self.height() - y - 1) * scale)), ctrl, shift, key, repeat, keysum, ) def enterEvent(self, ev): ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation( self.__saveX, self.__saveY, ctrl, shift, chr(0), 0, None ) self._Iren.EnterEvent() def leaveEvent(self, ev): ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation( self.__saveX, self.__saveY, ctrl, shift, chr(0), 0, None ) self._Iren.LeaveEvent() def mousePressEvent(self, ev): pos_x, pos_y = _get_event_pos(ev) ctrl, shift = self._GetCtrlShift(ev) repeat = 0 if ev.type() == EventType.MouseButtonDblClick: repeat = 1 self._setEventInformation(pos_x, pos_y, ctrl, shift, chr(0), repeat, None) self._ActiveButton = ev.button() if self._ActiveButton == MouseButton.LeftButton: self._Iren.LeftButtonPressEvent() elif self._ActiveButton == MouseButton.RightButton: self._Iren.RightButtonPressEvent() elif self._ActiveButton == MiddleButton: self._Iren.MiddleButtonPressEvent() def mouseReleaseEvent(self, ev): pos_x, pos_y = _get_event_pos(ev) ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation(pos_x, pos_y, ctrl, shift, chr(0), 0, None) if self._ActiveButton == MouseButton.LeftButton: self._Iren.LeftButtonReleaseEvent() elif self._ActiveButton == MouseButton.RightButton: self._Iren.RightButtonReleaseEvent() elif self._ActiveButton == MiddleButton: self._Iren.MiddleButtonReleaseEvent() def mouseMoveEvent(self, ev): pos_x, pos_y = _get_event_pos(ev) self.__saveModifiers = ev.modifiers() self.__saveButtons = ev.buttons() self.__saveX = pos_x self.__saveY = pos_y ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation(pos_x, pos_y, ctrl, shift, chr(0), 0, None) self._Iren.MouseMoveEvent() def keyPressEvent(self, ev): key, keySym = self._GetKeyCharAndKeySym(ev) ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation(self.__saveX, self.__saveY, ctrl, shift, key, 0, keySym) self._Iren.KeyPressEvent() self._Iren.CharEvent() def keyReleaseEvent(self, ev): key, keySym = self._GetKeyCharAndKeySym(ev) ctrl, shift = self._GetCtrlShift(ev) self._setEventInformation(self.__saveX, self.__saveY, ctrl, shift, key, 0, keySym) self._Iren.KeyReleaseEvent() def wheelEvent(self, ev): if hasattr(ev, 'delta'): self.__wheelDelta += ev.delta() else: self.__wheelDelta += ev.angleDelta().y() if self.__wheelDelta >= 120: self._Iren.MouseWheelForwardEvent() self.__wheelDelta = 0 elif self.__wheelDelta <= -120: self._Iren.MouseWheelBackwardEvent() self.__wheelDelta = 0 def GetRenderWindow(self): return self._RenderWindow def Render(self): self.update() def QVTKRenderWidgetConeExample(block=False): """A simple example that uses the QVTKRenderWindowInteractor class.""" import vtkmodules.vtkInteractionStyle # load implementations for rendering and interaction factory classes import vtkmodules.vtkRenderingOpenGL2 from vtkmodules.vtkFiltersSources import vtkConeSource from vtkmodules.vtkRenderingCore import vtkActor, vtkPolyDataMapper, vtkRenderer # every QT app needs an app app = QApplication.instance() if not app: # pragma: no cover app = QApplication(["PyVista"]) window = QMainWindow() # create the widget widget = QVTKRenderWindowInteractor(window) window.setCentralWidget(widget) # if you don't want the 'q' key to exit comment this. widget.AddObserver("ExitEvent", lambda o, e, a=app: a.quit()) ren = vtkRenderer() widget.GetRenderWindow().AddRenderer(ren) cone = vtkConeSource() cone.SetResolution(8) coneMapper = vtkPolyDataMapper() coneMapper.SetInputConnection(cone.GetOutputPort()) coneActor = vtkActor() coneActor.SetMapper(coneMapper) ren.AddActor(coneActor) # show the widget window.show() widget.Initialize() widget.Start() # start event processing # Source: https://doc.qt.io/qtforpython/porting_from2.html # 'exec_' is deprecated and will be removed in the future. # Use 'exec' instead. if block: try: app.exec() except AttributeError: app.exec_() _keysyms_for_ascii = ( None, None, None, None, None, None, None, None, None, "Tab", None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, "space", "exclam", "quotedbl", "numbersign", "dollar", "percent", "ampersand", "quoteright", "parenleft", "parenright", "asterisk", "plus", "comma", "minus", "period", "slash", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "colon", "semicolon", "less", "equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft", "backslash", "bracketright", "asciicircum", "underscore", "quoteleft", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", "Delete", ) _keysyms = { Key.Key_Backspace: 'BackSpace', Key.Key_Tab: 'Tab', Key.Key_Backtab: 'Tab', # Key.Key_Clear : 'Clear', Key.Key_Return: 'Return', Key.Key_Enter: 'Return', Key.Key_Shift: 'Shift_L', Key.Key_Control: 'Control_L', Key.Key_Alt: 'Alt_L', Key.Key_Pause: 'Pause', Key.Key_CapsLock: 'Caps_Lock', Key.Key_Escape: 'Escape', Key.Key_Space: 'space', # Key.Key_Prior : 'Prior', # Key.Key_Next : 'Next', Key.Key_End: 'End', Key.Key_Home: 'Home', Key.Key_Left: 'Left', Key.Key_Up: 'Up', Key.Key_Right: 'Right', Key.Key_Down: 'Down', Key.Key_SysReq: 'Snapshot', Key.Key_Insert: 'Insert', Key.Key_Delete: 'Delete', Key.Key_Help: 'Help', Key.Key_0: '0', Key.Key_1: '1', Key.Key_2: '2', Key.Key_3: '3', Key.Key_4: '4', Key.Key_5: '5', Key.Key_6: '6', Key.Key_7: '7', Key.Key_8: '8', Key.Key_9: '9', Key.Key_A: 'a', Key.Key_B: 'b', Key.Key_C: 'c', Key.Key_D: 'd', Key.Key_E: 'e', Key.Key_F: 'f', Key.Key_G: 'g', Key.Key_H: 'h', Key.Key_I: 'i', Key.Key_J: 'j', Key.Key_K: 'k', Key.Key_L: 'l', Key.Key_M: 'm', Key.Key_N: 'n', Key.Key_O: 'o', Key.Key_P: 'p', Key.Key_Q: 'q', Key.Key_R: 'r', Key.Key_S: 's', Key.Key_T: 't', Key.Key_U: 'u', Key.Key_V: 'v', Key.Key_W: 'w', Key.Key_X: 'x', Key.Key_Y: 'y', Key.Key_Z: 'z', Key.Key_Asterisk: 'asterisk', Key.Key_Plus: 'plus', Key.Key_Minus: 'minus', Key.Key_Period: 'period', Key.Key_Slash: 'slash', Key.Key_F1: 'F1', Key.Key_F2: 'F2', Key.Key_F3: 'F3', Key.Key_F4: 'F4', Key.Key_F5: 'F5', Key.Key_F6: 'F6', Key.Key_F7: 'F7', Key.Key_F8: 'F8', Key.Key_F9: 'F9', Key.Key_F10: 'F10', Key.Key_F11: 'F11', Key.Key_F12: 'F12', Key.Key_F13: 'F13', Key.Key_F14: 'F14', Key.Key_F15: 'F15', Key.Key_F16: 'F16', Key.Key_F17: 'F17', Key.Key_F18: 'F18', Key.Key_F19: 'F19', Key.Key_F20: 'F20', Key.Key_F21: 'F21', Key.Key_F22: 'F22', Key.Key_F23: 'F23', Key.Key_F24: 'F24', Key.Key_NumLock: 'Num_Lock', Key.Key_ScrollLock: 'Scroll_Lock', } if __name__ == "__main__": print(PyQtImpl) QVTKRenderWidgetConeExample() ```

I get no errors on my machine, but 2 windows instead of the intended 1:

MRE.py ```python from pyvistaqt import MainWindow, QtInteractor from qtpy import QtWidgets class Window(MainWindow): def __init__(self): super().__init__() self.setWindowTitle('Example') self.layout_ = QtWidgets.QVBoxLayout() self.container = QtWidgets.QFrame() self.container.setLayout(self.layout_) self.setCentralWidget(self.container) self.plotter = QtInteractor() self.plotter.add_axes() self.container.layout().addWidget(self.plotter) self.signal_close.connect(self.plotter.close) app = QtWidgets.QApplication([]) window = Window() window.show() app.exec_() ```

demo

When I uncomment

self._RenderWindow.SetWindowInfo(str(int(WId)))

The window is properly embedded, but the errors

QWindowsGLContext::makeCurrent: SetPixelFormat() failed (The pixel format is invalid.)
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed

occur at every paint event

demo3_Wzbcwu29

If I print the self._RenderWindow before and after setting the window info:

print(self._RenderWindow)
self._RenderWindow.SetWindowInfo(str(int(WId)))
print(self._RenderWindow)

The only thing that seems to have changed is the Window Id.

Before ``` vtkWin32OpenGLRenderWindow (000001E3ABE51800) Debug: Off Modified Time: 52 Reference Count: 1 Registered Events: (none) Erase: On Window Name: Visualization Toolkit - Win32OpenGL # Position: (0, 0) Size: (0, 0) Mapped: 0 ShowWindow: 1 UseOffScreenBuffers: 0 Double Buffered: 1 DPI: 72 TileScale: (1, 1) TileViewport: (0, 0, 1, 1) Borders: On Double Buffer: On Full Screen: Off Renderers: Debug: Off Modified Time: 44 Reference Count: 1 Registered Events: (none) Number Of Items: 0 Stereo Capable Window Requested: No Stereo Render: Off Point Smoothing: Off Line Smoothing: Off Polygon Smoothing: Off Abort Render: 0 Current Cursor: 0 Desired Update Rate: 0.0001 In Abort Check: 0 NeverRendered: 1 Interactor: 0000000000000000 Swap Buffers: On Stereo Type: CrystalEyes Number of Layers: 1 AlphaBitPlanes: On UseSRGBColorSpace: Off AnaglyphColorSaturation: 0.65 AnaglyphColorMask: 4 , 3 MultiSamples: 8 StencilCapable: False ContextId: 0000000000000000 Next Window Id: 0000000000000000 Window Id: 0000000000000000 ```
After vtkWin32OpenGLRenderWindow (000001E3ABE51800) Debug: Off Modified Time: 52 Reference Count: 1 Registered Events: (none) Erase: On Window Name: Visualization Toolkit - Win32OpenGL # Position: (0, 0) Size: (0, 0) Mapped: 0 ShowWindow: 1 UseOffScreenBuffers: 0 Double Buffered: 1 DPI: 72 TileScale: (1, 1) TileViewport: (0, 0, 1, 1) Borders: On Double Buffer: On Full Screen: Off Renderers: Debug: Off Modified Time: 44 Reference Count: 1 Registered Events: (none) Number Of Items: 0 Stereo Capable Window Requested: No Stereo Render: Off Point Smoothing: Off Line Smoothing: Off Polygon Smoothing: Off Abort Render: 0 Current Cursor: 0 Desired Update Rate: 0.0001 In Abort Check: 0 NeverRendered: 1 Interactor: 0000000000000000 Swap Buffers: On Stereo Type: CrystalEyes Number of Layers: 1 AlphaBitPlanes: On UseSRGBColorSpace: Off AnaglyphColorSaturation: 0.65 AnaglyphColorMask: 4 , 3 MultiSamples: 8 StencilCapable: False ContextId: 0000000000000000 Next Window Id: 0000000000000000 Window Id: 0000000000160EC6

Is this what is supposed to happen? Do you think Qt is thinking we are trying to do things on the QMainWindow (here named Example), when we are really only trying to do them on the vtkWin32OpenGLRenderWindow window?

FWIW, pyvista's MainWindow class is very straightforward:

"""This module contains a Qt-compatible MainWindow class."""

from __future__ import annotations

from qtpy import QtCore
from qtpy.QtCore import Signal
from qtpy.QtWidgets import QMainWindow, QWidget

class MainWindow(QMainWindow):
    """Convenience MainWindow that manages the application."""

    signal_close = Signal()
    signal_gesture = Signal(QtCore.QEvent)

    def __init__(
        self,
        parent: QWidget | None = None,
        title: str | None = None,
        size: tuple[int, int] | None = None,
    ) -> None:
        """Initialize the main window."""
        QMainWindow.__init__(self, parent=parent)
        if title is not None:
            self.setWindowTitle(title)
        if size is not None:
            self.resize(*size)

    def event(self, event: QtCore.QEvent) -> bool:
        """Manage window events and filter the gesture event."""
        if event.type() == QtCore.QEvent.Gesture:  # pragma: no cover
            self.signal_gesture.emit(event)
            return True
        return super().event(event)

    def closeEvent(self, event: QtCore.QEvent) -> None:  # pylint: disable=invalid-name
        """Manage the close event."""
        self.signal_close.emit()
        event.accept()
adam-grant-hendry commented 2 years ago

@prabhuramachandran @wmvanvliet @larsoner Actually, now that I think of it, this error message does seem consistent with the changes from QGLWidget to QOpenGLWidget.

In particular, for PySide6 and PyQt6, QVTKRWIBase = 'QOpenGLWidget', and one of the notes states

Note: Avoid calling winId() on a QOpenGLWidget. This function triggers the creation of a native window, resulting in reduced performance and possibly rendering glitches.

You can see from the QtWidgets.QWidget docs:

image

also

Drawing directly to the QOpenGLWidget's framebuffer outside the GUI/main thread is possible by reimplementing paintEvent() to do nothing. The context's thread affinity has to be changed via QObject::moveToThread(). After that, makeCurrent() and doneCurrent() are usable on the worker thread. Be careful to move the context back to the GUI/main thread afterwards.

and

Adding a QOpenGLWidget into a window turns on OpenGL-based compositing for the entire window. In some special cases this may not be ideal, and the old QGLWidget-style behavior with a separate, native child window is desired. Desktop applications that understand the limitations of this approach (for example when it comes to overlaps, transparency, scroll views and MDI areas), can use QOpenGLWindow with QWidget::createWindowContainer(). This is a modern alternative to QGLWidget and is faster than QOpenGLWidget due to the lack of the additional composition step. It is strongly recommended to limit the usage of this approach to cases where there is no other choice. Note that this option is not suitable for most embedded and mobile platforms, and it is known to have issues on certain desktop platforms (e.g. macOS) too. The stable, cross-platform solution is always QOpenGLWidget.

adam-grant-hendry commented 2 years ago

@prabhuramachandran @wmvanvliet @larsoner If I add

window_ = QtGui.QWindow.fromWinId(self.plotter.winId())
self.container = self.createWindowContainer(window_, self)

to my Window.__init__() method, this resolves the errors:

QWindowsGLContext::makeCurrent: SetPixelFormat() failed (The pixel format is invalid.)
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed

so the final MRE is now

Final MRE ```python from __future__ import annotations from pyvistaqt import MainWindow, QtInteractor from qtpy import QtGui, QtWidgets class Window(MainWindow): def __init__(self, parent=None): MainWindow.__init__(self, parent) self.setWindowTitle('Example') self.layout().setContentsMargins(0, 0, 0, 0) self.layout().setSpacing(0) self.plotter = QtInteractor() self.plotter.add_axes() window_ = QtGui.QWindow.fromWinId(self.plotter.winId()) self.container = self.createWindowContainer(window_, self) self.setCentralWidget(self.container) self.signal_close.connect(self.plotter.close) format_ = QtGui.QSurfaceFormat() format_.setMajorVersion(4) format_.setMinorVersion(6) format_.setDepthBufferSize(16) QtGui.QSurfaceFormat.setDefaultFormat(format_) app = QtWidgets.QApplication([]) window = Window() window.show() app.exec_() ```

I'm unsure if there is a better way. I'm open to suggestions though.

This and simply adding this check in QVTKRenderWindowInteractor.__init__()

if os.name == 'posix':
    self.setAttribute(WidgetAttribute.WA_PaintOnScreen)

solve 100% of the problems for me.

prabhuramachandran commented 2 years ago

Thanks for the fantastic work on this and for all your efforts. I am very sorry for not responding sooner, I have been a bit busy with the conference and then catching up with work. My hope is that I get this done during the sprints. Thanks again for all your work and finding a solution that works.

adam-grant-hendry commented 2 years ago

Thanks for the fantastic work on this and for all your efforts. I am very sorry for not responding sooner, I have been a bit busy with the conference and then catching up with work. My hope is that I get this done during the sprints. Thanks again for all your work and finding a solution that works.

No apologies necessary! Happy to help.

adam-grant-hendry commented 2 years ago

@prabhuramachandran Please note that my "fix" only works when QVTKRenderWidgetInteractor subclasses from QWidget. You must manually change QVTKRWIBase in vtmodules/qt/init.py to change the base class it derives from

# QVTKRWIBase, base class for QVTKRenderWindowInteractor,
# can be altered by the user to "QGLWidget" or "QOpenGLWidget"
# in case of rendering errors (e.g. depth check problems,
# readGLBuffer warnings...)
QVTKRWIBase = "QWidget"

and you can also set the python Qt binding by setting PyQtImpl to one of ["PySide6", "PyQt6", "PyQt5", "PySide2", "PyQt4", "PySide"]:

# PyQtImpl can be set by the user
PyQtImpl = None

QVTKRenderWindowInteractor.py then pulls in the modified information

# Check whether a specific PyQt implementation was chosen
try:
    import vtkmodules.qt
    PyQtImpl = vtkmodules.qt.PyQtImpl
except ImportError:
    pass

# Check whether a specific QVTKRenderWindowInteractor base
# class was chosen, can be set to "QGLWidget" in
# PyQt implementation version lower than Qt6,
# or "QOpenGLWidget" in Pyside6 and PyQt6
QVTKRWIBase = "QWidget"
try:
    import vtkmodules.qt
    QVTKRWIBase = vtkmodules.qt.QVTKRWIBase
except ImportError:
    pass

This situation isn't ideal because if the user changes or updates versions of VTK, manual changes in the __init__.py get overridden, so the changes would have to be remade. It would be much nicer if the user were to set this dynamically in their code:

import vtkmodules.qt
vtkmodules.qt.PyQtImpl = 'PySide6'
vtkmodules.qt.QVTKRWIBase = 'QOpenGLWidget'
from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor

I think the above should be added to the pyvistaqt documentation, since most users won't know to manually modify vtkmodules/qt/__init__.py. However, see below (I think QWidget should be the default).

Important

From the What's New in Qt4, Qt4 split QWidget's WFlags into Qt::WindowFlags and Qt::WidgetAttribute flags and introduced WA_PaintOnScreen for backwards-compatibility. In Qt4, all widgets are double-buffered, so the WA_PaintOnScreen allowed setting a widget to use single buffering.

Even though the raster paint engine is the primary paint engine for QtWidget-based classes on Windows, X11, and macOS, I don't that there's necessarily a huge performance difference suing QWidget vs QOpenGLWidget.

A good reference w.r.t. using the QVTKOpenGLWidget variants is this discourse discussion:

How to decide between QVTKOpenGL{Native}Widget?

We could try to implement them using the developer tips from here:

VTK Python Wrappers Notes

adam-grant-hendry commented 2 years ago

@prabhuramachandran I realized my fix isn't really a fix: the errors occur when QVTKRenderWindowInteractor subclasses from anything other than QWidget.

On Windows, X11, and MacOS, with Qt>5.4 the primary paint engine for QWidget-based classes is the raster paint engine. When the raster engine is used, Windows and macOS ignore WA_PaintOnScreen, but since we call winId (which works on all platforms), we get a native widget with the raster engine, so returning None from QWidget::paintEngine() doesn't cause an error.

Using an opengl paint engine, however, causes a problem. Since Qt4, all QWidget's are double-buffered, and WA_PaintOnScreen was added for backwards-compatibility to allow setting a widget to use single buffering. However, single buffering will not work with QOpenGLWidgets as the Qt docs clearly state:

QOpenGLWidget always renders offscreen, using framebuffer objects. QGLWidget on the other hand uses a native window and surface.

Hence QOpenGLWidget is always going to be double-buffered.

The QVTKOpenGL widgets were introduced to work with Qt's QOpenGLWidget, but these have not been ported to Python VTK (i.e. vtkmodules).

In PyQt5>5.4 and all of PyQt6 (and their PySide equivalents), the QGL classes are gone and replaced by the QOpenGL classes. So, if we want to support the latest versions of PyQt and PySide, let users fix rendering glitches on their hardware (esp. non-desktop versions) we need DLL's for the QVTKOpenGL widgets ported to vtkmodules.

Alternatively, if those DLL's can't be ported, we could try to implement these classes using the Python Wrappers developer notes.

adam-grant-hendry commented 2 years ago

@prabhuramachandran @wmvanvliet @larsoner I have submitted MR 9443 on VTK to attempt to solve this issue. Would you please kindly review the MR? (The VTK team has asked I bring in reviewers from mayavi and pyvista).

Thanks in advance!

adam-grant-hendry commented 2 years ago

@prabhuramachandran Any chance you would be able to review MR 9443 this weekend?