【问题标题】:QGraphicsView scrolling and image scaling/croppingQGraphicsView 滚动和图像缩放/裁剪
【发布时间】:2011-01-30 04:35:53
【问题描述】:

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

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 是从哪里来的?

编辑;我也尝试过使用 setBackgroundBrush,但我得到了一个平铺的背景,即使使用缩放/裁剪的 QImage/QPixmap。

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

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()));

}

【问题讨论】:

    标签: qt scale qgraphicsview qimage qpixmap


    【解决方案1】:

    您需要sceneRect 而不仅仅是widthheight。对于调整大小的缩放,您希望将一个插槽连接到sceneRectChanged,这样您就可以在场景改变大小时调整图像大小。

    或者您可以派生一个 QGraphicsView 并使用覆盖的 updateSceneRect 来更改图像大小,或者更好的是,只需覆盖 drawBackground

    【讨论】:

    • 来自文档:“场景矩形定义了场景的范围,在视图的情况下,这意味着您可以使用滚动条导航的场景区域。” sceneRect 不是我想要的,它给了我场景的大小,不管视口尺寸是多少。我想要视口尺寸。我想将图像调整到这些尺寸。这看起来很简单;抓住 qgraphicsview 的宽度/高度,工作应该完成。但是当我将图像缩放到该尺寸时,宽度和高度不正确:这里缺少的部分是什么?我会试试drawBackground。
    【解决方案2】:

    我找到ui-&gt;graphicsView-&gt;viewport()-&gt;size() 来获取视口的大小。仅在绘制小部件后才有效。

    【讨论】:

      【解决方案3】:

      QGraphicsView::fitInView 正是这样做的。根据文档,它通常放在resizeEvent 中。使用sceneRects 使整个场景适合视图:

      void CustomGraphicsView::resizeEvent(QResizeEvent *)
      {
        this->fitInView(this->sceneRect());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-18
        • 1970-01-01
        • 2012-07-21
        • 2015-04-20
        • 1970-01-01
        • 2011-11-16
        • 2016-08-29
        • 1970-01-01
        相关资源
        最近更新 更多