簡體   English   中英

QRubberBand在Qt中裁剪圖像

[英]QRubberBand to crop image in Qt

我希望能夠使用QRubberBand選擇圖像區域,然后在裁剪后將新選擇的區域保存在新位置。

我找到了這個答案,但我需要知道mapFormGlobal(a)(b)ab是什么

void MainResizeWindow::mousePressEvent(QMouseEvent *event)
{
    if(ui->imageLabel->underMouse()){
        myPoint = event->pos();
        rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
        rubberBand->show();
    }
}

void MainResizeWindow::mouseMoveEvent(QMouseEvent *event)
{
    rubberBand->setGeometry(QRect(myPoint, event->pos()).normalized());
}

void MainResizeWindow::mouseReleaseEvent(QMouseEvent *event)
{
    QPixmap OriginalPix(*ui->imageLabel->pixmap());
    double sx = ui->imageLabel->rect().width();
    double sy = ui->imageLabel->rect().height();
    sx = OriginalPix.width() / sx;
    sy = OriginalPix.height() / sy;

    QPoint a = mapToGlobal(myPoint);
    QPoint b = event->globalPos();

    a = ui->imageLabel->mapFromGlobal(a);
    b = ui->imageLabel->mapFromGlobal(b);

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

    QRect myRect(a, b);

 //  QPixmap OriginalPix(*ui->imageLabel->pixmap());

    QImage newImage;
    newImage = OriginalPix.toImage();


    QImage copyImage;
    copyImage = copyImage.copy(myRect);

    ui->imageLabel->setPixmap(QPixmap::fromImage(copyImage));
    ui->imageLabel->repaint();
}

我得到錯誤

error: 'a' was not declared in this scope
     a = ui->imageLabel->mapFromGlobal(a);
     ^

應該在mainresizewindow.h聲明它的mainresizewindow.h或正確的方法是什么

如下重寫mouseReleaseEvent方法:

void MainResizeWindow::mouseReleaseEvent(QMouseEvent *event)
{
    QPixmap OriginalPix(ui->imageLabel->pixmap());
    QImage newImage;
    newImage = OriginalPix.toImage();
    QImage copyImage;
    copyImage = newImage.copy(rubberBand->geometry());
    ui->imageLabel->setPixmap(QPixmap::fromImage(copyImage));
    ui->imageLabel->repaint();
}

釋放鼠標按鈕后,選擇的圖像將被裁剪並顯示在標簽中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM