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 over an old picture in QGraphicsScene

How to draw over an old picture in QGraphicsScene

Scheduled Pinned Locked Moved Unsolved General and Desktop
37 Posts 4 Posters 5.7k 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
    #24

    I append 2 images on QLabel

        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);
    
        //Pen
        myPen.setWidth(20);
        myPen.setColor(Qt::green);
        //myPen.setColor(QColor(255, 0, 0, 10));
        myPen.setCapStyle(Qt::RoundCap);
        myPen.setStyle(Qt::SolidLine);
        myImageViewer->installEventFilter(this);
    

    In the Mainwindow constructor, I wrote:

        QPixmap myPixmap;
        QPen myPen;
        QPoint lastPoint;
        bool eventFilter(QObject *obj, QEvent *event);
    

    And wrote such eventFilter:

    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
        qDebug()<<"eventFilter";
        if(obj == myImageViewer and event->type() == QEvent::MouseButtonPress)
        {
            qDebug()<<" QEvent::MouseButtonPress";
            QMouseEvent *mous = (QMouseEvent*)event;
            QPainter p(&myPixmap);
            p.setPen(myPen);
            p.drawPoint(mous->pos());
            p.end();
            lastPoint = mous->pos();
            myImageViewer->imageLabel->setPixmap(myPixmap);
        }
        if(obj == myImageViewer and event->type() == QEvent::MouseMove)
        {
            QMouseEvent *mous = (QMouseEvent*)event;
            QPainter p(&myPixmap);
            p.setPen(myPen);
            p.drawLine(lastPoint,mous->pos());
            p.end();
            lastPoint = mous->pos();
            myImageViewer->imageLabel->setPixmap(myPixmap);
        }
    }
    

    When eventfilter is triggered, the picture disappears and a white screen appears. And Qt gives an error message:

    QEvent::MouseButtonPress
    QPainter::begin: Paint device returned engine == 0, type: 2
    QPainter::setPen: Painter not active
    QPainter::drawPoints: Painter not active
    QPainter::end: Painter not active, aborted

    jsulmJ 1 Reply Last reply
    0
    • M Mikeeeeee

      I append 2 images on QLabel

          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);
      
          //Pen
          myPen.setWidth(20);
          myPen.setColor(Qt::green);
          //myPen.setColor(QColor(255, 0, 0, 10));
          myPen.setCapStyle(Qt::RoundCap);
          myPen.setStyle(Qt::SolidLine);
          myImageViewer->installEventFilter(this);
      

      In the Mainwindow constructor, I wrote:

          QPixmap myPixmap;
          QPen myPen;
          QPoint lastPoint;
          bool eventFilter(QObject *obj, QEvent *event);
      

      And wrote such eventFilter:

      bool MainWindow::eventFilter(QObject *obj, QEvent *event)
      {
          qDebug()<<"eventFilter";
          if(obj == myImageViewer and event->type() == QEvent::MouseButtonPress)
          {
              qDebug()<<" QEvent::MouseButtonPress";
              QMouseEvent *mous = (QMouseEvent*)event;
              QPainter p(&myPixmap);
              p.setPen(myPen);
              p.drawPoint(mous->pos());
              p.end();
              lastPoint = mous->pos();
              myImageViewer->imageLabel->setPixmap(myPixmap);
          }
          if(obj == myImageViewer and event->type() == QEvent::MouseMove)
          {
              QMouseEvent *mous = (QMouseEvent*)event;
              QPainter p(&myPixmap);
              p.setPen(myPen);
              p.drawLine(lastPoint,mous->pos());
              p.end();
              lastPoint = mous->pos();
              myImageViewer->imageLabel->setPixmap(myPixmap);
          }
      }
      

      When eventfilter is triggered, the picture disappears and a white screen appears. And Qt gives an error message:

      QEvent::MouseButtonPress
      QPainter::begin: Paint device returned engine == 0, type: 2
      QPainter::setPen: Painter not active
      QPainter::drawPoints: Painter not active
      QPainter::end: Painter not active, aborted

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #25

      @Mikeeeeee Why do you paint in eventFilter?!
      You already were told to paint in paintEvent.
      In eventFilter you only store needed information (like x/y).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        But in the example that I showed you drawing eventfilter occurs, and if you do so, then paintEvent works only a couple of times and then for some reason no longer works.

        void MainWindow::paintEvent(QPaintEvent *event)
        {
            qDebug()<<"paintEvent";
        }
        
        jsulmJ 1 Reply Last reply
        0
        • M Mikeeeeee

          But in the example that I showed you drawing eventfilter occurs, and if you do so, then paintEvent works only a couple of times and then for some reason no longer works.

          void MainWindow::paintEvent(QPaintEvent *event)
          {
              qDebug()<<"paintEvent";
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #27

          @Mikeeeeee After getting new coordinates you can call https://doc.qt.io/qt-5/qwidget.html#update to trigger paint event.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • M Offline
            M Offline
            Mikeeeeee
            wrote on last edited by
            #28

            It turns out to draw in eventFilter, but it was necessary to add this line to the constructor.

            myPixmap  = QPixmap(myImageViewer->image.width(),myImageViewer->image.height());
            

            I draw so:

            bool MainWindow::eventFilter(QObject *obj, QEvent *event)
            {
                if(obj == myImageViewer and event->type() == QEvent::MouseButtonPress)
                {
                    qDebug()<<" QEvent::MouseButtonPress";
                    QMouseEvent *mous = (QMouseEvent*)event;
                    QPainter p(&myPixmap);
                    p.setPen(myPen);
                    p.drawPoint(mous->pos());
                    p.end();
                    lastPoint = mous->pos();
                    myImageViewer->imageLabel->setPixmap(myPixmap);
                }
            }
            
            

            But when drawing the picture disappears and the screen turns black. How to fix? Can I set the drawing on the QImage overlay(":/Images/Images/mapTop.png");?

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

              And if I do so, the screen becomes white.

              myPixmap.fill();
              

              How to make myPixmap transparent?

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

                myPixmap.fill(Qt::transparent);

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

                  Thanks. Do so:

                      myPixmap  = QPixmap(myImageViewer->image.width(),myImageViewer->image.height());
                      myPixmap.fill(Qt::transparent);
                      myPixmap = QPixmap(":/Images/Images/mapTop.png");
                  

                  But after drawing the picture disappears :

                  myImageViewer->setImage(mapImage);
                  

                  How to get myPixmap the images:":/Images/Images/mapMain.png" and ":/Images/Images/mapTop.png" , but draw only on:
                  ":/Images/Images/mapTop.png"?

                  mrjjM 1 Reply Last reply
                  0
                  • M Mikeeeeee

                    Thanks. Do so:

                        myPixmap  = QPixmap(myImageViewer->image.width(),myImageViewer->image.height());
                        myPixmap.fill(Qt::transparent);
                        myPixmap = QPixmap(":/Images/Images/mapTop.png");
                    

                    But after drawing the picture disappears :

                    myImageViewer->setImage(mapImage);
                    

                    How to get myPixmap the images:":/Images/Images/mapMain.png" and ":/Images/Images/mapTop.png" , but draw only on:
                    ":/Images/Images/mapTop.png"?

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

                    @Mikeeeeee
                    You mean draw myPixmap on mapImage and then
                    show the combined image with myImageViewer->setImage(mapImage); ?
                    Thats like before where we combined them.

                    1 Reply Last reply
                    1
                    • M Offline
                      M Offline
                      Mikeeeeee
                      wrote on last edited by
                      #33
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mikeeeeee
                        wrote on last edited by
                        #34

                        Yes.The background will be mapImage, and I want to draw on it, with the ability to erase and make transparent Qpixmap. You may need to use: void QPixmap::setMask(const Bitmap &mask)

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

                          Tried to do so, but got an error: reference to type 'const QBitmap' could not bind to an lvalue of type 'QBitmap *'

                              QBitmap *myQBitmap = new QBitmap();
                              myQBitmap->fromImage(mapImage);
                              myPixmap.setMask(myQBitmap);
                          
                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #36

                            @Mikeeeeee Please, read the documentation.

                            QBitmap::fromImage is a static method. You're using it wrong.
                            QPixmap::setMask takes a const reference to a QBitmap, not a pointer.

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

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

                              But how to set 2 images and draw on top image?

                              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