Results 1 to 5 of 5

Thread: Insert QPixmap in QTextDocument

  1. #1
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Insert QPixmap in QTextDocument

    Hello guys!

    I need to insert a QPixmap in a QTextDocument and all I tried didnt work.

    If anyone could help me. Code below:

    Qt Code:
    1. QPrinter impressora(QPrinter::HighResolution);
    2. QTextDocument document;
    3. document.setHtml(html);
    4. if(matriz == true)
    5. {
    6. QPainter painter;
    7.  
    8. QPalette palette;
    9.  
    10. QRect rectpag = impressora.pageRect();
    11.  
    12. palette.setColor(backgroundRole(), QColor(255, 255, 255));
    13. this->ui->tab_3->setPalette(palette);
    14. this->ui->tab_3->setAutoFillBackground(true);
    15.  
    16. QPixmap pm = this->ui->tab_3->grab();
    17. pm = pm.transformed(QTransform().rotate(270));
    18. QSize tamanho = pm.size();
    19.  
    20. tamanho.scale(rectpag.size(), Qt::KeepAspectRatio);
    21.  
    22. painter.drawPixmap(Qt::AlignCenter, Qt::AlignHCenter, tamanho.width(), tamanho.height(), pm);
    23.  
    24. document.drawContents(&painter);
    25. this->ui->tab_3->setAutoFillBackground(false);
    26. }
    27. document.print(&impressora);
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Insert QPixmap in QTextDocument

    Your code never tries to insert an image into a QTextDocument. Inserting an image is done with QTextCursor::insertImage() and a suitable QTextImageFormat that names an image resource that has been added using QTextDocument::addResource(). The last link shows an example of this process.

    I have not tried it, but I expect that calling QTextDocument::setHtml() with HTML that references an accessible image through an img element should also work.


    What your code does is try to paint an image onto a QPainter that is not associated with a paint device (through the constructor or QPainter::begin()). You then draw the contents of the QTextDocument into the same uninitialised painter. When you finally call QTextDocument::print() the document is unchanged and image free.

  3. #3
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Insert QPixmap in QTextDocument

    Thank you for the reply ChrisW67. But I need to insert an image that in not saved in the HD. I can create an image from the Qpixmap and even doing that it will not be in my HD. Can I insert that image with QTextCursor?
    Last edited by guidupas; 26th March 2014 at 14:23.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Insert QPixmap in QTextDocument

    Yes, exactly as in the example QTextDocument::addResource() example I linked to before. The URL is just a unique identifier, it does not have to point at a web or file resource.

  5. #5
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Insert QPixmap in QTextDocument

    Thanks ChrisW67. It worked. Code below:
    Qt Code:
    1. QPrinter impressora(QPrinter::HighResolution);
    2.  
    3. QTextDocument document;
    4. document.setHtml(html);
    5. if(matriz == true)
    6. {
    7. QTextCursor cursor(&document);
    8.  
    9. QPalette palette;
    10.  
    11. palette.setColor(backgroundRole(), QColor(255, 255, 255));
    12. this->ui->tab_3->setPalette(palette);
    13. this->ui->tab_3->setAutoFillBackground(true);
    14.  
    15. QPixmap pm = this->ui->tab_3->grab();
    16. pm = pm.transformed(QTransform().rotate(270));
    17.  
    18. cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
    19.  
    20. QImage img = pm.toImage();
    21. img = img.scaledToHeight(img.height()-60);
    22. cursor.insertImage(img);
    23.  
    24. this->ui->tab_3->setAutoFillBackground(false);
    25. }
    26.  
    27. document.print(&impressora);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 4th November 2009, 07:12
  2. Insert SVG inside QTextDocument
    By giusepped in forum Qt Programming
    Replies: 6
    Last Post: 23rd April 2009, 20:57
  3. Insert SVG file into QtextDocument
    By giusepped in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 05:15
  4. Insert QwtPlot in QTextDocument
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 9th April 2009, 10:51
  5. Replies: 7
    Last Post: 12th August 2006, 16:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.