draggable window

mail@pastecode.io avatar
unknown
python
2 years ago
1.3 kB
6
Indexable
Never
#!/usr/bin/env python

import sys

from PySide2.QtCore import Qt, QEvent
from PySide2.QtWidgets import QApplication, QMainWindow


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

    def eventFilter(self, obj, event):
        if event.type() == QEvent.MouseButtonPress:
            if event.buttons() == Qt.LeftButton:
                self.old_position = event.globalPos()

                return True

        if event.type() == QEvent.MouseMove:
            if event.buttons() == Qt.LeftButton:
                delta = event.globalPos() - self.old_position
                self.move(self.x() + delta.x(), self.y() + delta.y())
                self.old_position = event.globalPos()

                return True

        return super(MainWindow, self).eventFilter(obj, event)


def main():
    app = QApplication(sys.argv)

    # Create a window.
    window = MainWindow()
    window.resize(250, 250)

    # Make it frameless
    window.setWindowFlag(Qt.FramelessWindowHint)

    # Install an event filter to filter the mouse events.
    window.installEventFilter(window)

    # Show the window and execute the app.
    window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()