eric7/E5Gui/E5MainWindow.py

branch
eric7
changeset 8356
68ec9c3d4de5
parent 8355
8a7677a63c8d
child 8357
a081458cc57b
equal deleted inserted replaced
8355:8a7677a63c8d 8356:68ec9c3d4de5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a main window class with styling support.
8 """
9
10 from PyQt6.QtCore import QCoreApplication
11 from PyQt6.QtWidgets import QMainWindow, QStyleFactory, QApplication
12
13 from .E5Application import e5App
14 from . import E5MessageBox
15
16
17 class E5MainWindow(QMainWindow):
18 """
19 Class implementing a main window with styling support.
20 """
21 def __init__(self, parent=None):
22 """
23 Constructor
24
25 @param parent reference to the parent widget (QWidget)
26 """
27 super().__init__(parent)
28
29 self.defaultStyleName = QApplication.style().objectName()
30
31 def setStyle(self, styleName, styleSheetFile):
32 """
33 Public method to set the style of the interface.
34
35 @param styleName name of the style to set (string)
36 @param styleSheetFile name of a style sheet file to read to overwrite
37 defaults of the given style (string)
38 """
39 # step 1: set the style
40 style = None
41 if styleName != "System" and styleName in QStyleFactory.keys():
42 # __IGNORE_WARNING_Y118__
43 style = QStyleFactory.create(styleName)
44 if style is None:
45 style = QStyleFactory.create(self.defaultStyleName)
46 if style is not None:
47 QApplication.setStyle(style)
48
49 # step 2: set a style sheet
50 if styleSheetFile:
51 try:
52 with open(styleSheetFile, "r", encoding="utf-8") as f:
53 styleSheet = f.read()
54 except OSError as msg:
55 E5MessageBox.warning(
56 self,
57 QCoreApplication.translate(
58 "E5MainWindow", "Loading Style Sheet"),
59 QCoreApplication.translate(
60 "E5MainWindow",
61 """<p>The Qt Style Sheet file <b>{0}</b> could"""
62 """ not be read.<br>Reason: {1}</p>""")
63 .format(styleSheetFile, str(msg)))
64 return
65 else:
66 styleSheet = ""
67
68 e5App().setStyleSheet(styleSheet)

eric ide

mercurial