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'


基础教程推荐
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01