VTK:如何向我的项目添加滚动条?

发布于 2024-09-27 11:34:37 字数 1317 浏览 0 评论 0原文

向我的 VTK 项目添加滚动条的最简单方法是什么?

谢谢

更新

def vtkSliderCallback2(obj, event):
    sliderRepres = obj.GetRepresentation()
    pos = sliderRepres.GetValue()
    contourFilter.SetValue(0, pos)

SliderRepres = vtk.vtkSliderRepresentation2D()
min = 0 #ImageViewer.GetSliceMin()
max = 256 #ImageViewer.GetSliceMax()
SliderRepres.SetMinimumValue(min)
SliderRepres.SetMaximumValue(max)
SliderRepres.SetValue((min + max) / 2)
SliderRepres.SetTitleText("Slice")
SliderRepres.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
SliderRepres.GetPoint1Coordinate().SetValue(0.2, 0.6)
SliderRepres.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
SliderRepres.GetPoint2Coordinate().SetValue(0.4, 0.6)

SliderRepres.SetSliderLength(0.02)
SliderRepres.SetSliderWidth(0.03)
SliderRepres.SetEndCapLength(0.01)
SliderRepres.SetEndCapWidth(0.03)
SliderRepres.SetTubeWidth(0.005)
SliderRepres.SetLabelFormat("%3.0lf")
SliderRepres.SetTitleHeight(0.02)
SliderRepres.SetLabelHeight(0.02)

SliderWidget = vtk.vtkSliderWidget()
SliderWidget.SetInteractor(iren)
SliderWidget.SetRepresentation(SliderRepres)
SliderWidget.KeyPressActivationOff()
SliderWidget.SetAnimationModeToAnimate()
SliderWidget.SetEnabled(True)
SliderWidget.AddObserver("InteractionEvent", vtkSliderCallback2)

What's the easiest way to add a scrollbar to my VTK project ?

thanks

Update

def vtkSliderCallback2(obj, event):
    sliderRepres = obj.GetRepresentation()
    pos = sliderRepres.GetValue()
    contourFilter.SetValue(0, pos)

SliderRepres = vtk.vtkSliderRepresentation2D()
min = 0 #ImageViewer.GetSliceMin()
max = 256 #ImageViewer.GetSliceMax()
SliderRepres.SetMinimumValue(min)
SliderRepres.SetMaximumValue(max)
SliderRepres.SetValue((min + max) / 2)
SliderRepres.SetTitleText("Slice")
SliderRepres.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
SliderRepres.GetPoint1Coordinate().SetValue(0.2, 0.6)
SliderRepres.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
SliderRepres.GetPoint2Coordinate().SetValue(0.4, 0.6)

SliderRepres.SetSliderLength(0.02)
SliderRepres.SetSliderWidth(0.03)
SliderRepres.SetEndCapLength(0.01)
SliderRepres.SetEndCapWidth(0.03)
SliderRepres.SetTubeWidth(0.005)
SliderRepres.SetLabelFormat("%3.0lf")
SliderRepres.SetTitleHeight(0.02)
SliderRepres.SetLabelHeight(0.02)

SliderWidget = vtk.vtkSliderWidget()
SliderWidget.SetInteractor(iren)
SliderWidget.SetRepresentation(SliderRepres)
SliderWidget.KeyPressActivationOff()
SliderWidget.SetAnimationModeToAnimate()
SliderWidget.SetEnabled(True)
SliderWidget.AddObserver("InteractionEvent", vtkSliderCallback2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2024-10-04 11:34:37

只是为了完整并供其他用户使用谷歌搜索。

如果您需要 vtkSliderWidget 设置值,它会执行您想要的操作。

//根据您的编辑进行编辑
如果您想获取该值,则必须将一个事件连接到滑块,该事件在值更改时触发。然后检索该值并进行相应更新。 C++ 示例位于 此处

// 我实际上认为我的问题是当我滑动拇指位置时会调用回调函数。如何避免呢?换句话说,我只希望最后一个位置触发回调函数...

尝试将其耦合到 EndInteractionEvent 而不是 InteractionEvent。

SliderWidget.AddObserver("EndInteractionEvent", vtkSliderCallback2)

// stuff

顺便说一下,如果你使用 python 和 VTK 并且需要 GUI 的东西,我建议你使用 python QT 和 python qt 小部件,它们可以简化很多这些东西。我的一个旧项目的一些代码使用 QT+Python+VTK 进行 GUI + python 的东西:

self.verticalSlider = QtGui.QSlider(self.centralwidget)
self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider.setObjectName("verticalSlider")
self.horizontalLayout.addWidget(self.verticalSlider)

// connect slider to a method onValueChange
QObject.connect(self.verticalSlider, SIGNAL("valueChanged(int)"), 
self.setFibreVolumeOpacity)

def setFibreVolumeOpacity(self, value):
    // do stuff here with slider value.

Just to be complete and for other users googling thing.

vtkSliderWidget will do what you want if you need it to set a value.

//edit based on your edit
If you want to get the value, you have to connect an event to the slider which is fired when the value is changed. Than retrieve this value and update accordingly. An example in C++ is found here

// I actually think my issue is that the callback function is invoked for each thumb position when I slide it. How can avoid that ? In other words I only want the last position to trigger the callback function...

Try coupling it to the EndInteractionEvent instead of to the InteractionEvent.

SliderWidget.AddObserver("EndInteractionEvent", vtkSliderCallback2)

// stuff

By the way, if you use python and VTK and need GUI stuff, I advice you to use the python QT and python qt widgets which ease up alot of this stuff. Some code of one of my old projects using QT+Python+VTK for GUI + python stuff:

self.verticalSlider = QtGui.QSlider(self.centralwidget)
self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider.setObjectName("verticalSlider")
self.horizontalLayout.addWidget(self.verticalSlider)

// connect slider to a method onValueChange
QObject.connect(self.verticalSlider, SIGNAL("valueChanged(int)"), 
self.setFibreVolumeOpacity)

def setFibreVolumeOpacity(self, value):
    // do stuff here with slider value.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文