use*_*255 15 c++ qt image qgraphicsview
我正在制作程序,向用户显示由他选择的一些图片.但是有一个问题,因为我想在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);
Run Code Online (Sandbox Code Playgroud)
我尝试了很多我在网上找到的解决方案,但没有人帮助我.当帧为200 x 400像素时,图片大小约为40 x 60像素.可能有什么不对?
以下是使用上面的代码生成的内容以及我想要获得的内容的一些示例:

use*_*255 21
我的问题的解决方案是Dialog的showEvent().这意味着您无法在显示表单之前调用fitInView(),因此您必须为对话框创建showEvent(),并且图片将适合QGraphics View的框架.
以及必须添加到对话框代码中的示例代码:
void YourClass::showEvent(QShowEvent *) {
ui->graphicsView->fitInView(scn->sceneRect(),Qt::KeepAspectRatio);
}
Run Code Online (Sandbox Code Playgroud)