Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add header and footer to an image?

How to add header and footer to an image?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zgembo
    wrote on 11 Feb 2019, 07:45 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      Yunus
      wrote on 11 Feb 2019, 07:58 last edited by
      #2

      @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

      Z 1 Reply Last reply 11 Feb 2019, 08:18
      0
      • Y Yunus
        11 Feb 2019, 07:58

        @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

        Z Offline
        Z Offline
        Zgembo
        wrote on 11 Feb 2019, 08:18 last edited by
        #3

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

        K 1 Reply Last reply 11 Feb 2019, 10:43
        0
        • Z Zgembo
          11 Feb 2019, 08:18

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

          K Offline
          K Offline
          kenchan
          wrote on 11 Feb 2019, 10:43 last edited by
          #4

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

          Z 1 Reply Last reply 11 Feb 2019, 10:45
          0
          • K kenchan
            11 Feb 2019, 10:43

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

            Z Offline
            Z Offline
            Zgembo
            wrote on 11 Feb 2019, 10:45 last edited by
            #5

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

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              Zgembo
              wrote on 11 Feb 2019, 12:33 last edited by Zgembo 2 Nov 2019, 12:35
              #6

              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.

              K 1 Reply Last reply 11 Feb 2019, 12:50
              0
              • Z Offline
                Z Offline
                Zgembo
                wrote on 11 Feb 2019, 12:44 last edited by
                #7

                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.

                1 Reply Last reply
                0
                • Z Zgembo
                  11 Feb 2019, 12:33

                  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.

                  K Offline
                  K Offline
                  kenchan
                  wrote on 11 Feb 2019, 12:50 last edited by kenchan 2 Nov 2019, 12:50
                  #8

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

                  1 Reply Last reply
                  1
                  • Z Offline
                    Z Offline
                    Zgembo
                    wrote on 11 Feb 2019, 14:55 last edited by
                    #9

                    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?

                    K 1 Reply Last reply 11 Feb 2019, 15:30
                    0
                    • Z Zgembo
                      11 Feb 2019, 14:55

                      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?

                      K Offline
                      K Offline
                      kenchan
                      wrote on 11 Feb 2019, 15:30 last edited by
                      #10

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

                      Z 1 Reply Last reply 12 Feb 2019, 07:04
                      3
                      • K kenchan
                        11 Feb 2019, 15:30

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

                        Z Offline
                        Z Offline
                        Zgembo
                        wrote on 12 Feb 2019, 07:04 last edited by
                        #11

                        @kenchan That worked. Thank you.

                        K 1 Reply Last reply 12 Feb 2019, 07:54
                        0
                        • Z Zgembo
                          12 Feb 2019, 07:04

                          @kenchan That worked. Thank you.

                          K Offline
                          K Offline
                          kenchan
                          wrote on 12 Feb 2019, 07:54 last edited by
                          #12

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

                          1 Reply Last reply
                          0

                          7/12

                          11 Feb 2019, 12:44

                          • Login

                          • Login or register to search.
                          7 out of 12
                          • First post
                            7/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved