本文介绍了Qt5 C ++ QGraphicsView:图像不适合视图框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个程序,显示用户选择他的一些图片。但是有一个问题,因为我想适合这张图片在QGraphicsView的框架,图片真的小于框架。

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 the picture is really smaller than the frame.

这里是我的代码:

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像素。什么是错误?

I was trying a lot of solutions that I found on web, but no one didn't help me. The picture is in size around 40 x 60 px when the frame is 200 x 400 px. What could be wrong?

下面是一些使用上面的代码生成的示例,以及我想要得到的结果:

Here is some example of what is produced with code above and what I want to get out:

推荐答案

解决方案我的问题是showEvent()对话框。这意味着你不能在窗体显示之前调用fitInView(),所以你必须为dialog创建showEvent(),并且图片将被嵌入到QGraphics View的框架中。

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);
}

这篇关于Qt5 C ++ QGraphicsView:图像不适合视图框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 07:06