Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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");
    pdfwriter.setPageSize(QPageSize(QPageSize::A4));
    QPainter painter(&pdfwriter);
    
    painter.setFont(*m_font);
    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

    Hi
    Try

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

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


Log in to reply