如何在vtk python中实现自己的交互器样式

问题描述 投票:0回答:1

目前,我需要使用pyqt和vtk启动我们的项目。我需要实现自己的交互风格。我从vtk.vtkInteractorStyleTrackballCamera继承了一个类。我的代码是:

import vtk, sys

from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor

from PyQt5.QtWidgets import *

class myInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):

    def __init__(self):
        super(myInteractorStyle, self).__init__()

    def OnLeftButtonDown(self):
        super(myInteractorStyle, self).OnLeftButtonDown()
        print('left button down')

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent=parent)

        self.frame = QFrame()

        self.vl = QVBoxLayout()
        self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
        self.vl.addWidget(self.vtkWidget)

        self.ren = vtk.vtkRenderer()
        self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()

        # style = vtk.vtkInteractorStyleTrackballCamera()
        style = myInteractorStyle()
        self.iren.SetInteractorStyle(style)

        # Create source
        source = vtk.vtkSphereSource()
        source.SetCenter(0, 0, 0)
        source.SetRadius(5.0)

        # Create a mapper
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(source.GetOutputPort())
        mapper.ScalarVisibilityOff()

        # Create an actor
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().SetColor(1, 1, 1)
        actor.GetProperty().ShadingOff()

        self.ren.AddActor(actor)

        self.ren.ResetCamera()

        self.frame.setLayout(self.vl)
        self.setCentralWidget(self.frame)

        self.show()
        self.iren.Initialize()


if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = MainWindow()

    sys.exit(app.exec_())

单击左键时,我希望此代码可以打印“向下按左键”,但不能。我的代码有什么问题?任何建议表示赞赏。

user-interface pyqt vtk
1个回答
0
投票

尝试:

...

class myInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
    def __init__(self, parent=None):
        self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)

    def leftButtonPressEvent(self, obj, event):
        self.OnLeftButtonDown()
        print('left button down')

...

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.