Results 1 to 6 of 6

Thread: QFontDatabase::addApplicationFontFromData && addApplicationFont returns -1

  1. #1
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QFontDatabase::addApplicationFontFromData && addApplicationFont returns -1

    I want to load hindi font form the resource. But whenever i tried use QFontDatabase::addApplicationFontFromData && addApplicationFont it returns -1 . Below is my code .

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. QFile res("fonts/raghu.ttf");
    6.  
    7. if (res.open(QIODevice::ReadOnly) == false) {
    8. qDebug()<<"error not able to load the file";
    9.  
    10. }
    11.  
    12. //File loaded sucessfully
    13. qDebug()<<"File Information";
    14. qDebug()<<"handle::"<<res.handle()<<"Size::"<<res.size()<<"\n";
    15.  
    16. //*QTextStream in(&res);
    17. qDebug()<<res.readAll();
    18.  
    19. QByteArray data =res.readAll();
    20.  
    21. int fontID = QFontDatabase::addApplicationFontFromData( data );
    22.  
    23. if (fontID == -1 ) {
    24. qDebug()<<"error";
    25. }
    26. else
    27. qDebug()<<fontID;
    28.  
    29. ui->setupUi(this);
    30. }
    To copy to clipboard, switch view to plain text mode 

    Kindly guide me where i am doing wrong . Thank you
    Last edited by Lykurg; 22nd December 2010 at 11:40. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Post Re: QFontDatabase::addApplicationFontFromData && addApplicationFont returns -1

    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:
    Qt Code:
    1. QFile res(":/fonts/raghu.ttf");
    To copy to clipboard, switch view to plain text mode 

    Doing this:
    Qt Code:
    1. qDebug()<<res.readAll(); // file read to end
    2. QByteArray data =res.readAll(); // try to read past end of file
    To copy to clipboard, switch view to plain text mode 
    (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.

  3. #3
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFontDatabase::addApplicationFontFromData && addApplicationFont returns -1

    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.

    Qt Code:
    1. qDebug()<<"handle::"<<res.handle()<<"Size::"<<res.size()<<"\n";
    To copy to clipboard, switch view to plain text mode 
    Output::
    handle::8 Size:: 81228

    Qt Code:
    1. QByteArray data =res.readAll();
    2. qDebug()<<"data size::"<<data.size();
    To copy to clipboard, switch view to plain text mode 
    Output::
    data size:: 81228
    Qt Code:
    1. int fontID = QFontDatabase::addApplicationFontFromData( data ); // returns -1
    2. if (fontID == -1 ) {
    3. qDebug()<<"error";
    4. }
    5. else
    6. qDebug()<<fontID;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFontDatabase::addApplicationFontFromData && addApplicationFont returns -1

    Issue resolved by upgrading to the QT 4.7

  5. #5
    Join Date
    Jun 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QFontDatabase::addApplicationFontFromData && addApplicationFont returns -1

    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.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QFontDatabase::addApplicationFontFromData && addApplicationFont returns -1

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.