I am using QMdiArea to hold a bunch of windows, I want to be able to set the color theme of all the windows based on a list of items found in one of the subwindows.
It seems to work fine with PyQt5 but using PyQt6 it changes the color when the user clicks on another window (outside of application); clicking back on the window changes the color back to blue (the default)
Running the settings window by itself keeps the changes but running through QMdiArea it is incorrect.
Working Example:
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()