如何在QT的QgraphicsView中保持图像原始

问题描述 投票:0回答:1

我已经在QGraphicsView中加载了图像,但是考虑到QGraphicsView场景的大小,我需要先缩放图像,然后将缩放后的图像添加到场景中。例如图像原始尺寸为1536 * 1024,QGraphicsView尺寸为500 * 400,首先我像这样缩放图像:

image2D = image2D.scaled( h * P , h, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
myScene->setSceneRect((h * P-w)/2, 0, h * P , h);
pixmapItem = new QGraphicsPixmapItem(image2D);
myScene->addItem(pixmapItem);
myView->setScene(myScene);

现在出现了一个问题,当wheelEvent发生并且QGraphicsView放大时,缩放的图像变得模糊不清,而我希望它保持与原始图像一样清晰。我找到一种方法来保存图像的原始副本,然后在wheelEvent发生时,只需缩放原始副本并将其放到场景中即可。但是我不知道怎么写这段代码,谢谢帮忙〜还是有任何简单的方法?

class interactiveView : public QGraphicsView
{
protected:
    void wheelEvent(QWheelEvent *event) override;
}
void interactiveView::wheelEvent(QWheelEvent *event)
{
    int scrollAmount = event->delta();

    xPos = event->pos().x();
    yPos = event->pos().y();

    scrollAmount > 0 ? zoomIn() : zoomOut();
}

缩放图像:

“缩放后的图像”

放大时变得模糊:

“在放大时变得不明显”

image qt scale mousewheel qgraphicsview
1个回答
0
投票

您可以使用此方法resetMatrix()重设图像

例如

graphicsView->scale(2, 2);
graphicsView->resetMatrix();
graphicsView->scale(1, 1);
© www.soinside.com 2019 - 2024. All rights reserved.