QMouseEvent#39; object has no attribute #39;pos#39;(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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:QMouseEvent' 对象没有属性 'pos'


- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01