Qt program with TTF fonts sample

Source: Internet
Author: User
TTF Introduction

TTF (Truetypefont) is a font file format jointly launched by Apple and Microsoft, and TTF has become one of the most commonly used font file representations since Windows became popular for more than 20 of years. The biggest advantage of TrueType fonts is that you can easily convert the font contour to a curve, you can fill the curve, make a variety of colors and effects, it can be further distorted, make special effect font, so often used to make some title word or pattern word.
There are two criteria used by the desktop publishing system: PostScript and TrueType fonts. Both of these font standards describe the font contour in a curved way, so you can output a very high quality glyph. TrueType fonts are also used as a postscript to display the word font, the font companies have both the standard products. So when you use TrueType fonts to make a layout, you can still replace it with the PostScript font output. QT Example

Because I am in the embedded Linux platform to develop an interface program, need to display Chinese fonts, so download the Liheipro.ttf to use. So let's take a look at what should be done in Qt.
(TTF font path is/home/root/seat_imx/liheipro.ttf)
The code is as follows:

#include "gytboxwidget.h"
#include <QApplication>
#include <QTextCodec>
#include < qfontdatabase>
#include <QFont>

int main (int argc, char *argv[])
{
    qapplication A (argc, argv );

    Qtextcodec::setcodecforlocale (Qtextcodec::codecforname ("UTF-8"));
    QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("UTF-8"));
    Qtextcodec::setcodecforcstrings (Qtextcodec::codecforname ("UTF-8"));

    int index = Qfontdatabase::addapplicationfont ("/home/root/seat_imx/liheipro.ttf");
    if (index!=-1) {
        qstringlist fontlist (qfontdatabase::applicationfontfamilies (index));

        /* FONT--Lihei Pro/
        if (Fontlist.count () > 0) {
            qfont Font_zh (fontlist.at (0));
            Font_zh.setbold (false);
            A.setfont (Font_zh);
        }

    Gytboxwidget W;
    W.show ();

    return a.exec ();
}

Then you can display Chinese, such as:

qlabel *label = new Qlabel (this); Label->settext (TR ("China")); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.