Display text in a fixed location relative to the window in an ImageView

82 views
Skip to first unread message

Oli N

unread,
Feb 19, 2021, 1:04:26 AM2/19/21
to pyqtgraph

Hi-
 I am trying to display 2D and 3D image datasets within an ImageView, with some metadata being displayed in the window corners (such as min/max intensity levels as per setLevels() method etc).
I expect these values to change during viewing so was hoping a straightforward way of doing so.

The best way I can see so far is to use the legendItem, however I can't find a way to prevent the user being able to click & drag it, and editing the text is a bit of a pain (seems like you have to use legend.getLabel(my_view) to get the label object to then setText(). Awkward.)

Alternatively, the LabelItem seems easier to change text, but:
1- I can't find a way to consistently locate the text relative to the window- it always moves when I pan/zoom.
2- I can't see how to get the correct coords relative to the window. I tried the mapToScene(), though I can't see a difference in behaviour compared to when I use mapFromScene() which leads me to believe I am not using the QGraphicsView object but something else...

Is there a way to fix text position on the screen so that it doesn't move with pan/zoom?

Sample code below, with sample legend, textItem and labelItem.  The labelItem moves and even zooms with the image. The textItem moves with the image.

How can I:
1- set Legend location
2- prevent the user moving the legend?

Thanks!


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__(selfparent):
        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=(50100100))
        self.setImage(img)

        plot_view.addItem(pg.LabelItem("this is a nice label"))

        text1 = pg.TextItem(text='text1'color=(01280))
        plot_view.addItem(text1)
        text1.setPos(75, -20)

        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()







Reply all
Reply to author
Forward
0 new messages