#!/usr/bin/env python #-*- coding:utf-8 -*- from PyQt5.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout from PyQt5.QtCore import Qt, QPoint from PyQt5.QtGui import QFont, QCursor class QTitleLabel(QLabel): """ 新建標題欄標簽類 """ def __init__(self, *args): super(QTitleLabel, self).__init__(*args) self.setAlignment(Qt.AlignLeft | Qt.AlignVCenter) self.setFixedHeight(30) class QTitleButton(QPushButton): """ 新建標題欄按鈕類 """ def __init__(self, *args): super(QTitleButton, self).__init__(*args) self.setFont(QFont("Webdings")) # 特殊字體以不借助圖片實現(xiàn)最小化最大化和關(guān)閉按鈕 self.setFixedWidth(40) class QUnFrameWindow(QWidget): """ 無邊框窗口類 """ def __init__(self): super(QUnFrameWindow, self).__init__(None, Qt.FramelessWindowHint) # 設置為頂級窗口,無邊框 self._padding = 5 # 設置邊界寬度為5 self.initTitleLabel() # 安放標題欄標簽 self.setWindowTitle = self._setTitleText(self.setWindowTitle) # 用裝飾器將設置WindowTitle名字函數(shù)共享到標題欄標簽上 self.setWindowTitle("UnFrameWindow") self.initLayout() # 設置框架布局 self.setMinimumWidth(250) self.setMouseTracking(True) # 設置widget鼠標跟蹤 self.initDrag() # 設置鼠標跟蹤判斷默認值 def initDrag(self): # 設置鼠標跟蹤判斷扳機默認值 self._move_drag = False self._corner_drag = False self._bottom_drag = False self._right_drag = False def initTitleLabel(self): # 安放標題欄標簽 self._TitleLabel = QTitleLabel(self) self._TitleLabel.setMouseTracking(True) # 設置標題欄標簽鼠標跟蹤(如不設,則標題欄內(nèi)在widget上層,無法實現(xiàn)跟蹤) self._TitleLabel.setIndent(10) # 設置標題欄文本縮進 self._TitleLabel.move(0, 0) # 標題欄安放到左上角 def initLayout(self): # 設置框架布局 self._MainLayout = QVBoxLayout() self._MainLayout.setSpacing(0) self._MainLayout.addWidget(QLabel(), Qt.AlignLeft) # 頂一個QLabel在豎放框架第一行,以免正常內(nèi)容擠占到標題范圍里 self._MainLayout.addStretch() self.setLayout(self._MainLayout) def addLayout(self, QLayout): # 給widget定義一個addLayout函數(shù),以實現(xiàn)往豎放框架的正確內(nèi)容區(qū)內(nèi)嵌套Layout框架 self._MainLayout.addLayout(QLayout) def _setTitleText(self, func): # 設置標題欄標簽的裝飾器函數(shù) def wrapper(*args): self._TitleLabel.setText(*args) return func(*args) return wrapper def setTitleAlignment(self, alignment): # 給widget定義一個setTitleAlignment函數(shù),以實現(xiàn)標題欄標簽的對齊方式設定 self._TitleLabel.setAlignment(alignment | Qt.AlignVCenter) def setCloseButton(self, bool): # 給widget定義一個setCloseButton函數(shù),為True時設置一個關(guān)閉按鈕 if bool == True: self._CloseButton = QTitleButton(b'\xef\x81\xb2'.decode("utf-8"), self) self._CloseButton.setObjectName("CloseButton") # 設置按鈕的ObjectName以在qss樣式表內(nèi)定義不同的按鈕樣式 self._CloseButton.setToolTip("關(guān)閉窗口") self._CloseButton.setMouseTracking(True) # 設置按鈕鼠標跟蹤(如不設,則按鈕在widget上層,無法實現(xiàn)跟蹤) self._CloseButton.setFixedHeight(self._TitleLabel.height()) # 設置按鈕高度為標題欄高度 self._CloseButton.clicked.connect(self.close) # 按鈕信號連接到關(guān)閉窗口的槽函數(shù) def setMinMaxButtons(self, bool): # 給widget定義一個setMinMaxButtons函數(shù),為True時設置一組最小化最大化按鈕 if bool == True: self._MinimumButton = QTitleButton(b'\xef\x80\xb0'.decode("utf-8"), self) self._MinimumButton.setObjectName("MinMaxButton") # 設置按鈕的ObjectName以在qss樣式表內(nèi)定義不同的按鈕樣式 self._MinimumButton.setToolTip("最小化") self._MinimumButton.setMouseTracking(True) # 設置按鈕鼠標跟蹤(如不設,則按鈕在widget上層,無法實現(xiàn)跟蹤) self._MinimumButton.setFixedHeight(self._TitleLabel.height()) # 設置按鈕高度為標題欄高度 self._MinimumButton.clicked.connect(self.showMinimized) # 按鈕信號連接到最小化窗口的槽函數(shù) self._MaximumButton = QTitleButton(b'\xef\x80\xb1'.decode("utf-8"), self) self._MaximumButton.setObjectName("MinMaxButton") # 設置按鈕的ObjectName以在qss樣式表內(nèi)定義不同的按鈕樣式 self._MaximumButton.setToolTip("最大化") self._MaximumButton.setMouseTracking(True) # 設置按鈕鼠標跟蹤(如不設,則按鈕在widget上層,無法實現(xiàn)跟蹤) self._MaximumButton.setFixedHeight(self._TitleLabel.height()) # 設置按鈕高度為標題欄高度 self._MaximumButton.clicked.connect(self._changeNormalButton) # 按鈕信號連接切換到恢復窗口大小按鈕函數(shù) def _changeNormalButton(self): # 切換到恢復窗口大小按鈕 try: self.showMaximized() # 先實現(xiàn)窗口最大化 self._MaximumButton.setText(b'\xef\x80\xb2'.decode("utf-8")) # 更改按鈕文本 self._MaximumButton.setToolTip("恢復") # 更改按鈕提示 self._MaximumButton.disconnect() # 斷開原本的信號槽連接 self._MaximumButton.clicked.connect(self._changeMaxButton) # 重新連接信號和槽 except: pass def _changeMaxButton(self): # 切換到最大化按鈕 try: self.showNormal() self._MaximumButton.setText(b'\xef\x80\xb1'.decode("utf-8")) self._MaximumButton.setToolTip("最大化") self._MaximumButton.disconnect() self._MaximumButton.clicked.connect(self._changeNormalButton) except: pass def resizeEvent(self, QResizeEvent): # 自定義窗口調(diào)整大小事件 self._TitleLabel.setFixedWidth(self.width()) # 將標題標簽始終設為窗口寬度 # 分別移動三個按鈕到正確的位置 try: self._CloseButton.move(self.width() - self._CloseButton.width(), 0) except: pass try: self._MinimumButton.move(self.width() - (self._CloseButton.width() + 1) * 3 + 1, 0) except: pass try: self._MaximumButton.move(self.width() - (self._CloseButton.width() + 1) * 2 + 1, 0) except: pass # 重新調(diào)整邊界范圍以備實現(xiàn)鼠標拖放縮放窗口大小,采用三個列表生成式生成三個列表 self._right_rect = [QPoint(x, y) for x in range(self.width() - self._padding, self.width() + 1) for y in range(1, self.height() - self._padding)] self._bottom_rect = [QPoint(x, y) for x in range(1, self.width() - self._padding) for y in range(self.height() - self._padding, self.height() + 1)] self._corner_rect = [QPoint(x, y) for x in range(self.width() - self._padding, self.width() + 1) for y in range(self.height() - self._padding, self.height() + 1)] def mousePressEvent(self, event): # 重寫鼠標點擊的事件 if (event.button() == Qt.LeftButton) and (event.pos() in self._corner_rect): # 鼠標左鍵點擊右下角邊界區(qū)域 self._corner_drag = True event.accept() elif (event.button() == Qt.LeftButton) and (event.pos() in self._right_rect): # 鼠標左鍵點擊右側(cè)邊界區(qū)域 self._right_drag = True event.accept() elif (event.button() == Qt.LeftButton) and (event.pos() in self._bottom_rect): # 鼠標左鍵點擊下側(cè)邊界區(qū)域 self._bottom_drag = True event.accept() elif (event.button() == Qt.LeftButton) and (event.y() < self._TitleLabel.height()): # 鼠標左鍵點擊標題欄區(qū)域 self._move_drag = True self.move_DragPosition = event.globalPos() - self.pos() event.accept() def mouseMoveEvent(self, QMouseEvent): # 判斷鼠標位置切換鼠標手勢 if QMouseEvent.pos() in self._corner_rect: self.setCursor(Qt.SizeFDiagCursor) elif QMouseEvent.pos() in self._bottom_rect: self.setCursor(Qt.SizeVerCursor) elif QMouseEvent.pos() in self._right_rect: self.setCursor(Qt.SizeHorCursor) else: self.setCursor(Qt.ArrowCursor) # 當鼠標左鍵點擊不放及滿足點擊區(qū)域的要求后,分別實現(xiàn)不同的窗口調(diào)整 # 沒有定義左方和上方相關(guān)的5個方向,主要是因為實現(xiàn)起來不難,但是效果很差,拖放的時候窗口閃爍,再研究研究是否有更好的實現(xiàn) if Qt.LeftButton and self._right_drag: # 右側(cè)調(diào)整窗口寬度 self.resize(QMouseEvent.pos().x(), self.height()) QMouseEvent.accept() elif Qt.LeftButton and self._bottom_drag: # 下側(cè)調(diào)整窗口高度 self.resize(self.width(), QMouseEvent.pos().y()) QMouseEvent.accept() elif Qt.LeftButton and self._corner_drag: # 右下角同時調(diào)整高度和寬度 self.resize(QMouseEvent.pos().x(), QMouseEvent.pos().y()) QMouseEvent.accept() elif Qt.LeftButton and self._move_drag: # 標題欄拖放窗口位置 self.move(QMouseEvent.globalPos() - self.move_DragPosition) QMouseEvent.accept() def mouseReleaseEvent(self, QMouseEvent): # 鼠標釋放后,各扳機復位 self._move_drag = False self._corner_drag = False self._bottom_drag = False self._right_drag = False if __name__ == "__main__": from PyQt5.QtWidgets import QApplication import sys app = QApplication(sys.argv) app.setStyleSheet(open("./UnFrameStyle.qss").read()) window = QUnFrameWindow() window.setCloseButton(True) window.setMinMaxButtons(True) window.show() sys.exit(app.exec_()) |
|