2

I'm having some problems here with PyQT6 while i try to move a image label trought the screen.

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' "

Here is the code:

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)

Edit

Traceback:

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'
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Diego Baggio
  • 39
  • 2
  • 10
  • Please include the full traceback error in your post. – ewokx May 12 '21 at 01:51
  • @ewong Done. Trackeback was inserted – Diego Baggio May 12 '21 at 01:56
  • `PyQt6` can be different then `PyQt5` and it may have different names or elements. You could use `print( dir(event) )` to see all function avaliable in `event` – furas May 12 '21 at 02:00
  • 1
    using Google I found that now it may be `.position()` - [About PyQt6 and PySide6](https://www.mfitzp.com/forum/t/about-pyqt6-and-pyside6/744/4) – furas May 12 '21 at 02:06

2 Answers2

4

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().

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

I found and modified a similar piece of PyQt5 code, author = "Deokyu Lim", to PySide6:

    def mouseMoveEvent(self, e: QMouseEvent):
        
        if e.buttons() != Qt.RightButton:
            return

        mime_data = QMimeData()
        mime_data.setData("application/hotspot",
                          b"%d %d" % (e.position().x(), e.position().y()))

        drag = QDrag(self)
        
        drag.setMimeData(mime_data)
        
        pixmap = QPixmap(self.size())
        self.render(pixmap)
        drag.setPixmap(pixmap)

        drag.setHotSpot(e.position().toPoint() - self.rect().topLeft())
        drag.exec(Qt.MoveAction)

And e.position() had the wrong signature in PySide6 and required to be e.position().toPoint() for the code to be acceptable and function. As I haven't earned the comment privilege, I have added this answer. Maybe this isn't correct but the solution above appeared to work correctly and may be helpful to others.

Sildeag
  • 74
  • 9