我认为这样做的唯一方法是避免使用您的应用程序从“SO”中获得的默认菜单栏。将您的应用程序的属性设置为不使用默认菜单栏并自己制作。
尝试设置您的应用程序的属性,看看它是否适合您。
app = QApplication(sys.argv)
app.setAttribute(Qt.AA_DontUseNativeMenuBar)
或仅设置应用所在的主窗口小部件的 windows 标志。
self.setWindowFlags(Qt.FramelessWindowHint)
类似的东西,但你仍然需要开发自己的“假菜单栏”,在那里你可以完全控制你想用它做什么。
这是一个小例子,看起来有点丑(还有更多更好的做法可供使用),但也许它已经可以为您提供一些您真正需要的想法:
import sys
from PyQt5.QtCore import QPoint
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.layout = QVBoxLayout()
self.layout.addWidget(MyBar(self))
self.layout.addStretch(-1)
self.setLayout(self.layout)
self.layout.setContentsMargins(0,0,0,0)
self.layout.addStretch(-1)
self.setFixedSize(800,400)
self.setWindowFlags(Qt.FramelessWindowHint)
class MyBar(QWidget):
def __init__(self, parent):
super(MyBar, self).__init__()
self.parent = parent
print(self.parent.width())
self.layout = QHBoxLayout()
self.layout.setContentsMargins(0,0,0,0)
self.title = QLabel("My Own Bar")
self.title.setFixedHeight(35)
self.title.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.title)
self.title.setStyleSheet("""
background-color: black;
color: white;
""")
self.setLayout(self.layout)
self.start = QPoint(0, 0)
self.pressing = False
def resizeEvent(self, QResizeEvent):
super(MyBar, self).resizeEvent(QResizeEvent)
self.title.setFixedWidth(self.parent.width())
def mousePressEvent(self, event):
self.start = self.mapToGlobal(event.pos())
self.pressing = True
def mouseMoveEvent(self, event):
if self.pressing:
self.end = self.mapToGlobal(event.pos())
self.movement = self.end-self.start
self.parent.move(self.mapToGlobal(self.movement))
self.start = self.end
def mouseReleaseEvent(self, QMouseEvent):
self.pressing = False
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
注意: 请注意,由于它现在是无框架的,它有点“失去”它的移动属性,所以我不得不重新实现它,这同样适用于调整大小和任何其他需要它框架的属性(例如关闭、迷你和最大按钮)...