pyqt Mainwindow自带的标题栏和菜单栏不能修改,看了很多博客也没有写完整的自定义标题栏和菜单栏的方法,探索了一番后实现了,记录一下,以供参考
实现方法,隐藏Mianwindow原来的标题栏,创建QDockWidget停靠于Mainwindow顶部,在QDockWidget中使用垂直布局添加两个widget,对两个widget分别设置实现标题栏和工具栏
下面是实现代码
Mainwindow:
import sys
from TitleBar import *
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from default import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.InitializeWindow()
def InitializeWindow(self):
self.setWindowFlags(Qt.FramelessWindowHint)
self.setMinimumSize(WINDOW_DEFAULT_WIDTH, WINDOW_DEFAULT_HEIGHT)
self.InitializeViews()
self.setStyleSheet(str(self.LoadStyleFromQss(WINDOW_QSS)))
def InitializeViews(self):
self.title_meun_dock = QDockWidget('', self)
self.title_meun_dock.setAllowedAreas(Qt.TopDockWidgetArea)
#self.title_meun_dock.setMinimumSize(QSize(1000, 60))
#self.title_meun_dock.setMaximumSize(QSize(1000, 60))
self.title_meun_dock.setFixedHeight(50)
self.title_meun_dock.setTitleBarWidget(QWidget())
self.addDockWidget(Qt.TopDockWidgetArea, self.title_meun_dock)
self.titleBar = TitleBar(self)
#self.client = QWidget(self)
self.menuBar = Meun