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 draw and change the region (semi-transparent) on top of a QLabel or QImage?
Qt 6.11 is out! See what's new in the release blog

How to draw and change the region (semi-transparent) on top of a QLabel or QImage?

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 5 Posters 17.3k Views 2 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by Mikeeeeee
    #5

    Wrong, I see a line. How to draw when you click the mouse under the mouse, as in Paint.

    mrjjM 1 Reply Last reply
    0
    • M Mikeeeeee

      Wrong, I see a line. How to draw when you click the mouse under the mouse, as in Paint.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #6

      @Mikeeeeee
      You use mousePress / mouseRelease and mouseMove events for that.
      Look here.
      https://github.com/peteristhegreat/persistent_paint/blob/master/paintwidget.cpp
      to see the logic. Basically we store the point on press, and update it in mouseMove, if
      we see mouseRelease, the line is done.
      You need the similar code in your MyLabel.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #7

        Do you see the image when you don't reimplement your paint event? I would guess no.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mikeeeeee
          wrote on last edited by
          #8

          PaintWidget create QPixmap m_nPTargetPixmap and draws onm_nPTargetPixmap.
          I set Qimage for Qlabel. Can I set Qpixmap on top of QImage in Qlabel and draw on Qpixmap?

          mrjjM 1 Reply Last reply
          0
          • M Mikeeeeee

            PaintWidget create QPixmap m_nPTargetPixmap and draws onm_nPTargetPixmap.
            I set Qimage for Qlabel. Can I set Qpixmap on top of QImage in Qlabel and draw on Qpixmap?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #9

            @Mikeeeeee
            You can just move the line code to your MyLabel and draw directly there.
            You dont need PaintWidget, it was just to show the logic of how to handle line drawing.

            However, i do wonder if you plan to be able to move the line after they are drawn or select them again ?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by
              #10

              I would like to move the line along with QImage. But Qlabel is constantly changing its size. That's why I wanted to draw on the second layer.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mikeeeeee
                wrote on last edited by
                #11

                In this example, how do you make it possible to draw lines instead of lines, as in Paint when you click curves?

                mrjjM 1 Reply Last reply
                0
                • M Mikeeeeee

                  In this example, how do you make it possible to draw lines instead of lines, as in Paint when you click curves?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #12

                  @Mikeeeeee
                  Hi

                  • In this example, how do you make it possible to draw lines instead of lines, as in Paint when you click curves?

                  im not sure what you ask?

                  When you click Curve in paint, you draw a curve, not a straight line.
                  (it looks like a Bezier Curve, but im not sure)

                  Do you mean to draw a curve instead of a line?

                  You might be able to use
                  https://doc.qt.io/qt-5/qpainterpath.html#cubicTo
                  also some background
                  https://en.wikipedia.org/wiki/Bézier_curve#Higher-order_curves

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mikeeeeee
                    wrote on last edited by
                    #13

                    I need to like this example.
                    0_1560086598467_1.png
                    But there's a lot of code. Please help to highlight the desired code.

                    mrjjM 1 Reply Last reply
                    0
                    • M Mikeeeeee

                      I need to like this example.
                      0_1560086598467_1.png
                      But there's a lot of code. Please help to highlight the desired code.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      @Mikeeeeee
                      What you show there seems to be just random paint. ( lots of points )
                      The sample you link seems to have some curve code
                      but the image you show as an example, its just drawn following the move and it is not a curve or line.

                      So you want to make a free painting function?

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mikeeeeee
                        wrote on last edited by
                        #15

                        Yes. Please help to finish this code.

                        mrjjM 1 Reply Last reply
                        0
                        • M Mikeeeeee

                          Yes. Please help to finish this code.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

                          @Mikeeeeee
                          well if you still using code from paintWidget

                          if(m_nbMousePressed)
                          {
                          painter.drawPixmap(0, 0, m_nPTargetPixmap);
                          painter.drawLine(m_line);
                          painter.drawPoint(m_line.p2() ); // paint the single point.
                          wasPressed = true;
                          }

                          p2() is updated from
                          void PaintWidget::mouseMoveEvent(QMouseEvent *event)

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Mikeeeeee
                            wrote on last edited by
                            #17

                            Did so, still draws a line.

                            
                            void PaintWidget::paintEvent(QPaintEvent *e)
                            {
                                static bool wasPressed = false;
                                QPainter painter(this);
                            
                                if(m_nbMousePressed)
                                {
                                    //painter.drawPixmap(0, 0, m_nPTargetPixmap);
                                    //painter.drawLine(m_line);
                                    painter.drawPoint(m_line.p2() ); // paint the single point.
                                    wasPressed = true;
                                }
                                else if(wasPressed)
                                {
                                    QPainter PixmapPainter(&m_nPTargetPixmap);
                                    QPen pen(Qt::green);
                                    PixmapPainter.setPen(pen);
                                    PixmapPainter.drawLine(m_line);
                            
                                    painter.drawPixmap(0, 0, m_nPTargetPixmap);
                                    wasPressed = false;
                                }
                            }
                            

                            Maybe you need to change:

                            PixmapPainter.drawLine(m_line);
                            
                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              Mikeeeeee
                              wrote on last edited by
                              #18

                              Did so, also does not work

                              void PaintWidget::paintEvent(QPaintEvent *e)
                              {
                                  static bool wasPressed = false;
                                  QPainter painter(this);
                              
                                  if(m_nbMousePressed)
                                  {
                                      //painter.drawPixmap(0, 0, m_nPTargetPixmap);
                                      //painter.drawLine(m_line);
                                      painter.drawPoint(m_line.p2() ); // paint the single point.
                                      wasPressed = true;
                                  }
                                  else if(wasPressed)
                                  {
                                      QPainter PixmapPainter(&m_nPTargetPixmap);
                                      QPen pen(Qt::green);
                                      PixmapPainter.setPen(pen);
                                      //PixmapPainter.drawLine(m_line);
                                      PixmapPainter.drawPoint(m_line.p2());
                              
                                      painter.drawPixmap(0, 0, m_nPTargetPixmap);
                                      wasPressed = false;
                                  }
                              }
                              
                              1 Reply Last reply
                              0
                              • mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #19

                                ok. you have to debug it then :)

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  Mikeeeeee
                                  wrote on last edited by
                                  #20

                                  Found the solution here

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    Mikeeeeee
                                    wrote on last edited by Mikeeeeee
                                    #21

                                    But is it possible to set QLabel 2 QImages (one with image, the second transparent) and draw on transparent QImages?

                                    1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Offline
                                      Christian EhrlicherC Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #22

                                      No, you have to write it on your own by reimplementing the paintEvent()

                                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                      Visit the Qt Academy at https://academy.qt.io/catalog

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        Mikeeeeee
                                        wrote on last edited by
                                        #23

                                        I made this class Please tell me how to set the method set2Images()?

                                        #ifndef QLABEL2IMAGES_H
                                        #define QLABEL2IMAGES_H
                                        #include "QLabel"
                                        
                                        
                                        class QLabel2Images : public QLabel 
                                        {
                                        public:
                                            QLabel2Images();
                                            ~QLabel2Images();
                                            void set2Images(QImage &downImage, QImage &yopImage);
                                        };
                                        
                                        #endif // QLABEL2IMAGES_H
                                        
                                        
                                        1 Reply Last reply
                                        0
                                        • Christian EhrlicherC Offline
                                          Christian EhrlicherC Offline
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #24

                                          @Mikeeeeee said in How to draw and change the region (semi-transparent) on top of a QLabel or QImage?:

                                          how to set the method

                                          Don't understand what you mean with this sentence - how to call this function? How to implement the function - since you're writing the function you should know it by your own.

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          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