I’m on Windows7 and using Qt SDK 4.8.
Trying to read a file in with QImage but it just doesn’t seem to load. That is, QImage(filename) or QImage(filename, “PNG”) or QImage.load(filename) always return NULL.
Here’s my code:
void MainWindow::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"),
QDir::homePath(),
tr("Image Files (*.png *.tga *.bmp)"));
if (!fileName.isEmpty())
{
targetImage = new QImage(fileName, "PNG");
if(targetImage->isNull())
{
QMessageBox::information(this,
tr("PhotoChop"),
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));
}
}
What am I doing wrong?
it has to do with how QImage loads “plugins” for reading files in. One option is to enable these plugins (for different file formats) and then linking against them.
Though instead I just used Gdiplus::Bitmap (from windows) to load in the file and then, from that, created an HBITMAP which QPixmap can use.