[Solved] PyQt Question : Getting FreeCAD's System Font

Need help, or want to share a macro? Post here!
User avatar
melwynfranciscarlo
Posts: 81
Joined: Thu Feb 04, 2021 7:47 am
Location: Mumbai, India

[Solved] PyQt Question : Getting FreeCAD's System Font

Postby melwynfranciscarlo » Tue Feb 16, 2021 3:34 pm

I'd like to set the QWidgets' font family and size to the default FreeCAD's font family and size. How do I do that? Could someone please share the relevant code snippet?

In case that none exist, what font family should I choose such that it's workable on different platforms?
Also, if I choose a strange font, would it affect its use on other platforms?

Thank you very much.
Last edited by melwynfranciscarlo on Sat Feb 27, 2021 12:56 pm, edited 1 time in total.
8-) MFC Boy: the one and only! 8-)
openBrain
Posts: 5747
Joined: Fri Nov 09, 2018 5:38 pm

Re: PyQt Question : Getting FreeCAD's System Font

Postby openBrain » Tue Feb 16, 2021 4:54 pm

Not sure I understand well. FreeCAD relies mainly on Qt to choose the font, which it self mainly get the font from the OS. If you specify nothing as font, an add-on or macro will be default inherits main app font (Qt propagates from parent to children).
Finally notice that FreeCAD doesn't use PyQt but PySide.

Anyway if you want to know font of main window

Code: Select all

FreeCADGui.getMainWindow().font()
User avatar
melwynfranciscarlo
Posts: 81
Joined: Thu Feb 04, 2021 7:47 am
Location: Mumbai, India

Re: PyQt Question : Getting FreeCAD's System Font

Postby melwynfranciscarlo » Tue Feb 16, 2021 5:00 pm

openBrain wrote:
Tue Feb 16, 2021 4:54 pm
Not sure I understand well. FreeCAD relies mainly on Qt to choose the font, which it self mainly get the font from the OS. If you specify nothing as font, an add-on or macro will be default inherits main app font (Qt propagates from parent to children).
Finally notice that FreeCAD doesn't use PyQt but PySide.

Anyway if you want to know font of main window

Code: Select all

FreeCADGui.getMainWindow().font()
Thank you so much, openBrain!

That's exactly what I needed: both the code as well as the information that by default the font is inherited.
I was confused as to how a Macro's UI would react on someone else's computer if they didn't have the same font as set by the Macro.
8-) MFC Boy: the one and only! 8-)
openBrain
Posts: 5747
Joined: Fri Nov 09, 2018 5:38 pm

Re: PyQt Question : Getting FreeCAD's System Font

Postby openBrain » Tue Feb 16, 2021 5:21 pm

melwynfranciscarlo wrote:
Tue Feb 16, 2021 5:00 pm
That's exactly what I needed: both the code as well as the information that by default the font is inherited.
I was confused as to how a Macro's UI would react on someone else's computer if they didn't have the same font as set by the Macro.
Now I'm sure I get your question correctly, some more details.
To get the real 'root' font, you can do :

Code: Select all

from PySide2 import QtGui
QtGui.QGuiApplication.font()
But in FC the main window doesn't change it so it's the same.

Font propagation is one reason (not the main one though) why it's important to correctly set widget relationship when dealing with UI.

One last thing : widget inherits parent font by default except if the said widget is a graphically independent 'window' (such as a dialog for example). In this case, if no font is set, it will however inherit QGuiApplication font so everything should be OK. You can nevertheless force parent font inheritance even in this specific case by setting the 'Qt::WA_WindowPropagation' attribute to the widget. ;)
User avatar
melwynfranciscarlo
Posts: 81
Joined: Thu Feb 04, 2021 7:47 am
Location: Mumbai, India

Re: PyQt Question : Getting FreeCAD's System Font

Postby melwynfranciscarlo » Sat Feb 20, 2021 5:48 pm

openBrain wrote:
Tue Feb 16, 2021 5:21 pm
melwynfranciscarlo wrote:
Tue Feb 16, 2021 5:00 pm
That's exactly what I needed: both the code as well as the information that by default the font is inherited.
I was confused as to how a Macro's UI would react on someone else's computer if they didn't have the same font as set by the Macro.
Now I'm sure I get your question correctly, some more details.
To get the real 'root' font, you can do :

Code: Select all

from PySide2 import QtGui
QtGui.QGuiApplication.font()
But in FC the main window doesn't change it so it's the same.

Font propagation is one reason (not the main one though) why it's important to correctly set widget relationship when dealing with UI.

One last thing : widget inherits parent font by default except if the said widget is a graphically independent 'window' (such as a dialog for example). In this case, if no font is set, it will however inherit QGuiApplication font so everything should be OK. You can nevertheless force parent font inheritance even in this specific case by setting the 'Qt::WA_WindowPropagation' attribute to the widget. ;)
I suppose root font would have been the right term to use. And yes, I agree that fonts and sizing parameters should be similar to the parent application.
With regard to the codes, both output the same result. I am working on a dialog; so if it inherits by default, then it should be fine.
Regardless, I did try setting the window flag, like this:

Code: Select all

self.form.findChild(QDialog, 'Dialog').setAttribute(Qt.WA_WindowPropagation)
I ended up getting the following error:

AttributeError: 'NoneType' object has no attribute 'setAttribute'

Maybe it's an incorrect way to implement it, I don't know. In this webpage, it said it was for widgets; I assumed a QDialog would qualify as one.
8-) MFC Boy: the one and only! 8-)
openBrain
Posts: 5747
Joined: Fri Nov 09, 2018 5:38 pm

Re: PyQt Question : Getting FreeCAD's System Font

Postby openBrain » Sat Feb 20, 2021 5:55 pm

melwynfranciscarlo wrote:
Sat Feb 20, 2021 5:48 pm
Regardless, I did try setting the window flag, like this:

Code: Select all

self.form.findChild(QDialog, 'Dialog').setAttribute(Qt.WA_WindowPropagation)
I ended up getting the following error:

AttributeError: 'NoneType' object has no attribute 'setAttribute'

Maybe it's an incorrect way to implement it, I don't know.
The error basically says that self.form has no child of type QDialog named 'Dialog'.
User avatar
Kunda1
Posts: 10391
Joined: Thu Jan 05, 2017 9:03 pm

Re: PyQt Question : Getting FreeCAD's System Font

Postby Kunda1 » Sat Feb 20, 2021 10:41 pm

Anyone volunteer to add this to Code_snippets?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
openBrain
Posts: 5747
Joined: Fri Nov 09, 2018 5:38 pm

Re: PyQt Question : Getting FreeCAD's System Font

Postby openBrain » Sun Feb 21, 2021 7:45 am

Kunda1 wrote:
Sat Feb 20, 2021 10:41 pm
Anyone volunteer to add this to Code_snippets?
Actually I often saw such questions in the past weeks. I plan to create a new wiki page about this (setting attributes and flags, and more) that can be used as a canvas for macro newcomers that want to set up UI interacting with main window. I hope to achieve in the coming weeks. ;)
User avatar
melwynfranciscarlo
Posts: 81
Joined: Thu Feb 04, 2021 7:47 am
Location: Mumbai, India

Re: PyQt Question : Getting FreeCAD's System Font

Postby melwynfranciscarlo » Sun Feb 21, 2021 11:03 am

openBrain wrote:
Sun Feb 21, 2021 7:45 am
Kunda1 wrote:
Sat Feb 20, 2021 10:41 pm
Anyone volunteer to add this to Code_snippets?
Actually I often saw such questions in the past weeks. I plan to create a new wiki page about this (setting attributes and flags, and more) that can be used as a canvas for macro newcomers that want to set up UI interacting with main window. I hope to achieve in the coming weeks. ;)
That'd be great, openBrain! Thank you very much! :)
8-) MFC Boy: the one and only! 8-)
User avatar
Kunda1
Posts: 10391
Joined: Thu Jan 05, 2017 9:03 pm

Re: PyQt Question : Getting FreeCAD's System Font

Postby Kunda1 » Sun Feb 21, 2021 12:01 pm

openBrain wrote:
Sun Feb 21, 2021 7:45 am
Kunda1 wrote:
Sat Feb 20, 2021 10:41 pm
Anyone volunteer to add this to Code_snippets?
Actually I often saw such questions in the past weeks. I plan to create a new wiki page about this (setting attributes and flags, and more) that can be used as a canvas for macro newcomers that want to set up UI interacting with main window. I hope to achieve in the coming weeks. ;)
That sounds fantastic!
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features