python - 在 PyQtGraph 和 PySide2 中使用 ImageView 修复文本位置
问题描述
我正在使用 PyQtGraph 和 PySide2 显示平面 2D 图像和 3D 体积的 2D 切片(CT/MRI 体积数据集等),用户可以在其中平移/缩放、滚动等。
我想做的是在视图中的几个位置放置文本,覆盖图像,例如在角落——我可以指定的地方。无论图像平移/缩放等如何,我都希望此文本保持其屏幕位置。我还希望在用户进行查看更改时实时更新其中的一些文本(例如查看参数,如像素大小)
据我所知,最合适的选项是 LegendItem。有问题——
- 如何禁用用户拖动?
- 如何控制文本位置?.setPos() 方法无效。
- 如何删除/隐藏图例文本左侧的行?
替代方法是 LabelItem 或 TextItem,但我找不到分配屏幕位置而不是图像位置的方法。即-我将如何指定视图窗口的左下角而不是图像的左下角-因为当然,图像可以移动。
- 有没有办法固定相对于视口的标签/文本位置?
有趣的是,LabelItem 随图像平移和缩放,而 TextItem 仅随图像平移。
这是我的最低工作代码,其中包含每个文本事物的示例。
from PySide2.QtWidgets import QApplication
from PySide2.QtWidgets import QMainWindow
from PySide2.QtWidgets import QWidget
from PySide2.QtWidgets import QHBoxLayout
import pyqtgraph as pg
import numpy as np
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.cw = QWidget(self)
self.cw.setAutoFillBackground(True)
self.setCentralWidget(self.cw)
self.layout = QHBoxLayout()
self.cw.setLayout(self.layout)
self.DcmImgWidget = MyImageWidget(parent=self)
self.layout.addWidget(self.DcmImgWidget)
self.show()
class MyImageWidget(pg.ImageView):
def __init__(self, parent):
super().__init__(parent, view=pg.PlotItem())
self.ui.histogram.hide()
self.ui.roiBtn.hide()
self.ui.menuBtn.hide()
plot_view = self.getView()
plot_view.hideAxis('left')
plot_view.hideAxis('bottom')
# 50 frames of 100x100 random noise
img = np.random.normal(size=(50, 100, 100))
self.setImage(img)
text0 = pg.LabelItem("this is a LabelItem", color=(128, 0, 0))
text0.setPos(25, 25) # <---- These are coords within the IMAGE
plot_view.addItem(text0)
text1 = pg.TextItem(text='This is a TextItem', color=(0, 128, 0))
plot_view.addItem(text1)
text1.setPos(75, -20) # <---- These are coords within the IMAGE
legend = plot_view.addLegend()
style = pg.PlotDataItem(pen='w')
legend.addItem(style, 'legend')
def main():
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
解决方案
一个可能的解决方案是在 ImageView 使用的 QGraphicsView 的视口中添加一个 QLabel:
class MyImageWidget(pg.ImageView):
def __init__(self, parent):
super().__init__(parent, view=pg.PlotItem())
self.ui.histogram.hide()
self.ui.roiBtn.hide()
self.ui.menuBtn.hide()
plot_view = self.getView()
plot_view.hideAxis("left")
plot_view.hideAxis("bottom")
# 50 frames of 100x100 random noise
img = np.random.normal(size=(50, 100, 100))
self.setImage(img)
label = QLabel("this is a QLabel", self.ui.graphicsView.viewport())
label.move(25, 25)
推荐阅读
- python - 尝试调用 image_viewer 模块时获取“_tkinter.TclError:图像“pyimage1”不存在”
- ios - Swift - 如何以编程方式访问 collectionView 的 targetContentOffset.pointee.y
- javascript - DiscordJS - 异步响应消息
- scala - Spark Zepplin 在数据集上抛出 NPE
- sql - SQL:选择里面选择?
- python - 找出一个数字末尾有多少个零(数字为 0 时字符串索引超出范围错误)
- javascript - 如何使用通过 JS 控制台添加的脚本来修改 Angular 页面中的表单内容以保存更改?
- flutter - Upwork 将 Flutter App 开发到 Play Store 和 Iphone Apps Store
- reactjs - 我正在尝试使用 npm 安装新组件。但它给出了错误
- python - Tkinter Listbox - 在列表框中选择 ALL 而不是我选择的