Can I specify a default font to use when translation is not provided?



  • I have an app that uses QTranslator to provide translations in non-Latin languages, e.g. Sinhalese. It's possible (common) that not all text in my GUI gets a translation, so some of it is default English. I have a special font called Iskoola Pota for Sinhalese, but it can not render English. When the GUI goes into Sinhala mode, all of my Text {} elements automatically update their font.family:

    @
    // Default Font is Trebuchet for English
    FontLoader {
    id: globalFont
    source: ":/fonts/trebuchet.ttf"
    }
    FontLoader {
    id: englishFont
    source: ":/fonts/trebuchet.ttf
    }
    Text {
    font.family: globalFont.name
    text: qsTr("Hello World!")
    }
    Rectangle {
    height: 50; width: 50
    MouseArea {
    anchors.fill: parent
    onClicked: { globalFont.source = ":/fonts/iskoola.ttf"; }
    }
    }
    @

    As you can see above, when the new font is installed, the Text font.family binding is updated with the new name. The problem is, when the translator doesn't provide a translation for qsTr("Hello World!") , qsTr() returns English characters which get rendered improperly by the iskoola.ttf font.

    In this case, if qsTr() returns a translation, I want it to use the new font, otherwise if it returns default text, I want it to use my Trebuchet font.

    I can do something cheesy like this, but I'm hoping there's a better solution:

    @
    Text {
    property string origText

    origText: "Hello World!"
    text: qsTr("Hello World!")

    font.family: origText == text ? englishFont.name : globalFont.name
    }
    @

    Ideas?

    Thanks in advance!

    Matt



  • AFAIK, there is know standard method to find out whether a translation was successful. Have you considered using Unicode?



  • OK, silly me. The TTF that I was using was not Unicode. The glyphs were written over the top the Latin characters and not in their proper Unicode range.

    There's an excellent font viewer for Linux called fontmatrix that helps diagnose this kind of problem. Using a proper Unicode font, things look much better.

    Unfortunately though, it's not possible (AFAIK) to tell Qt to prefer specific fonts for a given writing system. It's not clear to me how Qt selects fonts when the font family isn't explicitly provided.



  • Your question is answered right "here":http://doc.qt.nokia.com/4.7/qapplication.html#setFont . Also if you consider your question answered, please add [Solved] in front of the title (by editing the first post).



  • Sadly, setFont() doesn't seem to work for QML applications. I added a QApplication::setFont(QFont("Trebuchet MS")) and then did a read back of qDebug() << QApplication::font() to confirm Trebuchet was loaded. But still in my GUI, when I use a QML Text { font.family } for a non-latin Unicode font, my Latin text is still render in some non-Trebuchet font. Sounds like a bug?

    On Embedded, the only way I can find to force the fonts I need, is to point Qt at a font directory that has no fonts:
    @export QT_QWS_FONTDIR=/@
    or
    @#ifdef Q_WS_QWS
    qputenv("QT_QWS_FONTDIR", "/");
    #endif@
    Then I can load the specific fonts I need using QFontDatabase or in QML with FontLoader {}

    In my application, I am careful not to load fonts with overlapping unicode regions. Seems to work so far... :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.