微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PyQT6:'QMouseEvent' 对象没有属性 'pos'

如何解决PyQT6:'QMouseEvent' 对象没有属性 'pos'

当我尝试在屏幕上移动图像标签时,我在使用 PyQT6 时遇到了一些问题。

我正在尝试将 Scrollabel 区域中的标签移动到框架中,但出现以下错误:“PyQT6:'QMouseEvent' 对象没有属性 'pos'”

代码如下:

class DraggableLabel(QLabel):
    def init(self,parent,image):
        super(QLabel,self).init(parent)
        pixmap = Qpixmap(image)
        pixmap = pixmap.scaled(120,120)

        self.setpixmap(pixmap)
        # self.show()

    def mousepressEvent(self,event):
        if event.button() == Qt.MouseButtons.LeftButton:
            # print('Evento: ',event.screenPos())
            self.drag_start_position = event.pos()

    def mouseMoveEvent(self,event):
        if not (event.buttons() & Qt.MouseButtons.LeftButton):
            return
        if (event.pos() - self.drag_startposition).manhattanLength() < QApplication.startDragdistance():
            return

        drag = QDrag(self)
        mimedata = QMimeData()
        mimedata.setText(self.text())
        mimedata.setimageData(self.pixmap().toImage())

        drag.setMimeData(mimedata)
        pixmap = Qpixmap(self.size())
        painter = QPainter(pixmap)
        painter.drawpixmap(self.rect(),self.grab())
        painter.end()
        drag.setpixmap(pixmap)
        drag.setHotSpot(event.pos())
        drag.exec(Qt.copyAction | Qt.MoveAction)

编辑

追溯:

PS C:\Users\doug\Projetos> & C:/Python/python.exe c:/Users/doug/Projetos/main.py
qt.gui.imageio: libpng warning: iCCP: kNown incorrect sRGB profile
Traceback (most recent call last):
  File "c:\Users\doug_\Projetos\lib\sys_functions.py",line 25,in mousepressEvent
    self.drag_start_position = event.pos()
AttributeError: 'QMouseEvent' object has no attribute 'pos'

解决方法

Qt6 重构了事件输入 API 以适应新技术(阅读 https://www.qt.io/blog/input-events-in-qt-6 了解更多信息),因此它引入了新的基类,例如 QMouseEvent 继承的 QSinglePointEvent 具有 position() 方法,返回事件的位置(在本例中为鼠标)。即便如此,Qt6 的 pos() 方法是多余的,但为了兼容性而维护,但似乎 PyQt6 已经消除了它,这似乎是一个错误,因为 PySide6 仍然保持它与 Qt6 的兼容性。因此,在这种情况下,您应该使用 position() 而不是 pos()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。