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. cropping image on QLabel using mouseEvent
Forum Updated to NodeBB v4.3 + New Features

cropping image on QLabel using mouseEvent

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 2.3k 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.
  • D Offline
    D Offline
    Dimple
    wrote on last edited by VRonin
    #1

    Hi ,,,i need to crop image displayed on label after dragging ,image is disappearing from the QLabel,, can anybody pls help me to fix this issue ??
    i added this code

    
    void MainWindow::mousePressEvent(QMouseEvent *ev)
    {
    if(ui->label->underMouse())
    {
       //qDebug()<<"Entered Press";
        origin = ev->pos();
    
        if (!rubberBand)
            rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    
            rubberBand->show();
    
    }
    }
    
    void MainWindow::mouseMoveEvent(QMouseEvent *ev)
    {
    
    rubberBand->setGeometry(QRect(origin, ev->pos()).normalized());
    }
    
    void MainWindow::mouseReleaseEvent(QMouseEvent *ev)
    {
    QPoint a = mapToGlobal(origin);
    QPoint b = ev->globalPos();
    
    a = ui->label->mapFromGlobal(a);
    b = ui->label->mapFromGlobal(b);
    rubberBand->hide();
    QPixmap OriginalPix(*ui->label->pixmap());
    double sx = ui->label->rect().width();
    double sy = ui->label->rect().height();
    sx = OriginalPix.width() / sx;
    sy = OriginalPix.height() / sy;
    
    a.setX(int(a.x() * sx));
    b.setX(int(b.x() * sx));
    a.setX(int(a.x() * sy));
    b.setX(int(b.x() * sy));
    
    
    QRect myRect(a,b);
    
    QImage newImage;
    newImage = OriginalPix.toImage();
    
    QImage copyImage;
    copyImage = copyImage.copy(myRect);
    
    ui->label->setPixmap(QPixmap::fromImage(copyImage));
    ui->label->repaint();
    
    }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      QLabel is the wrong instrument for the job. have a look at this example: https://evileg.com/en/post/272/

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

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

        Hi
        I think you used the wrong image by accident

        you say

        QImage newImage;
        newImage = OriginalPix.toImage();
        QImage copyImage;
        copyImage = copyImage.copy(myRect); << copy empty

        i think you meant.

        QImage newImage;
        newImage = OriginalPix.toImage();
        QImage copyImage;
        copyImage = newImage.copy(myRect); << copy from newImage

        1 Reply Last reply
        1
        • D Offline
          D Offline
          Dimple
          wrote on last edited by Dimple
          #4

          @mjjj as u said i hv changed
          copyImage=newImage.copy(myRect);

          but here wat happens after dragged image,, its showing QLabel is completely black

          mrjjM 1 Reply Last reply
          0
          • D Dimple

            @mjjj as u said i hv changed
            copyImage=newImage.copy(myRect);

            but here wat happens after dragged image,, its showing QLabel is completely black

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

            @Dimple
            you mean when you drag the image to be cropped to the label ?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dimple
              wrote on last edited by
              #6

              yeah ,,after dragged the image ,,its showing QLabel is completely black (not shows image after cropped the image)

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dimple
                wrote on last edited by Dimple
                #7

                0_1545661635737_img2.PNG

                0_1545661348348_img.PNG

                mrjjM 1 Reply Last reply
                0
                • D Dimple

                  0_1545661635737_img2.PNG

                  0_1545661348348_img.PNG

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

                  @Dimple
                  I think the mapping of the coordinates are a bit off so the rect copied is outside
                  the coordinates for label holding image.

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    Dimple
                    wrote on last edited by
                    #9

                    can u please send me correct code ,,,im not getting

                    mrjjM 1 Reply Last reply
                    0
                    • D Dimple

                      can u please send me correct code ,,,im not getting

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

                      @Dimple
                      the calculations for the actual rect to copy

                      a = ui->label->mapFromGlobal(a);
                      b = ui->label->mapFromGlobal(b);
                      
                      double sx = ui->label->rect().width();
                      double sy = ui->label->rect().height();
                      
                      sx = OriginalPix.width() / sx;
                      sy = OriginalPix.height() / sy;
                      
                      a.setX(int(a.x() * sx));
                      b.setX(int(b.x() * sx));
                      a.setX(int(a.x() * sy));
                      b.setX(int(b.x() * sy));
                      
                      QRect myRect(a, b); // im not sure this rect is as intended.
                      

                      try use qDebug to write it out and inspect the values.
                      like
                      qDebug() << " crop rect" << myRect;

                      F 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @Dimple
                        the calculations for the actual rect to copy

                        a = ui->label->mapFromGlobal(a);
                        b = ui->label->mapFromGlobal(b);
                        
                        double sx = ui->label->rect().width();
                        double sy = ui->label->rect().height();
                        
                        sx = OriginalPix.width() / sx;
                        sy = OriginalPix.height() / sy;
                        
                        a.setX(int(a.x() * sx));
                        b.setX(int(b.x() * sx));
                        a.setX(int(a.x() * sy));
                        b.setX(int(b.x() * sy));
                        
                        QRect myRect(a, b); // im not sure this rect is as intended.
                        

                        try use qDebug to write it out and inspect the values.
                        like
                        qDebug() << " crop rect" << myRect;

                        F Offline
                        F Offline
                        fouad2130
                        Banned
                        wrote on last edited by fouad2130
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Dimple
                          wrote on last edited by
                          #12

                          @mrjj

                          a.setX(int(a.x() * sx));
                          b.setX(int(b.x() * sx));
                          a.setY(int(a.y() * sy));
                          b.setY(int(b.y() * sy));

                          QRect myRect(a,b);

                          QImage newImage;
                          newImage = OriginalPix.toImage();
                          QImage copyImage;
                          copyImage=newImage.copy(myRect);
                          ui->label->setPixmap(QPixmap::fromImage(copyImage));
                          ui->label->setScaledContents(true);
                          ui->label->repaint();

                          now its working ,,i can crop the image but after cropped the image ,,,if i click on cropped image ,,image is disappearing?

                          mrjjM 1 Reply Last reply
                          0
                          • D Dimple

                            @mrjj

                            a.setX(int(a.x() * sx));
                            b.setX(int(b.x() * sx));
                            a.setY(int(a.y() * sy));
                            b.setY(int(b.y() * sy));

                            QRect myRect(a,b);

                            QImage newImage;
                            newImage = OriginalPix.toImage();
                            QImage copyImage;
                            copyImage=newImage.copy(myRect);
                            ui->label->setPixmap(QPixmap::fromImage(copyImage));
                            ui->label->setScaledContents(true);
                            ui->label->repaint();

                            now its working ,,i can crop the image but after cropped the image ,,,if i click on cropped image ,,image is disappearing?

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

                            @Dimple
                            Hi
                            clicking the label triggers mousePressEvent and mouseReleaseEvent
                            so you will grab a very small /invalid area. ( thats my guess )

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              Dimple
                              wrote on last edited by
                              #14

                              @mrjj said in cropping image on QLabel using mouseEvent:

                              triggers

                              ok i ll change some functionality ,,Thanks for ur help

                              mrjjM 1 Reply Last reply
                              0
                              • D Dimple

                                @mrjj said in cropping image on QLabel using mouseEvent:

                                triggers

                                ok i ll change some functionality ,,Thanks for ur help

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

                                @Dimple
                                Hi
                                i imagine a bool flag to tell it its in crop mode , else it will just ignore the mosuePress code etc.

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  Dimple
                                  wrote on last edited by
                                  #16

                                  @mrjj

                                  ok i ll check it out,,,thanks

                                  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