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.setOutputFileName(fileDir);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setPaperSize(QPrinter::A4);
    printer.setPageMargins(20,20,20,20, QPrinter::Millimeter);
    printer.setColorMode(QPrinter::Color);
    printer.setResolution(300);

    //! Initialise le document et le style
    //! Document
    QTextDocument *doc = new QTextDocument();
    doc->setPageSize(printer.paperSize(QPrinter::Millimeter));
    doc->setDefaultFont(QFont("Arial", 4));
    QTextCursor cursor(doc);

    QTextImageFormat picture1;
    picture1.setName("./imagename.png");
    picture1.setWidth(150);
    

    QImage picture2;
    picture2.load("./imagename.png");
    picture2 = picture2.scaledToWidth(150);

    cursor.insertImage(picture1,QTextFrameFormat::InFlow);
    cursor.insertText("\n");
    cursor.insertImage(picture2);

    doc->print(&printer);
    

    @

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

    Thanks



  • 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!

    Bye!



  • 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
 

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