While the provided answers should have already given you enough suggestions, I'd like to add some insight.
Are there python sources for Qt?
First of all, you cannot find "the class written in python", because (luckily) there's none. PyQt is a binding: it is an interface to the actual Qt library, which is written in C++.
As you might already know, while Python is pretty fast on nowadays computers, it's not that fast, so using a binding is a very good compromise: it allows the simple syntax Python provides, and gives all speed provided by C++ compiled libraries under the hood.
You can find the source code for Qt widgets here (official mirror), or here.
How to override the default font?
Well, this depends on how you're going to manage your project.
Generally speaking, you can set the default font [size] for a specific widget, for its child widgets, for the top level window or even for the whole application. And there are at least two ways to do it.
- use
setFont()
: it sets the default font for the target; you can get the current default font using something.font()
, then use font.setPointSize()
(or setPointSizeF()
for float values, if the font allows it) and then call setFont(font)
on the target.
- use
font[-*]
in the target setStyleSheet()
;
Target?
The target might be the widget itself, one of its parents or even the QApplication.instance()
. You can use both setFont()
or setStyleSheet()
on any of them:
font = self.font()
font.setPointSize(24)
# set the font for the widget:
self.pushButton.setFont(someFont)
# set the font for the top level window (and any of its children):
self.window().setFont(someFont)
# set the font for *any* widget created in this QApplication:
QApplication.instance().setFont(someFont)
# the same as...
self.pushButton.setStyleSheet(''' font-size: 24px; ''')
# etc...
Also, consider setting the Qt.AA_UseStyleSheetPropagationInWidgetStyles
attribute for the application instance.
Setting and inheritance
By default, Qt uses font propagation (as much as palette propagation) for both setFont
and setStyleSheet
, but whenever a style sheet is set, it takes precedence, even if it's set on any of the parent widgets (up to the top level window OR the QApplication instance).
Whenever stylesheets are applied, there are various possibilities, based on CSS Selectors:
'font-size: 24px;'
: no selector, the current widget and any of its child will use the specified font size;
'QClass { font-size: 24px; }'
: classes and subclasses selector, any widget (including the current instance) and its children of the same class/subclass will use the specified font size:
'QClass[property="value"] {...}'
: property selector, as the above, but only if the property
matches the value; note that values are always quoted, and bool values are always lower case;
'.QClass {...}'
: classes selector, but not subclasses: if you're using a subclass of QLabel and the stylesheet is set for .QLabel
, that stylesheet won't be applied;
'QClass#objectName {...}'
: apply only for widgets for which objectName()
matches;
'QParentClass QClass {...}
': apply for widget of class QClass that are children of QParentClass
'QParentClass > QClass {...}
': apply for widget of class QClass that are direct children of QParentClass
Note that both setFont
and setStyleSheet
support propagation, but setStyle
only works on children when set to the QApplication instance: if you use widget.setStyle()
it won't have effect on any of the widget's children.
Finally, remember:
- whenever a widget gets reparented, it receives the font, palette and stylesheet of its parent, in "cascading" mode (the closest parent has precedence);
- stylesheets have precedence on both palette and font, whenever any of the related properties are set, and palette/font properties are not compatible with stylesheets (or, at least, they behave in unexpected ways);