1
我在使用PyQt時VTK中的上下文菜單位置有問題。主界面窗口設置VTK部件爲中心的窗口小部件:python 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])))
解決任何幫助,將不勝感激。
我用一點點不同的方法解決了這個問題,但你的問題更好(我有一個鼠標交互相關的問題)。 – avstenit