سؤال

أرغب في الحصول على صورة خلفية في 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؟

تعديل؛ لقد حاولت أيضًا استخدام الانتكاسات ، لكنني أحصل على خلفية مبلطة ، حتى عند استخدام Qimage/qpixmap المحجوز/المحصور.

تعديل؛ كان الحل الخاص بي حتى الآن هو تجاوز العيب () للحصول على النتيجة التي أردتها ، لكن هذا لا يزال لا يساعدني في تعلم كيفية حجم صورة إلى حجم منفذ العرض الخاص بـ 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 ليس مجرد width و height. للتوسع في تغيير الحجم الذي تريد توصيل فتحة 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