Hi,
I am trying to using vtkTextWidget in QT.
I tried to combine these two example.
https://kitware.github.io/vtk-examples/site/Python/Widgets/TextWidget/
https://kitware.github.io/vtk-examples/site/Python/Widgets/EmbedInPyQt/
Here is my code:
#!/usr/bin/env python
# Contributed by Eric E Monson
import sys
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QPushButton, QLineEdit, QApplication
from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkInteractionWidgets import vtkTextWidget, vtkTextRepresentation
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderer, vtkRenderWindow, vtkRenderWindowInteractor, vtkTextActor
)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(600, 600)
okButton = QPushButton("OK")
cancelButton = QPushButton("Cancel")
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.gridlayout = QtWidgets.QGridLayout(self.centralWidget)
self.vtkWidget = QVTKRenderWindowInteractor(self.centralWidget)
self.gridlayout.addWidget(self.vtkWidget, 0, 0, 1, 1)
# self.gridlayout.addWidget(okButton, 1, 1, 2, 2)
# self.gridlayout.addWidget(cancelButton, 2, 1, 2, 2)
MainWindow.setCentralWidget(self.centralWidget)
class SimpleView(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ren = vtkRenderer()
self.ui.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.ui.vtkWidget.GetRenderWindow().GetInteractor()
###################################################################
colors = vtkNamedColors()
# colors.SetColor('bkg', [0.1, 0.2, 0.4, 1.0])
source = vtkSphereSource()
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(colors.GetColor3d('MistyRose'))
# renderer = vtkRenderer()
# render_window = vtkRenderWindow()
# render_window.AddRenderer(renderer)
# interactor = vtkRenderWindowInteractor()
# interactor.SetRenderWindow(render_window)
# Create the TextActor
text_actor = vtkTextActor()
text_actor.SetInput('This is a test')
text_actor.GetTextProperty().SetColor(colors.GetColor3d('Lime'))
# Create the text representation. Used for positioning the text_actor
text_representation = vtkTextRepresentation()
text_representation.GetPositionCoordinate().SetValue(0.15, 0.15)
text_representation.GetPosition2Coordinate().SetValue(2, 2)
# Create the TextWidget
# Note that the SelectableOff method MUST be invoked!
# According to the documentation :
#
# SelectableOn/Off indicates whether the interior region of the widget can be
# selected or not. If not, then events (such as left mouse down) allow the user
# to 'move' the widget, and no selection is possible. Otherwise the
# SelectRegion() method is invoked.
text_widget = vtkTextWidget()
text_widget.SetRepresentation(text_representation)
text_widget.SetInteractor(self.iren)
text_widget.SetTextActor(text_actor)
text_widget.SelectableOff()
self.ren.AddActor(actor)
self.ren.AddActor(text_actor)
# renderer.AddActor(actor)
# renderer.SetBackground(colors.GetColor3d('MidnightBLue'))
# # render_window.SetSize(300, 300)
# # render_window.SetWindowName('TextWidget')
#
# interactor.Initialize()
# # render_window.Render()
text_widget.On()
# interactor.Start()
###################################################################
# # 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.AddActor(actor)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = SimpleView()
window.show()
window.iren.Initialize() # Need this line to actually show the render inside Qt
sys.exit(app.exec_())
Everything works well, EXCEPT that the textwidget is not selectable and dragable.
Thanks!