Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextDocument.setHtml() + CSS @font-face

QTextDocument.setHtml() + CSS @font-face

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • OneidavarO Offline
    OneidavarO Offline
    Oneidavar
    wrote on last edited by
    #1

    Hi,
    I'm using a QTextDocument and the setHtml() function the generate some documents.
    On the document i'd like to have a specific text in a custom font.

    QString css = "..."; //some css
    css += "@font-face { font-family: MyFont; src: url('MyFont.ttf'); }";
    
    QString html = ".." //some html
    html += "<span style=\"font-family:myFont;\">text</span>";
    
    doc.setHtml(css+html)
     
    

    but sadly the text in the document doesn't change to my font.
    The file "MyFont.tff" is in the same directory as the binary;
    Also: installing the font in C:\Windows\Fonts is not an option;
    How can I asign MyFont to the text?

    thx for helping ;)

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      hi and welcome
      Try with

      QString FullPathFont( qApp->applicationDirPath()+"/"+" "MyFont.tff");
      qDebug() << "font:" << FullPathFont; // please check its correct :)
      int id = QFontDatabase::addApplicationFont(FullPathFont);
      

      check id. -1 means not loaded.

      This should make the font available to your app .

      1 Reply Last reply
      1
      • OneidavarO Offline
        OneidavarO Offline
        Oneidavar
        wrote on last edited by Oneidavar
        #3

        Thank you,
        Problem solved.
        For future reference:

        int fontID = QFontDatabase::addApplicationFont("MyFont.ttf");
        QString fontFamily = QFontDatabase::applicationFontFamilies(fontID).at(0);
        ...
        html += "<span style=\""+fontFamily+"\">text</span>";
        
        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved