내 QGraphicsView에서 스크롤바가없고 키보드와 마우스로 스크롤하지 않고도 항상 크기 조정 (및 필요한 경우 잘림)되는 배경 이미지를 갖고 싶습니다. 아래 예는 뷰포트에서 이미지의 크기를 조절하고 자르기 위해 수행하는 작업이지만, 오더에서 추출한 자르기에 임의의 값을 사용하고 있습니다. 논리적 인 해결책을 원하십니까?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()));
}
문서에서 :
sceneRects
을 사용하면보기에 전체 장면 적합하게 스크롤 막대. " sceneRect는 내가 원하는 것이 아니며 뷰포트 크기에 관계없이 장면의 크기를 제공합니다. 뷰포트 크기를 원합니다. 나는 그 크기로 이미지의 크기를 정하고 싶다. 이것은 단순 해 보인다. qgraphicsview의 너비/높이를 잡고 작업을 수행해야합니다. 그러나 이미지를 그 크기로 확대 할 때 너비와 높이가 올바르지 않습니다. 여기서 누락 된 부분은 무엇입니까? 나는 drawBackground를 시도 할 것이다. – user298725