pyqt6+vtk

这里用PyQt6+vtk9.2.6

pip install PyQt6 pyqt6-tools vtk

这里拉了一个水平布局
然后水平布局中加入QWidget,object name改为vtkWidget
pyqt6+vtk_第1张图片
右键Promote Widgets
Promoted class name: QVTKRenderWindowInteractor
Header file: vtkmodules.qt.QVTKRenderWindowInteractor
点击Add,然后Promote
pyqt6+vtk_第2张图片

最终的ui如下


<ui version="4.0">
 <class>Formclass>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0x>
    <y>0y>
    <width>400width>
    <height>300height>
   rect>
  property>
  <property name="windowTitle">
   <string>Formstring>
  property>
  <widget class="QWidget" name="horizontalLayoutWidget">
   <property name="geometry">
    <rect>
     <x>0x>
     <y>0y>
     <width>401width>
     <height>301height>
    rect>
   property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QVTKRenderWindowInteractor" name="vtkWidget" native="true"/>
    item>
   layout>
  widget>
 widget>
 <customwidgets>
  <customwidget>
   <class>QVTKRenderWindowInteractorclass>
   <extends>QWidgetextends>
   <header>vtkmodules.qt.QVTKRenderWindowInteractorheader>
   <container>1container>
  customwidget>
 customwidgets>
 <resources/>
 <connections/>
ui>

pyqt6+vtk_第3张图片

然后通过pyuic转成py

python -m PyQt6.uic.pyuic MyWidget.ui -o MyWidget.py

然后就是main.py了

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from PyQt6 import QtGui
from PyQt6.QtWidgets import QWidget, QApplication
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderer
)

from MyWidget import Ui_Form


class MyWindow(QWidget, Ui_Form):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.setupUi(self)

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

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

        # Create an actor
        actor = vtkActor()
        actor.SetMapper(mapper)

        self.ren = vtkRenderer()
        self.ren.AddActor(actor)

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

        self.ren.ResetCamera()

        self.iren.Initialize()
        self.iren.Start()

    def closeEvent(self, event: QtGui.QCloseEvent) -> None:
        self.vtkWidget.Finalize()


if __name__ == '__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

效果
pyqt6+vtk_第4张图片

注意事项:
这2句一定要写

import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingOpenGL2

然后就是这个初始化也一定要写

self.iren.Initialize()
self.iren.Start()

最后的Finalize()也是

参考:
https://kitware.github.io/vtk-examples/site/Python/Widgets/EmbedInPyQt/
https://kitware.github.io/vtk-examples/site/Python/Tutorial/Tutorial_Step5/

你可能感兴趣的:(qt,pyqt,vtk)