Skip to content

Instantly share code, notes, and snippets.

@yuntan
Last active August 29, 2015 14:21
Show Gist options
  • Save yuntan/27be4dc1107a36e073be to your computer and use it in GitHub Desktop.
Save yuntan/27be4dc1107a36e073be to your computer and use it in GitHub Desktop.
QFontMetrics test

Output

Qt 4.8.6 On Arch Linux:

"Ricty,10,-1,5,50,0,0,0,0,0" 
width of  'あ'  =  13 
width of  '南'  =  13

Qt 5.4.1 On Arch Linux:

"Ricty,10,-1,5,50,0,0,0,0,0"
width of  'あ'  =  13
width of  '南'  =  7

Qt 5.5 beta On Arch Linux:

"Ricty,10,-1,5,50,0,0,0,0,0"
width of  '00\u3042'  =  13
width of  '00\u5357'  =  7
#include <QApplication>
#include <QFont>
#include <QFontMetrics>
#include <QTextLayout>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFont f("Ricty",10,50);
qDebug() << f.toString();
QFontMetrics fm(f);
QChar c1(12354);
QChar c2(21335);
qDebug() << "width of " << c1 << " = " << fm.width(c1);
qDebug() << "width of " << c2 << " = " << fm.width(c2);
// QTextLayout tl(QString(c2),f);
// qDebug() << tl.boundingRect();
return a.exec();
}
#-------------------------------------------------
#
# Project created by QtCreator 2015-05-23T21:29:42
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = qfontmetricstest
TEMPLATE = app
SOURCES += main.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment