当我尝试将图像标签移动到屏幕上时,我在 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'