cropping image on QLabel using mouseEvent
-
wrote on 24 Dec 2018, 09:38 last edited by VRonin
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 codevoid 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(); }
-
wrote on 24 Dec 2018, 10:14 last edited by
QLabel
is the wrong instrument for the job. have a look at this example: https://evileg.com/en/post/272/ -
Hi
I think you used the wrong image by accidentyou say
QImage newImage;
newImage = OriginalPix.toImage();
QImage copyImage;
copyImage = copyImage.copy(myRect); << copy emptyi think you meant.
QImage newImage;
newImage = OriginalPix.toImage();
QImage copyImage;
copyImage = newImage.copy(myRect); << copy from newImage -
wrote on 24 Dec 2018, 14:07 last edited by 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
-
@mjjj as u said i hv changed
copyImage=newImage.copy(myRect);but here wat happens after dragged image,, its showing QLabel is completely black
@Dimple
you mean when you drag the image to be cropped to the label ? -
wrote on 24 Dec 2018, 14:20 last edited by
yeah ,,after dragged the image ,,its showing QLabel is completely black (not shows image after cropped the image)
-
wrote on 24 Dec 2018, 14:24 last edited by Dimple
-
@Dimple
I think the mapping of the coordinates are a bit off so the rect copied is outside
the coordinates for label holding image. -
wrote on 24 Dec 2018, 14:40 last edited by
can u please send me correct code ,,,im not getting
-
@Dimple
the calculations for the actual rect to copya = 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; -
@Dimple
the calculations for the actual rect to copya = 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;This post is deleted! -
wrote on 26 Dec 2018, 11:37 last edited by
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?
-
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?
@Dimple
Hi
clicking the label triggers mousePressEvent and mouseReleaseEvent
so you will grab a very small /invalid area. ( thats my guess ) -
wrote on 27 Dec 2018, 07:05 last edited by
@mrjj said in cropping image on QLabel using mouseEvent:
triggers
ok i ll change some functionality ,,Thanks for ur help
-
@mrjj said in cropping image on QLabel using mouseEvent:
triggers
ok i ll change some functionality ,,Thanks for ur help
@Dimple
Hi
i imagine a bool flag to tell it its in crop mode , else it will just ignore the mosuePress code etc.
2/16