我正在开发一个程序,该程序向用户显示他选择的某些图片。但是有一个问题,因为我想将此图片适合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);

我尝试了很多在Web上找到的解决方案,但是没有人帮不上忙。当帧为200 x 400像素时,图片的尺寸约为40 x 60像素。有什么事吗

以下是上面代码产生的一些示例以及我想了解的内容:

最佳答案

我的问题的解决方案是Dialog的showEvent()。这意味着您不能在显示表单之前调用fitInView(),因此必须为对话框创建showEvent(),然后图片将适合QGraphics View的框架。

以及必须添加到对话框代码中的示例代码:

void YourClass::showEvent(QShowEvent *) {
    ui->graphicsView->fitInView(scn->sceneRect(),Qt::KeepAspectRatio);
}

08-27 01:25