Hello,
I want to print on a paper(or pdf...) a picture, scaled to the maximum of the page, according to the picture aspectRatio. Then I want to display under this picture some RichText.
I have found three solutions, but each one has a serious drawback
The fisrt one correctly resize the picture, but doesn't enable me to put RichText(only normal text). I made an example below
#include <QtGui>
int main(int argc, char *argv[])
{
bouton.show();
m_Printer.
setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("baseprinted.pdf");
QRect rect
= painter.
viewport();
QRect OldWindow
= painter.
window();
QPixmap m_Image
("c:\\picture.jpg");
QSize size
= m_Image.
size ();
size.scale (rect.size (), Qt::KeepAspectRatio);
painter.setViewport (0, 0, size.width (), size.height ());
painter.setWindow (m_Image.rect ());
painter.drawPixmap (m_Image.rect(), m_Image);
painter.setViewport(rect);
painter.setWindow(OldWindow);
QFont ComicMS
("Comic Sans MS",
40);
painter.setFont(ComicMS);
tmp.setX(0);
tmp.setY(size.height());
tmp.setWidth(rect.width());
tmp.setHeight(rect.height()-size.height());
painter.drawText(tmp, Qt::AlignHCenter | Qt::AlignTop, "Texte <b>!</b>");
return app.exec();
}
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton bouton("Just a Button");
bouton.show();
QPrinter m_Printer(QPrinter::HighResolution);
m_Printer.setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("baseprinted.pdf");
QPainter painter (&m_Printer);
QRect rect = painter.viewport();
QRect OldWindow= painter.window();
QPixmap m_Image("c:\\picture.jpg");
QSize size = m_Image.size ();
size.scale (rect.size (), Qt::KeepAspectRatio);
painter.setViewport (0, 0, size.width (), size.height ());
painter.setWindow (m_Image.rect ());
painter.drawPixmap (m_Image.rect(), m_Image);
painter.setViewport(rect);
painter.setWindow(OldWindow);
QRect tmp;
QFont ComicMS("Comic Sans MS", 40);
painter.setFont(ComicMS);
tmp.setX(0);
tmp.setY(size.height());
tmp.setWidth(rect.width());
tmp.setHeight(rect.height()-size.height());
painter.drawText(tmp, Qt::AlignHCenter | Qt::AlignTop, "Texte <b>!</b>");
return app.exec();
}
To copy to clipboard, switch view to plain text mode
The second one, enables me to put html, but the picture hasn't the good dimensions(it could be larger)
m_Printer.
setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("printed.pdf");
m_Printer.
setPageMargins(0,
0,
0,
0,
QPrinter::Point);
Docu.
addResource(QTextDocument::ImageResource,
QUrl( "monImage.jpg" ), m_Image.
scaled( m_Printer.
paperRect(QPrinter::Point).
size().
toSize() ,Qt
::KeepAspectRatio, Qt
::SmoothTransformation) );
Docu.setHtml("<img src=\"monImage.jpg\"/><br/><b>Hello</b> World");
Docu.print(&m_Printer);
m_Printer.setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("printed.pdf");
m_Printer.setPageMargins(0,0,0,0,QPrinter::Point);
QTextDocument Docu;
Docu.addResource(QTextDocument::ImageResource, QUrl( "monImage.jpg" ), m_Image.scaled( m_Printer.paperRect(QPrinter::Point).size().toSize() ,Qt::KeepAspectRatio, Qt::SmoothTransformation) );
Docu.setHtml("<img src=\"monImage.jpg\"/><br/><b>Hello</b> World");
Docu.print(&m_Printer);
To copy to clipboard, switch view to plain text mode
Then the last solution. It works well, but the text is to small(it's logic). And I dont know how to add other text at the rigtht size.
m_Printer.
setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("mixteprinted.pdf");
QRect rect
= painter.
viewport();
QRect OldWindow
= painter.
window();
QSize size
= m_Image.
size ();
size.scale (rect.size (), Qt::KeepAspectRatio);
painter.setViewport (0, 0, size.width (), size.height ());
painter.setWindow (m_Image.rect ());
Docu.setHtml("<img src=\"monImage.jpg\"/><br/><b>Hello</b> World");
Docu.drawContents(&painter);
Docu.setHtml("<b>Hello</b> GUYS");
painter.setViewport(rect);
painter.setWindow(OldWindow);
QFont ComicMS
("Comic Sans MS",
40);
Docu.setDefaultFont(ComicMS);
Docu.drawContents(&painter);
m_Printer.setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("mixteprinted.pdf");
QPainter painter (&m_Printer);
QRect rect = painter.viewport();
QRect OldWindow= painter.window();
QSize size = m_Image.size ();
size.scale (rect.size (), Qt::KeepAspectRatio);
painter.setViewport (0, 0, size.width (), size.height ());
painter.setWindow (m_Image.rect ());
QTextDocument Docu;
Docu.addResource(QTextDocument::ImageResource, QUrl( "monImage.jpg" ), m_Image );
Docu.setHtml("<img src=\"monImage.jpg\"/><br/><b>Hello</b> World");
Docu.drawContents(&painter);
Docu.setHtml("<b>Hello</b> GUYS");
painter.setViewport(rect);
painter.setWindow(OldWindow);
QFont ComicMS("Comic Sans MS", 40);
Docu.setDefaultFont(ComicMS);
Docu.drawContents(&painter);
To copy to clipboard, switch view to plain text mode
Each solution has a drawback that I cant avoid. I'm focused on the last solution, where my goal is then just to add some text at the right size after the Docu.drawContents(&painter);
But after many tryes, I can't find tsomething similar to painter.drawText() that put the text at the right size.
If you have any Ideas?
Thank you and Merry Christmas
Bookmarks