Draw QWidget under Text in QTextDocument



  • I want to print a Text with a given QWidget under the Text, but everytime i open my PrintPreview the QWidget is painted over the Text. The QWidget always starts to paint at the Top Left Corner of the QTextDocument. How can i solve this? I need to know the Size of the actually Text and start painting it under this Position. This is my Code at the Moment. Im a little bit confused.

    void Printer::printDiaryEvent(QPrinter *printer)
    {
        QTextDocument doc;
        QString html;
        html.append("<h2>" + mDate + "</h2><br><br>");
        html.append(mDiaryText);
        doc.setHtml(html);
        doc.documentLayout()->setPaintDevice(printer);
        doc.setPageSize(printer->pageRect().size());
        QPainter painter(printer);
        mRating->render(&painter);
        doc.drawContents(&painter, QRectF(doc.size().height(), 0, mRating->width(), mRating->height()));
        doc.print(printer);
    }
    


  • @Fuel
    I know nothing, but is there a good reason why you are passing the doc height as the X (rather than Y) coordinate...?



  • It makes no difference. QWidget is always on Top of the Document.



  • void Printer::printDiaryEvent(QPrinter *printer)
    {
        QTextDocument doc;
        QString html;
        html.append("<h2>" + mDate + "</h2><br><br>");
        html.append(mDiaryText);
    QTextCursor cursor(&doc);
    cursor.insertHtml(html);
    // cursor.insertBlock(); // you might need this to have a separation between image and text, not sure
    cursor.insertImage(mRating.grab().toImage());
        doc.documentLayout()->setPaintDevice(printer);
        doc.setPageSize(printer->pageRect().size());
        doc.print(printer);
    }
    


  • @VRonin nice thanks it works.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.