大家好,我在程序中用到了加载图片的功能,代码是我看着Qt的例子写的
……
QString fileName = QFileDialog::getOpenFileName( this, tr( "Image Viewer" ),
QDir::currentPath(), tr( "Image Files (*.png *.jpg *.bmp)" ) ) ;
if ( !fileName.isEmpty() )
{
QFileInfo imageInfo( fileName ) ;
qint64 fileSize = imageInfo.size() ;
if ( fileSize / (1024 * 1024 ) > 1 )
{
QMessageBox::information( this, tr( "Tip" ),
tr( "%1 is too large, choose less than 1M" ).arg( fileName ) ) ;
return ;
}
QImage image( fileName ) ;
if ( image.isNull() )
{
QMessageBox::information( this, tr( "Tip" ),
tr( "Cannot load %1." ).arg( fileName ) ) ;
return ;
}
ui.m_iconPreview->setPixmap( QPixmap::fromImage( image ) ) ;
m_qstrDeviceIcon = fileName ;
}
主要是Qt自带的/examples/widget examples/Image Viewer Example这个小程序,我的程序里加载*.png格式的图片没有问题,但是加载*.jpg图片的时候就会加载失败,到这里"image( fileName );" image为空,输出不能加载*.jpg文件,我的代码基本上和Qt的例子是一样的,Qt例子程序可以加载各种图片没问题,我的问题可能出在哪里呢,一直没有找到原因,很困扰,希望得到大家的帮助,谢谢