How to add header and footer to an image?
-
wrote on 11 Feb 2019, 07:45 last edited by
Hi all,
Currently I am working on printing a screenshot of a screen and I have managed to do that. What I need now is to add header and footer to printed image. Header and footer would be rectangle object with text.
I have created a QGraphicScene object and I have added pixmap to that scene. On top of that pixmap I need to add header object with text and border and on the bottom I need to add footer with a text and a border.
What would be the best way of doing this?
-
wrote on 11 Feb 2019, 07:58 last edited by
@Zgembo Hi. Did you see this example? This was helpful for me before about qraphicscene. I didnt look at carefully but I believe your answer is in it.
-
@Zgembo Hi. Did you see this example? This was helpful for me before about qraphicscene. I didnt look at carefully but I believe your answer is in it.
wrote on 11 Feb 2019, 08:18 last edited by@Yunus said in How to add header and footer to an image?:
@Zgembo Hi. Did you see this example? This was helpful for me before about qraphicscene. I didnt look at carefully but I believe your answer is in it.
Thank you, but I do not see it in there.
-
@Yunus said in How to add header and footer to an image?:
@Zgembo Hi. Did you see this example? This was helpful for me before about qraphicscene. I didnt look at carefully but I believe your answer is in it.
Thank you, but I do not see it in there.
wrote on 11 Feb 2019, 10:43 last edited by@Zgembo
What you need to do is add the graphics items for the header and footer above and below the bitmap in the scene.
Like text or text inside rectangles.
Easiest way would be to add the header text first, then the pixmap below that and the footer text below that.
You must manage the coordinates and sizes of the graphics items yourself. -
@Zgembo
What you need to do is add the graphics items for the header and footer above and below the bitmap in the scene.
Like text or text inside rectangles.
Easiest way would be to add the header text first, then the pixmap below that and the footer text below that.
You must manage the coordinates and sizes of the graphics items yourself. -
wrote on 11 Feb 2019, 12:33 last edited by Zgembo 2 Nov 2019, 12:35
I am trying to prepare central graph in a way that it has rectangle border around pixmap.
// page size in pixels
QSizeF pageSize = printer.pageRect().size();//this is the original widget screenshot QPixmap *screenshot = new QPixmap(printWidget->getMainPlot()->toPixmap()); //here we crop original pixmap to selected paper size based on selected paper size QPixmap *croppedScreenShot = new QPixmap(screenshot->copy(0, 0, pageSize.width(), pageSize.height())); //draw border around screenshot QPainter *paintScreenShot = new QPainter(croppedScreenShot); paintScreenShot->setPen(*(new QColor(Qt::black))); paintScreenShot->drawRect(0, 0, croppedScreenShot->width(), croppedScreenShot->height()); //end border
But my croppedScreenShot only has top and left border. Right and button borders are not drawn.
-
wrote on 11 Feb 2019, 12:44 last edited by
I figured it out.
When drawing a rect I had to substract -1 from width and height and left and button borders are shown now.
I have put his image into QGraphicScene. What I have not figured out is how to add one rectangle above and bellow with some text. -
I am trying to prepare central graph in a way that it has rectangle border around pixmap.
// page size in pixels
QSizeF pageSize = printer.pageRect().size();//this is the original widget screenshot QPixmap *screenshot = new QPixmap(printWidget->getMainPlot()->toPixmap()); //here we crop original pixmap to selected paper size based on selected paper size QPixmap *croppedScreenShot = new QPixmap(screenshot->copy(0, 0, pageSize.width(), pageSize.height())); //draw border around screenshot QPainter *paintScreenShot = new QPainter(croppedScreenShot); paintScreenShot->setPen(*(new QColor(Qt::black))); paintScreenShot->drawRect(0, 0, croppedScreenShot->width(), croppedScreenShot->height()); //end border
But my croppedScreenShot only has top and left border. Right and button borders are not drawn.
wrote on 11 Feb 2019, 12:50 last edited by kenchan 2 Nov 2019, 12:50@Zgembo
I don't see any QGraphicsScene in you code??
Anyways, if you know how big you want the header text to be just add it to the scene first.
You will know the height of the text you want to draw so just put the bitmap item below that.
Then, you know the height of your pixmap so add the footer text below that.
Sorry i don't have and sample code for this but it is just simple graphics items stuff. I am sure you can figure it out :-) -
wrote on 11 Feb 2019, 14:55 last edited by
I am trying to construct footer pixmap which needs to have some wrapped text.
QPixmap *foterPixmap = new QPixmap(pageSize.width(), 60);
QPainter foterPainter = new QPainter(foterPixmap);
foterPainter->setPen((new QColor(Qt::black)));
foterPainter->drawRect(0, 0, foterPixmap->width() - 1, foterPixmap->height() - 1);
foterPainter->setFont(font);
QString foterText = "Comment: " + getComment();
QRect boundingRect = foterPixmap->rect();
foterPainter->drawText(foterPixmap->rect(), Qt::AlignLeft|Qt::AlignTop|QTextOption::WrapAtWordBoundaryOrAnywhere,foterText, &boundingRect);Whatever I do the "foterText" is not wrapper. What am I missing here?
-
I am trying to construct footer pixmap which needs to have some wrapped text.
QPixmap *foterPixmap = new QPixmap(pageSize.width(), 60);
QPainter foterPainter = new QPainter(foterPixmap);
foterPainter->setPen((new QColor(Qt::black)));
foterPainter->drawRect(0, 0, foterPixmap->width() - 1, foterPixmap->height() - 1);
foterPainter->setFont(font);
QString foterText = "Comment: " + getComment();
QRect boundingRect = foterPixmap->rect();
foterPainter->drawText(foterPixmap->rect(), Qt::AlignLeft|Qt::AlignTop|QTextOption::WrapAtWordBoundaryOrAnywhere,foterText, &boundingRect);Whatever I do the "foterText" is not wrapper. What am I missing here?
-
@Zgembo
I am not sure if the QTextOption:: stuff works with the painter drawText function.
According to the docs it uses Qt::TextWordWrap for that.
2/12