[PySide] conforming fonts between platforms

Sean Fisk sean at seanfisk.com
Mon Nov 11 08:48:01 CET 2013


Hi Frank,

I struggled with this a while
ago<http://lists.qt-project.org/pipermail/pyside/2013-April/001252.html>and
have it working on Windows and Mac OS X. Still having some problems on
GNU/Linux (specifically targeting Ubuntu) but my team is working on it. We
first compile some TTF files into our resources, then import them in our
program, then call this:

# fonts.py
from PySide import QtCore, QtGui
def init():
    """Initialize embedded fonts."""
    font_dir_resource = QtCore.QResource(':/fonts')
    font_resource_path = font_dir_resource.absoluteFilePath()
    for ttf_filename in font_dir_resource.children():
        # DON'T use `os.path.join()' here because Qt always uses UNIX-style
        # paths. On Windows `os.sep' is '\\'.
        res_file = QtCore.QFile('/'.join([font_resource_path, ttf_filename]))
        # Must re-open the file in read-only mode to read the contents
        # correctly.
        res_file.open(QtCore.QIODevice.ReadOnly)
        byte_array = res_file.readAll()
        QtGui.QFontDatabase.addApplicationFontFromData(byte_array)

And to use it (snippet):

class LoginView(QtGui.QDialog):
    def __init__(self, parent=None):
        super(LoginView, self).__init__(parent)

        # ...
        self.title_font = QtGui.QFont('YourFontName', 46)
        self.title_font.setStyleStrategy(QtGui.QFont.PreferAntialias)
        self.title_label = QtGui.QLabel('Your text in your font')
        self.title_label.setFont(self.title_font)

Hope this helps. And if you get it working on GNU/Linux, let me know what
you did!

Cheers,
--
Sean Fisk


On Mon, Nov 11, 2013 at 2:24 AM, Frank Rueter | OHUfx <frank at ohufx.com>wrote:

> Hi all,
>
> I am facing the challenge I'm sure many of you have had to deal with
> before:
>
> I need to make sure that the font used in my application looks as
> similar as posisble between windows, linux and osx.
>
> I am currently using 12 point Helvetica, which turns into a 16 pixel
> high Sans Nimbus L on my linux box messing up my custom widget's layouts.
>
> What is the best practise here?
> Supposedly it is possible to compile a font into a resource which would
> ensure almost identical results, right?! Has anybody ever done this before?
>
> Cheers,
> frank
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20131111/8bed40d4/attachment.html>


More information about the PySide mailing list