Sat, 31 Dec 2022 16:23:21 +0100
Updated copyright for 2023.
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2 | |
9653
e67609152c5e
Updated copyright for 2023.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
3 | # Copyright (c) 2012 - 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4 | # |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5 | |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6 | """ |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
7 | Module implementing a main window class with styling support. |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8 | """ |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9 | |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
10 | from PyQt6.QtWidgets import QApplication, QMainWindow, QStyleFactory |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
11 | |
8356
68ec9c3d4de5
Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8318
diff
changeset
|
12 | from .EricApplication import ericApp |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
13 | |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
14 | |
8356
68ec9c3d4de5
Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8318
diff
changeset
|
15 | class EricMainWindow(QMainWindow): |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
16 | """ |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
17 | Class implementing a main window with styling support. |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
18 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
19 | |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
20 | def __init__(self, parent=None): |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
21 | """ |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
22 | Constructor |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
23 | |
8843
586ee2c4553a
Enhanced style sheet previewer and style sheet handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8358
diff
changeset
|
24 | @param parent reference to the parent widget |
586ee2c4553a
Enhanced style sheet previewer and style sheet handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8358
diff
changeset
|
25 | @type QWidget |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | """ |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8205
diff
changeset
|
27 | super().__init__(parent) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
28 | |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29 | self.defaultStyleName = QApplication.style().objectName() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
30 | |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | def setStyle(self, styleName, styleSheetFile): |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
32 | """ |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
33 | Public method to set the style of the interface. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
34 | |
8843
586ee2c4553a
Enhanced style sheet previewer and style sheet handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8358
diff
changeset
|
35 | @param styleName name of the style to set |
586ee2c4553a
Enhanced style sheet previewer and style sheet handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8358
diff
changeset
|
36 | @type str |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | @param styleSheetFile name of a style sheet file to read to overwrite |
8843
586ee2c4553a
Enhanced style sheet previewer and style sheet handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8358
diff
changeset
|
38 | defaults of the given style |
586ee2c4553a
Enhanced style sheet previewer and style sheet handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8358
diff
changeset
|
39 | @type str |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
40 | """ |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41 | # step 1: set the style |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | style = None |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43 | if styleName != "System" and styleName in QStyleFactory.keys(): |
8205
4a0f1f896341
Applied some code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
44 | # __IGNORE_WARNING_Y118__ |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
45 | style = QStyleFactory.create(styleName) |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | if style is None: |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
47 | style = QStyleFactory.create(self.defaultStyleName) |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
48 | if style is not None: |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
49 | QApplication.setStyle(style) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
50 | |
2101
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
51 | # step 2: set a style sheet |
8843
586ee2c4553a
Enhanced style sheet previewer and style sheet handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8358
diff
changeset
|
52 | ericApp().setStyleSheetFile(styleSheetFile) |