tr("Open Image"),
tr("Image Files (*.png *.tga *.bmp)"));
// you could have a BMP or Targa file at this point
// Qt has no Targa format support out of the box
if (!fileName.isEmpty())
{
targetImage
= new QImage(fileName,
"PNG");
// Not much good if the file is not a PNG
// You allocate memory you do not free: memory leak
// No need for this to be on the heap at all
if(targetImage->isNull())
{
tr("Viewer"),
tr("Cannot load %1.").arg(fileName));
return;
}
onScreenImage.
setBackgroundRole(QPalette::Base);
onScreenImage.setScaledContents(true);
onScreenImage.
setPixmap(QPixmap::fromImage(*targetImage
));
// Why did you use a QImage if you require a QPixmap ultimately?
}
}
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"),
QDir::homePath(),
tr("Image Files (*.png *.tga *.bmp)"));
// you could have a BMP or Targa file at this point
// Qt has no Targa format support out of the box
if (!fileName.isEmpty())
{
targetImage = new QImage(fileName, "PNG");
// Not much good if the file is not a PNG
// You allocate memory you do not free: memory leak
// No need for this to be on the heap at all
if(targetImage->isNull())
{
QMessageBox::information(this,
tr("Viewer"),
tr("Cannot load %1.").arg(fileName));
return;
}
onScreenImage.setBackgroundRole(QPalette::Base);
onScreenImage.setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
onScreenImage.setScaledContents(true);
onScreenImage.setPixmap(QPixmap::fromImage(*targetImage));
// Why did you use a QImage if you require a QPixmap ultimately?
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks