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:
Code:
document.setHtml(html);
if(matriz == true)
{
QRect rectpag
= impressora.
pageRect();
palette.
setColor(backgroundRole
(),
QColor(255,
255,
255));
this->ui->tab_3->setPalette(palette);
this->ui->tab_3->setAutoFillBackground(true);
QPixmap pm
= this
->ui
->tab_3
->grab
();
pm = pm.transformed(QTransform().rotate(270));
QSize tamanho
= pm.
size();
tamanho.scale(rectpag.size(), Qt::KeepAspectRatio);
painter.drawPixmap(Qt::AlignCenter, Qt::AlignHCenter, tamanho.width(), tamanho.height(), pm);
document.drawContents(&painter);
this->ui->tab_3->setAutoFillBackground(false);
}
document.print(&impressora);
Thanks a lot.
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.
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?
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.
Re: Insert QPixmap in QTextDocument
Thanks ChrisW67. It worked. Code below:
Code:
document.setHtml(html);
if(matriz == true)
{
palette.
setColor(backgroundRole
(),
QColor(255,
255,
255));
this->ui->tab_3->setPalette(palette);
this->ui->tab_3->setAutoFillBackground(true);
QPixmap pm
= this
->ui
->tab_3
->grab
();
pm = pm.transformed(QTransform().rotate(270));
img = img.scaledToHeight(img.height()-60);
cursor.insertImage(img);
this->ui->tab_3->setAutoFillBackground(false);
}
document.print(&impressora);