简体   繁体   中英

my QRubberBand coordinates doesnt correspond with the ones of my image

I have a QPixmap object in a QLabel object. I want to draw in that pixmap rectangles so that i can get the coordinates of that rectangle in the image. My problem is that when I draw that qrubberband rectangle in that pixmap, it is not getting the original coordinates of the image. It is getting others and I dont know why. Here is my code where I create my qrubberband.

def mousePressEvent (self, eventQMouseEvent):
 if eventQMouseEvent.buttons() == Qt.LeftButton:
    self.originQPoint = eventQMouseEvent.pos()
        self.currentQRubberBand = QRubberBand(QRubberBand.Rectangle, self)
    self.sequence.append(self.currentQRubberBand)

    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)
    palette = QPalette()
    palette.setColor(self.currentQRubberBand.foregroundRole(), QColor(r, g, b))
    self.currentQRubberBand.setPalette(palette)

    self.currentQRubberBand.setGeometry(QRect(self.originQPoint, QSize()))
    self.currentQRubberBand.show()

 elif eventQMouseEvent.buttons() == Qt.RightButton:
    found = False
    for rect in self.sequence:
        if(rect.geometry().contains(eventQMouseEvent.pos())):
            self.menu = MenuRectangle(self._timeline, eventQMouseEvent, rect, 
                            self.imageLabel, self.sequence)
            found = True
    if not found:
        self.menuglobalimage = MenuGlobalImage(self._timeline, eventQMouseEvent, self._topic, 
                            self.sequence, self.imageLabel, self.stamp)


def mouseMoveEvent (self, eventQMouseEvent):
    self.currentQRubberBand.setGeometry(QRect(self.originQPoint, eventQMouseEvent.pos()).normalized())

Thank you in advanced.

QMouseEvent.pos() returns the position of the event relative to the widget that catches the event. So depending on where your label is, that might not be what you want.

likewise, QLabel.pos() returns the position of the label relative to the widget it's positioned in

Try comparing the position of the label with self.myimagelabel.pos() with the position of the mouse event

my guess is you will need to do something like labelPos = myimagelabel.pos() - eventQMouseEvent.pos() to get the coordinates of the event relative to the image

just a guess though, a minimal working example would help

An additional thing to try would be to subclass QLabel and implement a qmousepressevent. Similar to what they do here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM