PDA

View Full Version : QTextDocument confusing font issues



while_e
17th January 2018, 17:00
I'm attempting to generate a PDF file from `QTextDocument`, which is working just fine. The issue comes when I want to use a custom font for just a specific `<p></p>` object. I tried using the standard means of `QFontDatabase::addApplicationFont()` which successfully gets the font loaded into the application, but for some reason it writes all of the `QString` based variables that are added to `messageBody` in this font instead of that specified by `QTextFocument.setDefaultFont()`, or even a manually via `style={font-family:}`.

Am I doing this wrong, or overlooking something here, or is this some kind of bug?




int main(int argc, char *argv[])
{
QApplication a(argc,argv);

// With this added, all hell breaks loose
QFontDatabase::addApplicationFont(":/Barcode.ttf");

QString messageBody;
QString serialNumber = "ABC123";
messageBody += "<p style='font-size: large;'><b>Serial Number:</b> " + serialNumber + "</p>";
messageBody += "<p style='font-size: large; font-family=\"Console\"'><b>Serial Number:</b> " + serialNumber + "</p>";


QTextDocument document;
document.setHtml(messageBody);

QFont myfont("Console", 8, QFont::Normal);
document.setDefaultFont(myfont);

QPrinter printer(QPrinter::PrinterResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("test.pdf");

document.print(&printer);

return 0;
}



In either case I would get the output PDF like so:
12744

I can't wrap my head around why/how it would differentiate the base QString (messageBody) from those that are concatenated into it (serialNumber)?