1

我在使用 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])))

任何解决此问题的帮助将不胜感激。

4

1 回答 1

2

contextmeny 事件方法将一个点作为输入。如果我们假设您的菜单名为 qMenuVTK 并且您有一个父窗口,则以下内容应该有效:

在您的 rightbuttonpress 事件中添加以下内容:

self.parent.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.parent.customContextMenuRequested.connect(self.onContextMenu)

方法事件将如下所示:

def onContextMenu(self, point):
   self.qMenuVTK.exec_(self.parent.mapToGlobal(point))
于 2016-07-26T10:06:26.170 回答