Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QGraphicsView滚动和图像缩放/裁剪_Qt_Scale_Qgraphicsview_Qimage_Qpixmap - Fatal编程技术网

Qt QGraphicsView滚动和图像缩放/裁剪

Qt QGraphicsView滚动和图像缩放/裁剪,qt,scale,qgraphicsview,qimage,qpixmap,Qt,Scale,Qgraphicsview,Qimage,Qpixmap,我希望在我的QGraphicsView中有一个背景图像,它总是按视口的大小缩放(必要时裁剪),没有滚动条,也没有使用键盘和鼠标滚动。下面的示例是我在视口中缩放和裁剪图像所做的操作,但我使用随机值进行裁剪,这些裁剪是从以太中拉出的。我想要一个合乎逻辑的解决方案 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this

我希望在我的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时也是如此

编辑;到目前为止,我的解决方案是覆盖traffackground()以获得我想要的结果,但这仍然无法帮助我了解如何将图像大小调整为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()));

}
void CustomGraphicsView::退税地(QPainter*painter,const qrect&rect)
{
qDebug()您不仅需要
宽度
高度
。对于调整大小时的缩放,您需要连接一个插槽,以便在场景大小更改时调整图像大小


或者,您可以派生一个覆盖了更改图像大小的
QGraphicsView
,或者更好的是,只需覆盖即可。

我发现
ui->graphicsView->viewport()->size()
获取视口大小。仅在绘制小部件后才起作用。

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

void CustomGraphicsView::resizeEvent(QResizeEvent *)
{
  this->fitInView(this->sceneRect());
}
从文档中可以看出:“场景矩形定义了场景的范围,在视图中,这意味着可以使用滚动条导航的场景区域。”SCENERTISTER不是我想要的,它为我提供了场景的大小,而不管视口的尺寸是什么。我想要视口的尺寸。我想要根据这些尺寸来调整图像的大小。这似乎很简单;抓住qgraphicsview的宽度/高度,然后就应该完成工作。但是,当我将图像缩放到尺寸:这里缺少什么?我会试试牵引地面。