PDA

View Full Version : QFontDatabase::addApplicationFontFromData && addApplicationFont returns -1



jyotinder
22nd December 2010, 06:04
I want to load hindi font form the resource. But whenever i tried use QFontDatabase::addApplicationFontFromData && addApplicationFont it returns -1 . Below is my code .


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QFile res("fonts/raghu.ttf");

if (res.open(QIODevice::ReadOnly) == false) {
qDebug()<<"error not able to load the file";

}

//File loaded sucessfully
qDebug()<<"File Information";
qDebug()<<"handle::"<<res.handle()<<"Size::"<<res.size()<<"\n";

//*QTextStream in(&res);
qDebug()<<res.readAll();

QByteArray data =res.readAll();

int fontID = QFontDatabase::addApplicationFontFromData( data );

if (fontID == -1 ) {
qDebug()<<"error";
}
else
qDebug()<<fontID;

ui->setupUi(this);
}


Kindly guide me where i am doing wrong . Thank you

ChrisW67
22nd December 2010, 06:42
Assuming that the file open is not failing, then you are attempting to open a file from a location relative to the current working directory. You are not opening the file from the Q resources, that would look something like:


QFile res(":/fonts/raghu.ttf");


Doing this:


qDebug()<<res.readAll(); // file read to end
QByteArray data =res.readAll(); // try to read past end of file

(as you do) will mean data.size() is always 0. This would explain the failure to load a font from the byte array.

True Type fonts will only work on Linux/UNIX systems with fontconfig installed.

Please use [code] tags around code in your posts.

jyotinder
22nd December 2010, 11:28
Hi ChrisW67 thank you for replying. I have installed fontconfig 2.6.0 on my system and using Qt 4.6.2. I am able to fetch the font(TrueType font (application/x-font-ttf)) form the resource, but QFontDatabase::addApplicationFontFromData return -1. Even though data.size() return non-zero value.



qDebug()<<"handle::"<<res.handle()<<"Size::"<<res.size()<<"\n";

Output::
handle::8 Size:: 81228



QByteArray data =res.readAll();
qDebug()<<"data size::"<<data.size();

Output::
data size:: 81228



int fontID = QFontDatabase::addApplicationFontFromData( data ); // returns -1
if (fontID == -1 ) {
qDebug()<<"error";
}
else
qDebug()<<fontID;

jyotinder
27th December 2010, 11:11
Issue resolved by upgrading to the QT 4.7

MeghaM
20th June 2012, 12:03
Hi

the readAll() function in qiodevice is unable to read ttf files.

The lines

QFile res(fileInfo.absoluteFilePath());
QByteArray data = res.readAll();

data gets the correct content of the file is a pdf file or any other file. But it always gets o when trying to read a ttf file. Anyone has any clue.

ChrisW67
21st June 2012, 00:40
the readAll() function in qiodevice is unable to read ttf files.
You are mistaken. Since you are not opening the file at all this readAll() call will return zero bytesl regardless of the file content.