Resolution issue with QTextCursor::insertImage ( const QImage & image, const QString & name = QString() )

  • Hi group,
    I'd like to insert a QImage inside a QTextDocument and save it as a PDF file.
    To do this i use the QTextCursor::insertImage ( const QImage & image, const QString & name = QString() ) method
    but the image is pixelized.
    With the same image and with the QTextCursor::insertImage ( const QTextImageFormat & format, QTextFrameFormat::Position alignment ) method it is fine.

    there is an example:
    @//! Initialise l'imprimante
    QPrinter printer;
    printer.setPageMargins(20,20,20,20, QPrinter::Millimeter);

    //! Initialise le document et le style
    //! Document
    QTextDocument *doc = new QTextDocument();
    doc->setDefaultFont(QFont("Arial", 4));
    QTextCursor cursor(doc);

    QTextImageFormat picture1;

    QImage picture2;
    picture2 = picture2.scaledToWidth(150);




    Does anyone know that issue or does anyone see my mistake?


  • Hi Vincent,
    I know it's bit too late to give you an answer.
    I'm not sure about what I'm giving to you know, but I think this is a normal feature. I explain myself:
    I a first way, with a QImage, you make a real transformation on your Image. If your original image have a resolution of 20002000 for example, after setWidth(150);, your image is now a matrix of 150150.

    In a another way, with QTextImageFormat, your using a kind of container composed with an image. When you try to resize it with setWidth your operation don't involve a transformation onimage data but only a transform on the container. So, you got the same image (composed of 2000*2000 pixel for example), in a shorter area.
    So you are increasing the dpi with this way, data are not reduced, only the size!


  • Sorry for double post,
    An example to insert a QImage without losing image resolution:

    //Adding image to ressource
    QUuid uid           = QUuid::createUuid();//Creating a name for our ressource
    p_cursor.document()->addResource(QTextDocument::ImageResource, QUrl(uid.toString()), m_markerImage);
    //Creating a container for our image and insert it to document
    QTextImageFormat format;
    format.setName      ( uid.toString());
    format.setWidth     ( p_cursor.document()->idealWidth ());
    p_cursor.insertBlock( blockFormat, charFormat);
    p_cursor.insertImage( format);

    I hope it can be useful!
    Best regards

Log in to reply