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 14.6k 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
    #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 Online
            Christian EhrlicherC Online
            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 Online
                Christian EhrlicherC Online
                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
                • M Offline
                  M Offline
                  Mikeeeeee
                  wrote on last edited by
                  #25

                  I don't know how to implement it. Need one figure to place from below, the second figure(transparent) to place from above.

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

                    When the second image is (mostly) transparent then simply paint the first and then the second.

                    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
                    1
                    • M Offline
                      M Offline
                      Mikeeeeee
                      wrote on last edited by
                      #27

                      The first is easy to add by setImage(), but how to add the second?

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

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

                        The first is easy to add by setImage(), but how to add the second?

                        No, you have to write it on your own by reimplementing the paintEvent()
                        When the second image is (mostly) transparent then simply paint the first and then the second.

                        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
                        1
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #29

                          Hi
                          Well you already had most of the code in the first version

                           virtual void paintEvent(QPaintEvent* e) {
                              QLabel::paintEvent(e); // paint the base image
                              
                              QPainter p(this);    
                              p.drawQImage(0, 0, OtherQImage); // draw the other image
                            }
                          };
                          

                          so your void set2Images(QImage &downImage, QImage &yopImage);
                          so set one image to the normal setPixmap for label and store the other one (the overlay) in a new variable
                          which you then use to draw it. ( like OtherQImage)

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #30

                            Hi,

                            An alternative would be to compose the two images before setting the result on the QLabel.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            mrjjM 1 Reply Last reply
                            1
                            • SGaistS SGaist

                              Hi,

                              An alternative would be to compose the two images before setting the result on the QLabel.

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

                              @SGaist
                              That's actually a better idea as the base image will be scaled with a factor
                              so combining them first would
                              make the zooming easier to handle.

                                QImage base; // set to some file/size
                                QImage overlay;  // set to some file/size
                                QPainter paint(&base);
                                paint.drawImage(0,0,overlay);
                                ui->label->setPixmap(QPixmap::fromImage(base));
                              
                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                Mikeeeeee
                                wrote on last edited by
                                #32

                                Many thanks. That's how it works:

                                    QImage mapImage(":/Images/Images/mapMain.png");
                                    myImageViewer->setImage(mapImage);    
                                    QImage base(":/Images/Images/mapMain.png"); // set to some file/size
                                    QImage overlay(":/Images/Images/mapTop.png");  // set to some file/size
                                    QPainter paint(&base);
                                    paint.drawImage(0,0,overlay);
                                    myImageViewer->imageLabel->setPixmap(QPixmap::fromImage(base));
                                    ui->verticalLayout->addWidget(myImageViewer);
                                
                                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