python VTK 上下文菜单不在鼠标位置
python VTK context menu not at mouse position
我在使用 PyQt 的 VTK 中的上下文菜单位置有问题。主 GUI window 已将 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])))
如能帮助解决此问题,我们将不胜感激。
contextmeny 事件方法将一个点作为输入。如果我们假设您的菜单名为 qMenuVTK 并且您有一个父菜单 window,则以下应该有效:
在您的右键按下事件中添加以下内容:
self.parent.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.parent.customContextMenuRequested.connect(self.onContextMenu)
方法事件将如下所示:
def onContextMenu(self, point):
self.qMenuVTK.exec_(self.parent.mapToGlobal(point))
我在使用 PyQt 的 VTK 中的上下文菜单位置有问题。主 GUI window 已将 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])))
如能帮助解决此问题,我们将不胜感激。
contextmeny 事件方法将一个点作为输入。如果我们假设您的菜单名为 qMenuVTK 并且您有一个父菜单 window,则以下应该有效:
在您的右键按下事件中添加以下内容:
self.parent.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.parent.customContextMenuRequested.connect(self.onContextMenu)
方法事件将如下所示:
def onContextMenu(self, point):
self.qMenuVTK.exec_(self.parent.mapToGlobal(point))