目录

初始化简单窗口

实现目标:使用窗体来移动窗口位置,且点击按钮不会移动窗口。 

复杂窗口

 实现目标:使用中间Widget窗体来移动窗口位置,且点击按钮和周边不会移动窗口。 


初始化简单窗口

class Mains(QWidget):
    def __init__(self, parent=None):
        super(Mains, self).__init__(parent)
        self.resize(400, 300)
        button = QPushButton(self)
        button.setText("搜索")


if __name__ == '__main__':
    app = QApplication([])
    main = Mains()
    main.show()
    app.exec()

Qt第三十五章:窗口移动_ide

实现目标:使用窗体来移动窗口位置,且点击按钮不会移动窗口。 

class Mains(QWidget):
    def __init__(self, parent=None):
        super(Mains, self).__init__(parent)
        self.resize(400, 300)
        button = QPushButton(self)
        button.setText("搜索")

        # 是否正在移动
        self.flag_is_moving = False
        # 窗口的起始绝对位置
        self.window_absolute_start = self.pos()
        # 鼠标的起始绝对位置
        self.mouse_absolute_start = QPoint(0, 0)
        # 鼠标的起始相对位置
        self.mouse_relative_start = QPoint(0, 0)

    def resizeEvent(self, event: PySide6.QtGui.QResizeEvent) -> None:
        return super().resizeEvent(event)

    def mousePressEvent(self, event: PySide6.QtGui.QMouseEvent) -> None:
        # 窗口开始移动
        self.flag_is_moving = True
        # 窗口的起始绝对位置
        self.window_absolute_start = self.pos()
        # 鼠标的起始绝对位置
        self.mouse_absolute_start = event.globalPosition().toPoint()
        # 鼠标的起始相对位置
        self.mouse_relative_start = event.position().toPoint()
        return super().mousePressEvent(event)

    def mouseMoveEvent(self, event: PySide6.QtGui.QMouseEvent) -> None:
        if self.flag_is_moving:
            # 当前鼠标的绝对位置
            point = event.globalPosition().toPoint()
            # 窗口绝对位置=窗口的绝对起始位置+(窗口的移动距离/鼠标的移动距离)
            # 窗口绝对位置=窗口的绝对起始位置+(鼠标的当前绝对位置-鼠标的绝对起始位置)
            self.move(self.window_absolute_start.x() + point.x() - self.mouse_absolute_start.x(),
                      self.window_absolute_start.y() + point.y() - self.mouse_absolute_start.y())
        return super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event: PySide6.QtGui.QMouseEvent) -> None:
        # 停止窗口移动
        self.flag_is_moving = False
        return super().mouseReleaseEvent(event)


if __name__ == '__main__':
    app = QApplication([])
    main = Mains()
    main.show()
    app.exec()

复杂窗口

class Mains(QWidget):
    def __init__(self, parent=None):
        super(Mains, self).__init__(parent)
        self.resize(400, 300)
        widget = QWidget(self)
        widget.setObjectName("widget")
        widget.setGeometry(50, 50, 300, 200)
        widget.setStyleSheet("QWidget#widget{background-color:rgb(100,100,100)}")
        button = QPushButton(widget)
        button.setText("搜索")


if __name__ == '__main__':
    app = QApplication([])
    main = Mains()
    main.show()
    app.exec()

Qt第三十五章:窗口移动_移动窗口_02

 实现目标:使用中间Widget窗体来移动窗口位置,且点击按钮和周边不会移动窗口。 

class Mains(QWidget):
    def __init__(self, parent=None):
        super(Mains, self).__init__(parent)
        self.resize(400, 300)
        widget = QWidget(self)
        widget.setObjectName("widget")
        widget.setGeometry(50, 50, 300, 200)
        widget.setStyleSheet("QWidget#widget{background-color:rgb(100,100,100)}")
        button = QPushButton(widget)
        button.setText("搜索")

        # 是否正在移动
        self.flag_is_moving = False
        # 窗口的起始绝对位置
        self.window_absolute_start = self.pos()
        # 鼠标的起始绝对位置
        self.mouse_absolute_start = QPoint(0, 0)
        # 鼠标的起始相对位置
        self.mouse_relative_start = QPoint(0, 0)

    def resizeEvent(self, event: PySide6.QtGui.QResizeEvent) -> None:
        return super().resizeEvent(event)

    def mousePressEvent(self, event: PySide6.QtGui.QMouseEvent) -> None:
        # 窗口开始移动,这里我们可以设置可以拖动窗口的区域
        child = self.childAt(event.scenePosition().toPoint())  # 鼠标点击的控件
        if child is not None and child.objectName() == "widget":
            self.flag_is_moving = True
        # 窗口的起始绝对位置
        self.window_absolute_start = self.pos()
        # 鼠标的起始绝对位置
        self.mouse_absolute_start = event.globalPosition().toPoint()
        # 鼠标的起始相对位置
        self.mouse_relative_start = event.position().toPoint()
        return super().mousePressEvent(event)

    def mouseMoveEvent(self, event: PySide6.QtGui.QMouseEvent) -> None:
        if self.flag_is_moving:
            # 当前鼠标的绝对位置
            point = event.globalPosition().toPoint()
            # 窗口绝对位置=窗口的绝对起始位置+(窗口的移动距离/鼠标的移动距离)
            # 窗口绝对位置=窗口的绝对起始位置+(鼠标的当前绝对位置-鼠标的绝对起始位置)
            self.move(self.window_absolute_start.x() + point.x() - self.mouse_absolute_start.x(),
                      self.window_absolute_start.y() + point.y() - self.mouse_absolute_start.y())
        return super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event: PySide6.QtGui.QMouseEvent) -> None:
        # 停止窗口移动
        self.flag_is_moving = False
        return super().mouseReleaseEvent(event)


if __name__ == '__main__':
    app = QApplication([])
    main = Mains()
    main.show()
    app.exec()