Qt5 C++ QGraphicsView: Images don't fit view frame

后端 未结 4 2095
挽巷
挽巷 2020-12-18 21:31

I\'m working on program which shows user some picture that is selected by him. But there is a problem because I would like to fit this picture in QGraphicsView\'s frame and

相关标签:
4条回答
  • 2020-12-18 21:48

    I think you should scale image. I do this and it works well:

    QRect ref_Rect = QRect(x_pos, y_pos, Width, Length);
    QGraphicsView* qGraph = new QGraphicsView(this);
    qGraph->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    qGraph->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    qGraph->setGeometry(ref_Rect);
    
    QGraphicsScene* scene = new QGraphicsScene(qGraph);
    scene->setSceneRect(0, 0, ref_Rect.width(), ref_Rect.height());
    qGraph->setScene(scene);
    
    QImage *image = new QImage();
    image->load("folder/name.png");
    *image = image->scaled(ref_Rect.width(), ref_Rect.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); 
    QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(*image));    
    
    scene->addItem(item);
    qGraph->show();
    
    0 讨论(0)
  • 2020-12-18 21:55

    Solution for my question is showEvent() for Dialog. This means that you can't call fitInView() before the form is showed, so you have to create showEvent() for dialog and the picture will be fitted into QGraphics View's frame.

    And example code which you have to add into dialog's code:

    void YourClass::showEvent(QShowEvent *) {
        ui->graphicsView->fitInView(scn->sceneRect(),Qt::KeepAspectRatio);
    }
    
    0 讨论(0)
  • 2020-12-18 21:55

    You should handle resize event, I guess it's the way it's meant to be played:

    bool YourDialog::eventFilter(QObject *obj, QEvent *event)
    {
            if (event->type() == QEvent::Show){
                ui->conceptView->fitInView(conceptScene->sceneRect(), Qt::KeepAspectRatio);
            }
    
            if (event->type() == QEvent::Resize){
                ui->conceptView->fitInView(conceptScene->sceneRect(), Qt::KeepAspectRatio);
            }
    }
    
    0 讨论(0)
  • 2020-12-18 21:59

    The reason you're not seeing your image as you want it is because the QGraphicsView function fitInView does not do what you think it does.

    It ensures that the object fits inside the viewport, without any overlap of the borders of the view, so if your object was not in the view, calling fitInView will cause the view to move / scale etc to ensure that the object is completely visible. Also, if the viewport is too small for the area provided to fitInView, nothing will happen.

    So, to get what you want, map the extents of the GraphicsView coordinates to the GraphicsScene and then set the image's scene coordinates to those. As @VBB said, if you stretch the image, it may change the aspect raio, so you can use scaledToWidth on the QPixmap.

    Something like this: -

    QRectF sceneRect = ui->graphicsView->sceneRect(); // the view's scene coords
    QPixmap image = QPixmap::fromImage(*image);
    
    // scale the image to the view and maintain aspect ratio
    image = image.scaledToWidth(sceneRect.width());
    
    QGraphicsPixmapItem* pPixmap = scn->addPixmap(QPixmap::fromImage(*image));
    
    // overloaded function takes the object and we've already handled the aspect ratio
    ui->graphicsView->fitInView(pPixmap);
    

    You may find that you don't need the call to fitInView, if your viewport is in the right place and if you don't want it to look pixellated, use an image with a high resolution.

    0 讨论(0)
提交回复
热议问题