[英]my QRubberBand coordinates doesnt correspond with the ones of my image
我在QLabel
對象中有一個QPixmap
對象。 我想繪制該像素圖矩形,以便我可以在圖像中獲取該矩形的坐標。 我的問題是,當我在該像素圖中繪制該qrubberband矩形時,它沒有獲得圖像的原始坐標。 越來越多的人,我不知道為什么。 這是我創建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())
在此先感謝您。
QMouseEvent.pos()返回事件相對於捕獲事件的小部件的位置。 因此,取決於標簽的位置,可能不是您想要的。
同樣,QLabel.pos()返回標簽相對於標簽所在位置的位置
嘗試將self.myimagelabel.pos()的標簽位置與鼠標事件的位置進行比較
我的猜測是,您將需要執行諸如labelPos = myimagelabel.pos()-eventQMouseEvent.pos()之類的操作,以獲取事件相對於圖像的坐標
只是一個猜測,一個最小的工作示例將有所幫助
要嘗試的另一件事是子類QLabel並實現qmousepressevent。 類似於他們在這里所做的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.