QMouseEvent' 对象没有属性 'pos'

时间:2023-03-11
本文介绍了QMouseEvent' 对象没有属性 'pos'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I'm having some problems here with PyQT6 while i try to move a image label trought the screen.

我试图将 Scrollabel 区域中的标签移动到框架中,但出现以下错误:PyQT6: 'QMouseEvent' object has no attribute 'pos'"

I'm trying to move a label that is in a Scrollabel Area to a frame, and i get the following error: "PyQT6: 'QMouseEvent' object has no attribute '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:UsersdougProjetos> & 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:Usersdoug_Projetoslibsys_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 了解更多信息)所以它引入了新的基础诸如 QSinglePointEvent 之类的 QMouseEvent 继承自的类具有返回事件位置(在本例中为鼠标)的 position() 方法.即便如此,Qt6 有 pos() 方法是多余的,但为了兼容性而维护,但似乎 PyQt6 已经消除了它,这似乎是一个错误,因为 PySide6 仍然保持它与 Qt6 的兼容性.所以在这种情况下,你应该使用 position() 而不是 pos().

Qt6 has refactored the event inputs API to adapt to new technologies (read https://www.qt.io/blog/input-events-in-qt-6 for more information) so it has introduced new base classes such as QSinglePointEvent from which QMouseEvent inherits that have the position() method that returns the position of the event (in this case the mouse). Even so, Qt6 has the pos() method that is redundant but is maintained for compatibility but it seems that PyQt6 has eliminated it which seems like a bug since PySide6 still maintains it having compatibility with Qt6. So in this case you should use position() instead of pos().

这篇关于QMouseEvent' 对象没有属性 'pos'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何在 Windows 上读取 Python 中的系统信息? 下一篇:Qthread中的Python访问父类的小部件

相关文章