質問

私は、スクロールバーがなく、キーボードやマウスでスクロールせずに、ビューポートのサイズに常に拡大縮小(および必要に応じてトリミング)された私の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から来ているか。

EDIT。私はまた、使用setBackgroundBrushしようと試みてきたが、私はタイル張りの背景を取得し、拡大縮小/トリミングされたQImageの/ QPixmapの。

を使用した場合でも、

EDIT。私のソリューションは、これまで私が望んでいた結果を得るために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()));

}
役に立ちましたか?

解決

あなたは sceneRect にだけではなくwidthheightをしたいです。リサイズのスケーリングのためにあなたができるよう、 sceneRectChangedするにスロットを接続したいですシーンのサイズを変更するたびに画像のサイズを変更します。

それとも、変更、オーバーライド QGraphicsViewするupdateSceneRectを導き出すことができます画像サイズ、またはより良いまだ、単に上書き drawBackgroundするます。

他のヒント

私は、ビューポートのサイズを取得するためにui->graphicsView->viewport()->size()を発見しました。ウィジェットのけれども描かれた後。

のみ動作します

QGraphicsView::fitInViewは、まさにこれを行います。文書によると、それは一般的にresizeEventに入れています。

sceneRectsを使用すると、ビューにシーン全体のフィット感を作ります
void CustomGraphicsView::resizeEvent(QResizeEvent *)
{
  this->fitInView(this->sceneRect());
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top