QPainter::draw() ingnores \n

  • Hi,

    i have a QString with a longer text that contains \n for new lines. The QLabel displays everything correctly. Now i want to save this text as pdf file. I tried Printing the QLabel, but that's not so good because of the resolution. So i thought the better way is to print the text directly instead of display it on a label and print that label.

    QPdfWriter pdfwriter("qtwritten.pdf");
    QPainter painter(&pdfwriter);
    painter.drawText(0, 0, "Hello\nworld!");

    The Result is 0_1541330039923_Unbenannt.JPG

    Any solution ideas? Or maybe alternatives to write the text to a pdf file?

  • Lifetime Qt Champion


    painter->drawText(this->rect(), Qt::AlignCenter,"Hello\nworld!");

    We give it an area (rect) instead of x,y only

Log in to reply