【问题标题】:my QRubberBand coordinates doesnt correspond with the ones of my image我的 QRubberBand 坐标与我的图像不对应
【发布时间】:2018-11-21 16:04:27
【问题描述】:

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

先谢谢你了。

【问题讨论】:

标签: qt pyqt qt5 qpixmap rqt


【解决方案1】:

QMouseEvent.pos() 返回事件相对于捕获事件的小部件的位置。因此,根据您的标签所在的位置,这可能不是您想要的。

同样,QLabel.pos() 返回标签相对于它所在的小部件的位置

尝试用self.myimagelabel.pos()比较标签的位置和鼠标事件的位置

我的猜测是你需要做类似 labelPos = myimagelabel.pos() - eventQMouseEvent.pos() 来获取事件相对于图像的坐标

只是一个猜测,一个最小的工作示例会有所帮助

要尝试的另一件事是继承 QLabel 并实现 qmousepressevent。类似于他们所做的here

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多