pyqt5 标签在主窗口中的位置

问题描述 投票:0回答:1

这是我在 Qt Designer 中的布局:

这是这个的输出

我将图像添加到此标签区域,如下所示:

class MainWindow(QMainWindow):
    def __init__(self,parent = None):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.boxes = []  # Klonlanmış box'ların listesi

        self.img = QPixmap("box.png").scaled(100, 150)  # Orjinal box resmi
        print( self.img.width(),self.img.height())
        self.ui.image.setPixmap(self.img)

        self.dragging = False  
        self.offset = None  

我想获取此标签上的点击位置,但它给了我与 event.pos() 不同的位置

  def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
           print(event.pos())
           print(self.ui.image.mapTo(self, self.ui.image.pos()))
           print(self.ui.image.pos())

这不一样

我需要匹配鼠标点击位置和标签位置。我需要检测这个框是否被点击。在这种布局中这怎么可能?

python pyqt5
1个回答
0
投票

您可以将

event.globalPos()
与标签坐标结合使用。

  def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
           print(event.globalPos())
           print(self.ui.image.mapTo(self, self.ui.image.pos()))
           print(self.ui.image.pos())
© www.soinside.com 2019 - 2024. All rights reserved.