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

后端 未结 4 2109
挽巷
挽巷 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条回答
  •  萌比男神i
    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();
    

提交回复
热议问题