我在图像预览器中无法将图像正确缩放到窗口大小。我有一个 ImgViewer 用于预览图像的窗口,并希望在主窗口选择新文件时更新显示的图像。在主窗口中单击新图像时,图像将正确更新,但缩放不适用。图像总是显示得太大 imgviewer公司 窗户。
ImgViewer
imgviewer公司
下面是我为图像更新功能编写的代码:
void ImgViewer::update_img(QString img_path){ img_object = new QImage(); img_object->load(img_path); image = QPixmap::fromImage(*img_object); QPixmap scaled_img = image.scaled(img_object->size(), Qt::KeepAspectRatio); scene = new QGraphicsScene(this); scene->addPixmap(scaled_img); scene->setSceneRect(scaled_img.rect()); ui->graphicsView->setScene(scene); }
每次调整窗口大小时是否需要重置场景?
使用您的窗口尺寸在波纹管中 code 以下内容:
code
QPixmap scaled_img = image.scaled(this->width(), this->height(), Qt::KeepAspectRatio);
而不是:
QPixmap scaled_img = image.scaled(img_object->size(), Qt::KeepAspectRatio);
不同长宽比下波纹管的使用: