5

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

}
4

3 に答える 3

1

QGraphicsView::fitInViewまさにこれを行います。ドキュメントによると、一般的にresizeEvent. を使用sceneRectsすると、シーン全体がビューに収まります。

void CustomGraphicsView::resizeEvent(QResizeEvent *)
{
  this->fitInView(this->sceneRect());
}
于 2016-11-05T10:10:59.730 に答える
1

sceneRectとだけwidthではありませんheight。サイズ変更のスケーリングでは、スロットを接続してsceneRectChanged、シーンのサイズが変わるたびに画像のサイズを変更できるようにします。

または、画像サイズを変更QGraphicsViewするオーバーライドを使用してを派生させることもできます。updateSceneRectdrawBackground

于 2010-03-22T16:50:48.510 に答える
0

ui->graphicsView->viewport()->size()ビューポートのサイズを取得することがわかりました。ただし、ウィジェットが描画された後にのみ機能します。

于 2010-07-30T08:41:20.500 に答える