我使用PyQtGraph和PySide2来显示平面的二维图像和三维体积的二维切片(CT/MRI体积数据集等)。在那里,用户可以平移/缩放、滚动等。
我想要做的是在视图中的几个位置设置文本,在图像上,例如在角落--我可以指定的地方。我希望这个文本保持其屏幕位置,而不考虑图像的平移/缩放等。我也希望在用户进行查看更改(例如,查看参数,例如像素大小)时,实时更新其中的一些文本。
据我所见,最合适的选择是LegendItem。有问题-
user-dragging?
另一种选择是LabelItem或TextItem,尽管我无法找到一种方法来分配屏幕位置,而不是图像位置。我将如何指定视图窗口的左下角,而不是图像的左下角--因为图像可以移动。
-Is有一种方法来固定标签/文本相对于视图的位置吗?
有趣的是,LabelItem pans &缩放图像,而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()
发布于 2021-02-22 21:58:43
一个可能的解决方案是将一个QLabel添加到ImageView使用的QGraphicsView的视口:
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)
https://stackoverflow.com/questions/66327667
复制相似问题