我正在使用 QMdiArea 来保存一堆窗口,我希望能够根据在其中一个子窗口中找到的项目列表来设置所有窗口的颜色主题。
PyQt5 似乎可以正常工作,但使用 PyQt6 时,它会在用户单击另一个窗口(应用程序外部)时改变颜色;单击窗口将颜色更改回蓝色(默认)
单独运行设置窗口会保留更改,但通过 QMdiArea 运行它是不正确的。
工作示例:
from PyQt6 import QtGui, QtCore, QtWidgets
class Settings(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.centralwidget = QtWidgets.QWidget(self)
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.themeList = QtWidgets.QListWidget()
self.themeList.addItem("Red")
self.themeList.addItem("Blue")
self.themeList.addItem("Pink")
self.themeList.addItem("Orange")
self.themeList.itemDoubleClicked.connect(self.setTheme)
self.gridLayout.addWidget(self.themeList, 0, 0, 1, 1)
self.setCentralWidget(self.centralwidget)
def setTheme(self):
app = QtWidgets.QApplication.instance()
if app is None:
raise RuntimeError("No Qt Application found.")
app.setStyle("Fusion")
palette = QtGui.QPalette()
color = self.themeList.currentItem().text()
if color == "Blue":
palette.setColor(QtGui.QPalette.ColorRole.Link, QtGui.QColor(42, 130, 218))
palette.setColor(QtGui.QPalette.ColorRole.Highlight, QtGui.QColor(42, 130, 218))
elif color == "Orange":
palette.setColor(QtGui.QPalette.ColorRole.Link, QtGui.QColor(255, 174, 0))
palette.setColor(QtGui.QPalette.ColorRole.Highlight, QtGui.QColor(255, 174, 0))
elif color == "Red":
palette.setColor(QtGui.QPalette.ColorRole.Link, QtGui.QColor(196, 6, 6))
palette.setColor(QtGui.QPalette.ColorRole.Highlight, QtGui.QColor(196, 6, 6))
elif color == "Pink":
palette.setColor(QtGui.QPalette.ColorRole.Link, QtGui.QColor(255, 192, 203))
palette.setColor(QtGui.QPalette.ColorRole.Highlight, QtGui.QColor(255, 192, 203))
palette.setColor(QtGui.QPalette.ColorRole.Window, QtGui.QColor(38, 38, 38))
palette.setColor(QtGui.QPalette.ColorRole.WindowText, QtCore.Qt.GlobalColor.white)
palette.setColor(QtGui.QPalette.ColorRole.Base, QtGui.QColor(48, 48, 48))
palette.setColor(QtGui.QPalette.ColorRole.AlternateBase, QtGui.QColor(38, 38, 38))
palette.setColor(QtGui.QPalette.ColorRole.ToolTipBase, QtCore.Qt.GlobalColor.white)
palette.setColor(QtGui.QPalette.ColorRole.ToolTipText, QtCore.Qt.GlobalColor.white)
palette.setColor(QtGui.QPalette.ColorRole.Text, QtCore.Qt.GlobalColor.white)
palette.setColor(QtGui.QPalette.ColorRole.Button, QtGui.QColor(38, 38, 38))
palette.setColor(QtGui.QPalette.ColorRole.ButtonText, QtCore.Qt.GlobalColor.white)
palette.setColor(QtGui.QPalette.ColorRole.BrightText, QtCore.Qt.GlobalColor.red)
palette.setColor(QtGui.QPalette.ColorRole.HighlightedText, QtCore.Qt.GlobalColor.black)
app.setPalette(palette)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.centralwidget = QtWidgets.QWidget(self)
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
self.mdiArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarPolicy.ScrollBarAsNeeded)
self.mdiArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarPolicy.ScrollBarAsNeeded)
self.gridLayout.addWidget(self.mdiArea, 0, 0, 1, 1)
self.setCentralWidget(self.centralwidget)
self.settings = Settings()
self.createSettings()
def createSettings(self):
"""create settings window """
self.subwindow = QtWidgets.QMdiSubWindow()
self.subwindow.setWidget(self.settings)
self.mdiArea.addSubWindow(self.subwindow)
self.subwindow.setMinimumSize(500, 400)
self.subwindow.setMaximumSize(2000, 2000)
self.subwindow.setWindowFlags(
QtCore.Qt.WindowFlags.WindowTitleHint | QtCore.Qt.WindowFlags.WindowMinimizeButtonHint | QtCore.Qt.WindowFlags.WindowSystemMenuHint)
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()