PyQt窗口FramelessWindowHint模式下自定义窗口缩放与移动功能( 二 )


2、使用示例
class MyWin(MainWinCustom):def __init__(self):super(MyWin2, self).__init__()self.resize(200, 300)# 创建一个 QWidget 作为中心区域的容器central_widget = QWidget(self)# 在中心容器中添加其他控件label_title = QLabel("这是标题栏", central_widget)label_title.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)label_title.setFixedHeight(40)label_title.setStyleSheet("""QLabel{background: yellow;}""")layout = QVBoxLayout(central_widget)layout.addWidget(label_title)label = QLabel('Power by 浅若清风cyf\n\t2023/06/16\n')label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)layout.addWidget(label)layout.setContentsMargins(0, 0, 0, 0)# 将中心容器设置为 QMainWindow 的中心部件self.setCentralWidget(central_widget)# 设置label_title的高度为有效识别区域self.set_titleHeight(label_title.height())if __name__ == '__main__':QtCore.QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)QtGui.QGuiApplication.setAttribute(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough) app = QApplication(sys.argv)win = MyWin()win.show()sys.exit(app.exec_())