Pramod Butte
Pramod Butte

Reputation: 21

Getting the exact data index from a click on pyqt5graph image (not just pixel value)

I am new to PyqtGraph ( Infact this is my first time)

I have a Qt designer file which I import in my python code. I 6 windows in which I plot a 42x22 (different sizes) as an inmage, these have been promoted the graphicview.

I have a data set which is 6x42x22 and so I use a for loop to plot the 6 images

 for n in range(imageStackSize):
            self.glayout = pg.GraphicsLayout()
            self.vb = self.glayout.addViewBox()
            self.vb.setAspectLocked(lock=True, ratio=self.aspect_ratio)
            img_temp = image[n, :, :]

and... ``

img = pg.ImageItem(img_temp,  lut=self.jet_lut)
                if n == 0:
                    self.ui.Channel1_img.setCentralItem(self.glayout)
                    self.vb.addItem(img)

            elif n == 1:
                self.ui.Channel2_img.setCentralItem(self.glayout)
                self.vb.addItem(img)

            elif n == 2:
                self.ui.Channel3_img.setCentralItem(self.glayout)
                self.vb.addItem(img)
            elif n == 3:
                self.ui.Channel4_img.setCentralItem(self.glayout)
                self.vb.addItem(img)
            elif n == 4:
                self.ui.Channel5_img.setCentralItem(self.glayout)
                self.vb.addItem(img)
            elif n == 5:
                self.ui.Channel6_img.setCentralItem(self.glayout)
                self.vb.addItem(img)

After this I am trying to click on one of the image (ideally I would like to make it such that I can click any of the six images) to get the (6,x,y) coordinated the first dimension does not matter. In order to achieve this I did

self.ui.Channel1_img.scene().sigMouseClicked.connect(self.onClick)
    #self.ui.Channel2_img.scene().sigMouseClicked.connect(self.onClick)
    #self.ui.Channel3_img.scene().sigMouseClicked.connect(self.onClick)
    #self.ui.Channel4_img.scene().sigMouseClicked.connect(self.onClick)
    #self.ui.Channel5_img.scene().sigMouseClicked.connect(self.onClick)
    #self.ui.Channel6_img.scene().sigMouseClicked.connect(self.onClick)
    #self.ui.PMTVoltage_plt.scene().sigMouseClicked.connect(self.onClick)

def onClick(self, event): print("clicked")

and then I tried

items = self.ui.Channel1_img.imageItem.mapFromViewToItem(event.pos())

and

 items = self.ui.Channel1_img.imageItem.mapFromSceneToView(event.pos())

but the prog just crashes. I read somewhere that the coordinates are in the viewbox, but I cant seem to find the viewbox or vb in the self.ui.Channel1_img

I went through the entire ui variable in debug to look for vb or image Item and could not find it.

infact the only thing I found was,

items = {dict} {<pyqtgraph.graphicsItems.ViewBox.ViewBox.ViewBox 
  object at 0x000001CF73888CA8>: [(0, 0)]}
  <pyqtgraph.graphicsItems.ViewBox.ViewBox.ViewBox object at 
   0x000001CF73888CA8> (1990508186792) = {list} <class 'list'>: [(0, 0)]
   0 = {tuple} <class 'tuple'>: (0, 0)
   0 = {int} 0
   1 = {int} 0
    __len__ = {int} 2
    __len__ = {int} 1
    __len__ = {int} 1

what am I missing? Any help is appreciated

Upvotes: 1

Views: 791

Answers (1)

Pramod Butte
Pramod Butte

Reputation: 21

Ok I figured it out, here is my solution for some who may have same question

I plot the data using it as imageItem

vb = pg.ViewBox()
    self.ui.PMTVoltage_plt.useOpenGL()
    self.ui.PMTVoltage_plt.setCentralItem(vb)
    self.img = pg.ImageItem(pmtImage)
    vb.addItem(self.img)

and then in other function I recover the vb using getViewBox() and then use mapFromViewtoItem()

vb = self.ui.PMTVoltage_plt.getViewBox()
    items = vb.mapSceneToView(event.scenePos())
    pixels = vb.mapFromViewToItem(self.img, items)
    print(items)

Hope this helps

Upvotes: 1

Related Questions