pyqt5_控件_自定义无边框窗体拖动

2021-02-15 22:16

阅读:566

标签:false   bsp   pre   set   move   art   ==   qt5   tracking   

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys


class Example(QWidget):
    _startPos = None
    _endPos = None
    _isTracking = False

    def __init__(self):
        super().__init__()
        self._initUI()

    def _initUI(self):
        self.setFixedSize(QSize(400, 400))
        self.setWindowFlags(Qt.FramelessWindowHint)  # 无边框
        self.show()

    def mouseMoveEvent(self, e: QMouseEvent):  # 重写移动事件
        self._endPos = e.pos() - self._startPos
        self.move(self.pos() + self._endPos)

    def mousePressEvent(self, e: QMouseEvent):
        if e.button() == Qt.LeftButton:
            self._isTracking = True
            self._startPos = QPoint(e.x(), e.y())

    def mouseReleaseEvent(self, e: QMouseEvent):
        if e.button() == Qt.LeftButton:
            self._isTracking = False
            self._startPos = None
            self._endPos = None


if __name__ == __main__:
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

 

pyqt5_控件_自定义无边框窗体拖动

标签:false   bsp   pre   set   move   art   ==   qt5   tracking   

原文地址:https://www.cnblogs.com/alplf123/p/8425704.html


评论


亲,登录后才可以留言!