Saturday, May 4, 2013

Qt 5 code showing how to use Linguist and how to install user defined font

using QFontdatabase and QFont we can use font that is not install on our system on a Qt5 application, we simply load the font using QFile and add it font database. 


//linguistic.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TRANSLATIONS = amharic_ln.ts
TARGET = linguistic
TEMPLATE = app
SOURCES += main.cpp\
        dialog.cpp

HEADERS  += dialog.h
FORMS    += dialog.ui
CODECFORTR = UTF-8


//main.cpp
#include "dialog.h"
#include <QApplication>
#include <QTranslator>
#include <QFont>
#include <QFontDatabase>
#include <QFile>
#include <QDebug>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFile res("C:/Documents and Settings/user/My Documents/Qt Projects/linguistic/gfzemen.ttf");

    res.open(QIODevice::ReadOnly);
    QFontDatabase fdata;
    fdata.addApplicationFontFromData(res.readAll());
    Dialog w;

    QFont f("GF Zemen Unicode");
    f.setPointSize(12);
    w.setFont(f);
    w.show();
 
    return a.exec();
}

//dialog.h
#ifndef DIALOG_H
#define DIALOG_H
 
#include <QDialog>
#include <QTranslator>
#include <QDebug>
#include <QDir>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT
    
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    
private:
    Ui::Dialog *ui;
    QApplication *tapp;
    QTranslator translate;
    QString file;
};

#endif // DIALOG_H

//dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent):  QDialog(parent), ui(new Ui::Dialog)
{
    translate.load("C:/Documents and Settings/user/My Documents/Qt Projects/linguistic/amharic_ln.qm");
    tapp->installTranslator(&translate);

    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}

No comments:

Post a Comment

How to create Custom push button in QT5

To be honest this is not my work its an exact copy from this link. http://mcmtechtips.blogspot.com/2012/03/shape-qpushbutton-in-your-own-st...