How to add header and footer to an image?



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



  • @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.

    https://doc.qt.io/qt-5/qtwidgets-widgets-icons-example.html



  • @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.

    https://doc.qt.io/qt-5/qtwidgets-widgets-icons-example.html

    Thank you, but I do not see it in there.



  • @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.



  • @kenchan Thank you for your suggestion. Do you have any sample code?



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



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



  • @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 :-)



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



  • @kenchan That worked. Thank you.



  • @Zgembo Glad to hear that worked for you.
    If this issue is solved please mark it as solved so others will know when searching for a similar issue.


Log in to reply
 

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