【发布时间】:2019-10-20 23:06:04
【问题描述】:
我正在使用 QGraphicsView 和 QGraphicsScene 来显示上传的图像,然后在其上显示一些绘图。我正在上传和图像这样:
void MeasuresWidget::on_openAction_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty())
{
QImage image(fileName);
if (image.isNull())
{
QMessageBox::information(this, tr("Measures Application"), tr("Cannot load %1.").arg(fileName));
return;
}
scene->clear();
scene->addPixmap(QPixmap::fromImage(image).scaledToWidth(w, Qt::SmoothTransformation));
}
}
我面临的问题是,如果我上传的图像小于之前上传的图像,则似乎有空白空间,即场景保持之前图像的大小(较大的图像)并且更大比现在的。我尝试在单个变量中保持场景的原始大小并在每个上传操作中使用setSceneRect():
//in constructor
originalRect = ui->graphicsView->rect();
//in upload action
scene->setSceneRect(originalRect);
但结果是场景的大小始终保持不变,如果大于实际图像,则将其剪切。我之前使用 QLabel 显示图像,我使用了QLabel::setScaledContents() 函数,它对我来说效果很好。那么,我的问题是我可以使用 QGraphicsScene 实现相同的行为吗?
更新 1: 如果我在每个上传操作中创建新场景,应用程序就会按照我想要的方式运行。代码现在看起来像:
void MeasuresWidget::on_openAction_triggered()
{
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty())
{
QImage image(fileName);
if (image.isNull())
{
QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName));
return;
}
scene->clear();
scene->addPixmap(QPixmap::fromImage(image).scaledToWidth(w, Qt::SmoothTransformation));
}
}
这样好吗?我可以实现我想要的行为而无需在每次上传操作时创建新场景吗?
【问题讨论】:
标签: c++ qt scale qgraphicsview qgraphicsscene