Hi,
I can put Qt widgets over the VTK widget in Python, but a few stange things happen when I do.
- Transparency doesn’t work.
- The color changes slowly when moving the mouse cursor over the button and reset only when resizing the window.
I have tried both PyQT and PySide2 with different versions, inclusing the latest. I also tried with both VTK 8.2 and 9.0. I have seen working examples, but so far, not in Python.
The following code is my example. The button should be transparent. If it works on your end, then it could be specific to my os/hardware.
import sys, os
import vtk
from PyQt5 import QtCore, QtWidgets, QtGui
import PyQt5
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
dirname = os.path.dirname(PyQt5.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.widget = QtWidgets.QWidget()
self.vl = QtWidgets.QVBoxLayout()
self.vtkWidget = QVTKRenderWindowInteractor(self.widget)
self.vl.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
self.layout = QtWidgets.QHBoxLayout()
self.vtkWidget.setLayout(self.layout)
btn = QtWidgets.QPushButton("TEST TEST", self.vtkWidget)
btn.setStyleSheet("background-color: rgba(255,0,255,128)")
self.layout.addWidget(btn)
# Create source
source = vtk.vtkSphereSource()
source.SetCenter(0, 0, 0)
source.SetRadius(5.0)
# Create a mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
# Create an actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
self.ren.AddActor(actor)
self.ren.ResetCamera()
self.widget.setLayout(self.vl)
self.setCentralWidget(self.widget)
self.show()
self.iren.Initialize()
self.iren.Start()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())