我正在开发向用户显示他选择的一些图片的程序。但是有一个问题,因为我想把这张图片放在QGraphicsView的框架中,而图片确实比框架小。
所以这是我的代码:
image = new QImage(data.absoluteFilePath()); // variable data is defined when calling this method
scn = new QGraphicsScene(this); // object defined in header
ui->graphicsView->setScene(scn);
scn->addPixmap(QPixmap::fromImage(*image));
ui->graphicsView->fitInView(scn->itemsBoundingRect(),Qt::KeepAspectRatio);
我尝试了很多我在网上找到的解决方案,但没有人不帮助我。当框架为 200 x 400 像素时,图片的大小约为 40 x 60 像素。有什么问题?
下面是一些使用上面的代码生成的示例以及我想要得到的示例:
原文由 user1257255 发布,翻译遵循 CC BY-SA 4.0 许可协议
我的问题的解决方案是 Dialog 的 showEvent()。这意味着您不能在显示表单之前调用 fitInView(),因此您必须为对话框创建 showEvent() 并且图片将适合 QGraphics View 的框架。
以及您必须添加到对话框代码中的示例代码: