窗口无边框 自由拉伸拖动 自定义置顶最大化最小化关闭按钮 pyqt pyside QT

演示:

项目地址

设置无边框窗口后,窗口无法移动位置、拉伸大小,最大化最小化也被隐藏,因此需要自己添加这些按钮,并重写拉伸和移动窗口的代码:

import ctypes
import ctypes.wintypes
import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2 import QtCore
from PySide2.QtGui import *
from PySide2.QtSvg import *
from PySide2 import QtGui

from window_ui import Ui_MainWindow

# Windows constants
WM_NCHITTEST = 0x0084
HTCAPTION = 2
HTLEFT = 10
HTRIGHT = 11
HTTOP = 12
HTTOPLEFT = 13
HTTOPRIGHT = 14
HTBOTTOM = 15
HTBOTTOMLEFT = 16
HTBOTTOMRIGHT = 17

class FrameLessWindow(Ui_MainWindow, QMainWindow):
    def __init__(self) -> None:
        super().__init__()
        self._drag = False
        self._corner_drag = False
        self._bottom_drag = False
        self._right_drag = False
        self._padding = 5
        self.setupUi(self)
        self.setWindowFlags(Qt.FramelessWindowHint) # 隐藏边框 

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            if event.pos().x() > self.width() - self._padding and event.pos().y() > self.height() - self._padding:
                self._corner_drag = True
            elif event.pos().x() > self.width() - self._padding:
                self._right_drag = True
            elif event.pos().y() > self.height() - self._padding:
                self._bottom_drag = True
            else:
                self._drag = True
                self.move_DragPosition = event.globalPos() - self.pos()
            event.accept()

    def mouseMoveEvent(self, event):
        if Qt.LeftButton and self._drag:
            self.move(event.globalPos() - self.move_DragPosition)
            event.accept()
        elif Qt.LeftButton and self._right_drag:
            self.resize(event.pos().x(),self.height())
            event.accept()
        elif Qt.LeftButton and self._bottom_drag:
            self.resize(self.width(),event.pos().y())
            event.accept()
        elif Qt.LeftButton and self._corner_drag:
            self.resize(event.pos().x(),event.pos().y())
            event.accept()

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self._drag = False
            self._corner_drag = False
            self._bottom_drag = False
            self._right_drag = False

    def nativeEvent(self, eventType, message):
        if eventType == b"windows_generic_MSG":
            msg = ctypes.wintypes.MSG.from_address(message.__int__())
            if msg.message == WM_NCHITTEST:
                x = msg.lParam & 0xffff
                y = (msg.lParam >> 16) & 0xffff
                pos = self.mapFromGlobal(QtCore.QPoint(x, y))
                if pos.x() < 5:
                    if pos.y() < 5:
                        return True, HTTOPLEFT
                    elif pos.y() > self.height() - 5:
                        return True, HTBOTTOMLEFT
                    else:
                        return True, HTLEFT
                elif pos.x() > self.width() - 5:
                    if pos.y() < 5:
                        return True, HTTOPRIGHT
                    elif pos.y() > self.height() - 5:
                        return True, HTBOTTOMRIGHT
                    else:
                        return True, HTRIGHT
                elif pos.y() < 5:
                    return True, HTTOP
                elif pos.y() > self.height() - 5:
                    return True, HTBOTTOM
        return False


你可能感兴趣的:(qt,pyqt,ui)