我在使用 PyQt 的 VTK 中的上下文菜单位置有问题。主 GUI 窗口已将 VTK 小部件设置为中央小部件:
from vtk_widget.vtk_widget import VTKWidget
class DySMainWindow(QtGui.QMainWindow):
def __init__(self):
self.vtk_widget = VTKWidget(self)
self.setCentralWidget(self.vtk_widget)
是VTK widget
:
import vtk
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt4 import QtGui, QtCore, Qt
class VTKWidget(QVTKRenderWindowInteractor):
def __init__(self, MBD_system=None, parent=None):
super(VTKWidget, self).__init__(parent)
# this should show context menu
self.AddObserver("RightButtonPressEvent", self.contextMenu)
self.renderer = vtk.vtkRenderer()
self.GetRenderWindow().AddRenderer(self.renderer)
self.interactor = self.GetRenderWindow().GetInteractor()
self.interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
# camera object
self.camera = self.renderer.GetActiveCamera()
if self.projection == 'perspective':
self.camera.ParallelProjectionOff()
else:
self.camera.ParallelProjectionOn()
self.renderer.SetActiveCamera(self.camera)
self.renderer.ResetCamera()
self.renderer.SetBackground(0, 0, 0)
self.interactor.Initialize()
def contextMenu(self, caller, event):
pos = self.interactor.GetEventPosition()
menu = QtGui.QMenu(parent=self)
menu.addAction(self.tr("Edit object"))
menu.exec_(self.mapToGlobal(QtCore.QPoint(pos[0], pos[1])))
任何解决此问题的帮助将不胜感激。