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. QLabel show image and drawLine
Qt 6.11 is out! See what's new in the release blog

QLabel show image and drawLine

Scheduled Pinned Locked Moved General and Desktop
14 Posts 5 Posters 9.0k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    leafjungle
    wrote on last edited by
    #1

    I want to draw a line in a image, and I am using QLabel.

    first:
    ui.mylabel->setPixmap(QPixmap::fromImage(img1));//display an image

    second:
    void paintEvent(QPaintEvent* ev){//override the paintEvent of Main Window

     QPainter draw(this);
     draw.drawLine(100, 100, 200,200);
    

    }

    But the line cannot be shown, because it is covered by the image.
    The image should be a background for mylabel, and the line should be in the top layer.

    How can I solve the problem?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,
      Have you subclassed QLabel and reimplemented paintevent() ?

      If so, then you must first drawPixmap first and then drawLine to make it overlap.

      157

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leafjungle
        wrote on last edited by
        #3

        Is there any other solutions? I have reimplemented the paintEvent of the main windows, which contains a label.

        [quote author="p3c0" date="1381121484"]Hi,
        Have you subclassed QLabel and reimplemented paintevent() ?

        If so, then you must first drawPixmap first and then drawLine to make it overlap.[/quote]

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          What do you exactly want to do ? Can you elaborate more ?

          So far i understood is
          You have a QMainWindow whose paintEvent you have reimplemented(not a good idea) then it has a QLabel which you have added in your form and on which you want to draw the line ?

          157

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            [quote author="leafjungle" date="1381125023"]Is there any other solutions? I have reimplemented the paintEvent of the main windows, which contains a label.[/quote]

            That's not how it works. Each widget just draws it's own contents, but not it's child widgets. Child widgets are stacked on top and draw it's own contents, and so on...

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • L Offline
              L Offline
              leafjungle
              wrote on last edited by
              #6

              Yes.

              What I want is:
              put an image on the QMainWindow, then draw a line on the image.

              And the problem is:
              The line is covered by the image, thus I can only see the image, the line is not visible.

              [quote author="p3c0" date="1381125914"]What do you exactly want to do ? Can you elaborate more ?

              So far i understood is
              You have a QMainWindow whose paintEvent you have reimplemented(not a good idea) then it has a QLabel which you have added in your form and on which you want to draw the line ?[/quote]

              1 Reply Last reply
              0
              • L Offline
                L Offline
                leafjungle
                wrote on last edited by
                #7

                I understand your points.

                But if I want to put an image on the QMainWindow, then draw line on the QMainWindow. How can I make the line on top layer to make sure that it will not be covered by the image.

                [quote author="raven-worx" date="1381127103"][quote author="leafjungle" date="1381125023"]Is there any other solutions? I have reimplemented the paintEvent of the main windows, which contains a label.[/quote]

                That's not how it works. Each widget just draws it's own contents, but not it's child widgets. Child widgets are stacked on top and draw it's own contents, and so on...
                [/quote]

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  leafjungle
                  wrote on last edited by
                  #8

                  !http://c.hiphotos.baidu.com/album/w=2048;q=90/sign=27d2541638f33a879e6d071af2642b49/d31b0ef41bd5ad6e2f2fb3cc83cb39dbb7fd3cc6.jpg(image1)!
                  http://f.hiphotos.bdimg.com/album/s=1400;q=90/sign=bc133aefd52a60595610e51e18040fea/d53f8794a4c27d1e3f07ede219d5ad6eddc438f7.jpg

                  !http://f.hiphotos.baidu.com/album/w=2048;q=90/sign=b6a92afc17ce36d3a20484300ecb01f6/d53f8794a4c27d1e3f07ede219d5ad6eddc438f7.jpg(image2)!
                  http://c.hiphotos.baidu.com/album/w=2048;q=90/sign=27d2541638f33a879e6d071af2642b49/d31b0ef41bd5ad6e2f2fb3cc83cb39dbb7fd3cc6.jpg

                  Look at the image above. what I want is image1, but the problem is shown in image2.

                  [quote author="raven-worx" date="1381127103"][quote author="leafjungle" date="1381125023"]Is there any other solutions? I have reimplemented the paintEvent of the main windows, which contains a label.[/quote]

                  That's not how it works. Each widget just draws it's own contents, but not it's child widgets. Child widgets are stacked on top and draw it's own contents, and so on...
                  [/quote]

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    leafjungle
                    wrote on last edited by
                    #9

                    http://f.hiphotos.bdimg.com/album/s=1400;q=90/sign=bc133aefd52a60595610e51e18040fea/d53f8794a4c27d1e3f07ede219d5ad6eddc438f7.jpg

                    http://c.hiphotos.baidu.com/album/w=2048;q=90/sign=27d2541638f33a879e6d071af2642b49/d31b0ef41bd5ad6e2f2fb3cc83cb39dbb7fd3cc6.jpg

                    1 Reply Last reply
                    0
                    • p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #10

                      hi,

                      Both seems to be identical images. Can't see the problem.

                      157

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jakakishan
                        wrote on last edited by
                        #11

                        if you First draw line and then set image then it may be solve.
                        sorry for poor eng.

                        Jkishan

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          ChrisW67
                          wrote on last edited by
                          #12

                          If you want the label to have the image with a line drawn on top then it is the paintEvent() of the QLabel you want to enhance.
                          @
                          class MyLabelSubclass: public QLabel {
                          ...
                          protected:
                          void paintEvent(QPaintEvent* ev);
                          };

                          //override the paintEvent of QLabel to draw a line
                          void MyLabelSubclass::paintEvent(QPaintEvent* ev) {
                          QLabel::paintEvent(ev); // do the default label display

                          QPainter draw(this);
                          draw.setPen(QPen(QColor(Qt::red)));
                          draw.drawLine(rect().topLeft(), rect().bottomRight());
                          }
                          @
                          Then use MyLabelSubclass on the form rather than a base QLabel.

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            leafjungle
                            wrote on last edited by
                            #13

                            In the second image, a part of the line is covered by the image.

                            [quote author="p3c0" date="1382158070"]hi,

                            Both seems to be identical images. Can't see the problem.[/quote]

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              ChrisW67
                              wrote on last edited by
                              #14

                              There is no "In the second image, a part of the line is covered by the image," because there is no "part of the line" visible in the second. The two images above are byte for byte identical.
                              @
                              chrisw@newton /tmp $ md5sum *jpg
                              1b4061d36b2069e2956457d51fc6d3ad d31b0ef41bd5ad6e2f2fb3cc83cb39dbb7fd3cc6.jpg
                              1b4061d36b2069e2956457d51fc6d3ad d53f8794a4c27d1e3f07ede219d5ad6eddc438f7.jpg
                              @

                              1 Reply Last reply
                              0

                              • Login

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