Reputation: 79
I am using PyQtGraph and PySide2 to display flat 2D images and 2D slices of 3D volumes (CT/MRI volume datasets etc.) where the user can pan/zoom, scroll etc.
What I would like to do is have text in a few locations within the view, overlying the image, for example in the corner- somewhere I can specify. I want this text to maintain its screen position, regardless of image pan/zoom etc. I also want to live update some of this text as the user makes viewing changes (e.g. viewing parameters such as pixel size)
As far as I can see, the most appropriate option is a LegendItem. There are problems-
The alternative would be a LabelItem or TextItem, though I can't find a way to assign a screen position as opposed to an image location. ie- how would I specify bottom left corner of view window as opposed to bottom left of image - because of course, the image can move.
-Is there a way of fixing the Label/Text position relative to the viewport?
Interestingly the LabelItem pans & zooms with the image, whereas the TextItem only pans with the image.
Here is my minimum working code with examples of each text thing.
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()
Upvotes: 1
Views: 850
Reputation: 244301
A possible solution is to add a QLabel to the viewport of the QGraphicsView used by ImageView:
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)
Upvotes: 1