問(wèn)題描述
當(dāng)我嘗試將圖像標(biāo)簽移動(dòng)到屏幕上時(shí),我在使用 PyQT6 時(shí)遇到了一些問(wèn)題.
I'm having some problems here with PyQT6 while i try to move a image label trought the screen.
我試圖將 Scrollabel 區(qū)域中的標(biāo)簽移動(dòng)到框架中,但出現(xiàn)以下錯(cuò)誤: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 重構(gòu)了事件輸入 API 以適應(yīng)新技術(shù)(閱讀 https://www.qt.io/blog/input-events-in-qt-6 了解更多信息)所以它引入了新的基礎(chǔ)諸如 QSinglePointEvent 之類(lèi)的 QMouseEvent 繼承自的類(lèi)具有返回事件位置(在本例中為鼠標(biāo))的 position()
方法.即便如此,Qt6 有 pos()
方法是多余的,但為了兼容性而維護(hù),但似乎 PyQt6 已經(jīng)消除了它,這似乎是一個(gè)錯(cuò)誤,因?yàn)?PySide6 仍然保持它與 Qt6 的兼容性.所以在這種情況下,你應(yīng)該使用 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()
.
這篇關(guān)于QMouseEvent' 對(duì)象沒(méi)有屬性 'pos'的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!