【发布时间】:2021-08-02 09:17:34
【问题描述】:
当我尝试将图像标签移动到屏幕上时,我在使用 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'
【问题讨论】:
-
请在您的帖子中包含完整的回溯错误。
-
@ewong 完成。引用已插入
-
PyQt6可能与PyQt5不同,并且它可能具有不同的名称或元素。您可以使用print( dir(event) )查看event中的所有可用功能 -
用谷歌发现现在可能是
.position()- About PyQt6 and PySide6