qt_window1.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import sys
  2. from PyQt5 import Qt
  3. from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
  4. from vedo import Plotter, Cone, printc
  5. class MainWindow(Qt.QMainWindow):
  6. def __init__(self, parent=None):
  7. Qt.QMainWindow.__init__(self, parent)
  8. self.frame = Qt.QFrame()
  9. self.layout = Qt.QVBoxLayout()
  10. self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
  11. # Create renderer and add the vedo objects and callbacks
  12. self.plt = Plotter(qt_widget=self.vtkWidget)
  13. self.id1 = self.plt.add_callback("mouse click", self.onMouseClick)
  14. self.id2 = self.plt.add_callback("key press", self.onKeypress)
  15. self.plt += Cone().rotate_x(20)
  16. self.plt.show() # <--- show the vedo rendering
  17. # Set-up the rest of the Qt window
  18. button = Qt.QPushButton("My Button makes the cone red")
  19. button.setToolTip('This is an example button')
  20. button.clicked.connect(self.onClick)
  21. self.layout.addWidget(self.vtkWidget)
  22. self.layout.addWidget(button)
  23. self.frame.setLayout(self.layout)
  24. self.setCentralWidget(self.frame)
  25. self.show() # <--- show the Qt Window
  26. def onMouseClick(self, evt):
  27. printc("You have clicked your mouse button. Event info:\n", evt, c='y')
  28. def onKeypress(self, evt):
  29. printc("You have pressed key:", evt.keypress, c='b')
  30. @Qt.pyqtSlot()
  31. def onClick(self):
  32. printc("..calling onClick")
  33. self.plt.objects[0].color('red').rotate_z(40)
  34. self.plt.interactor.Render()
  35. def onClose(self):
  36. #Disable the interactor before closing to prevent it
  37. #from trying to act on already deleted items
  38. printc("..calling onClose")
  39. self.vtkWidget.close()
  40. if __name__ == "__main__":
  41. app = Qt.QApplication(sys.argv)
  42. window = MainWindow()
  43. app.aboutToQuit.connect(window.onClose) # <-- connect the onClose event
  44. app.exec_()