QGraphicsView 滚动和图像缩放/裁剪 [英] QGraphicsView scrolling and image scaling/cropping

查看:182
本文介绍了QGraphicsView 滚动和图像缩放/裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 QGraphicsView 中有一个背景图像,它总是缩放(并在必要时裁剪)到视口的大小,没有滚动条,也没有用键盘和鼠标滚动.下面的示例是我在视口中缩放和裁剪图像的操作,但我使用随机值进行从以太中拉出的裁剪.我想要一个合乎逻辑的解决方案?

I would like to have a background image in my QGraphicsView that is always scaled (and cropped if necessary) to the size of the viewport, without scrollbars and without scrolling with the keyboard and mouse. The example below is what I am doing to scale and crop an image in the viewport, but I am using random values for the cropping that are pulled out of the aether. I would like a logical solution?

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    scene = new QGraphicsScene(this);

    ui->graphicsView->resize(800, 427); 
    // MainWindow is 800x480, GraphicsView is 800x427. I want an image that
    // is the size of the graphicsView.

    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    // the graphicsView still scrolls if the image is too large, but 
    // displays no scrollbars. I would like it not to scroll (I want to 
    // add a scrolling widget into the QGraphicsScene later, on top of
    // the background image.)


    QPixmap *backgroundPixmap = new QPixmap(":/Valentino_Bar_Prague.jpg");
    QPixmap sized = backgroundPixmap->scaled(
            QSize(ui->graphicsView->width(), 
                  ui->graphicsView->height()),
            Qt::KeepAspectRatioByExpanding); // This scales the image too tall

    QImage sizedImage = QImage(sized.toImage());
    QImage sizedCroppedImage = QImage(sizedImage.copy(0,0,
       (ui->graphicsView->width() - 1.5),
       (ui->graphicsView->height() + 19))); 
    // so I try to crop using copy(), and I have to use these values
    // and I am unsure why.

    QGraphicsPixmapItem *sizedBackground = scene->addPixmap(
        QPixmap::fromImage(sizedCroppedImage));
    sizedBackground->setZValue(1);
    ui->graphicsView->setScene(this->scene);
}

我想知道一种将图像缩放和裁剪到 QGraphicsView 大小的方法,即使我调整 QGraphicsView 的大小也能正常工作.1.5 和 19 来自哪里?

I would like to know a way to scale and crop an image to the size of the QGraphicsView that will work even when I resize the QGraphicsView. Where are the 1.5 and 19 coming from?

编辑;我也尝试使用 setBackgroundBrush,但即使使用缩放/裁剪的 QImage/QPixmap,我也会得到平铺背景.

EDIT; I have also attempted to use setBackgroundBrush, but I get a tiled background, even when using the scaled/cropped QImage/QPixmap.

编辑;到目前为止,我的解决方案是覆盖 drawBackground() 以获得我想要的结果,但这仍然无法帮助我学习如何将图像大小调整为 qgraphicsview 的视口大小.任何进一步的答案将不胜感激.

EDIT; My solution thus far has been to override drawBackground() to get the result I wanted, but this still doesn't help me learn how to size an image to the viewport size of a qgraphicsview. Any further answers would be greatly appreciated.

void CustomGraphicsView::drawBackground( QPainter * painter, const QRectF & rect )
{

    qDebug() << "background rect: " << rect << endl;

    QPixmap *backgroundPixmap = new QPixmap(":/Valentino_Bar_Prague.jpg");
    QPixmap sized = backgroundPixmap->scaled(QSize(rect.width(), rect.height()), Qt::KeepAspectRatioByExpanding);

    painter->drawPixmap(rect, sized, QRect(0.0, 0.0, sized.width(), sized.height()));

}

推荐答案

你想要 sceneRect 不仅仅是 widthheight.对于调整大小的缩放,您希望将插槽连接到 sceneRectChanged 这样您就可以在场景大小发生变化时调整图像大小.

You want sceneRect not just width and height. For the scaling on resize you want to connect a slot to sceneRectChanged so you can resize the image whenever the scene changes size.

或者您可以使用覆盖的 <更改图像大小的代码>updateSceneRect,或者更好的是,只需覆盖 drawBackground.

Or you can derive a QGraphicsView with an overridden updateSceneRect that changes the image size, or better yet, just override drawBackground.

这篇关于QGraphicsView 滚动和图像缩放/裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆