问题描述:

Simple, I'm trying to display an image in a simple Qt GUI app.

I have this code:

ui->label_2->setVisible(true);

QPixmap supremect(":/images/supremecourt.jpg");

ui->label_2->setPixmap(supremect);

if(supremect.isNull())

{

QMessageBox err(this);

err.setText("File null");

err.exec();

}

building=SPCT; // A flag

ui->label_2->show();

It compiles perfectly, but when I run it, nothing shows. I am sure that the image exists in the resources, so what am I doing wrong?

EDIT: Converting to PNG does not help

网友答案:

Try

QPixmap p;
QString str = ":/images/supremecourt.jpg";
bool retVal = p.load(str);
if(retVal == false)
{
    qDebug() << "Failed to load image!";
    foreach(const QByteArray &fmt, QImageReader::supportedImageFormats())
    {
        qDebug() << QString(fmt);
        // if this doesn't list jpeg then you don't have 
        // the plugin in your imageformats folder.
    }

    if( !QFile::exists(str) )
    {
        qDebug() << "File" << str << "doesn't exist!";
    }
}

http://qt-project.org/doc/qt-4.8/qpixmap.html#load

http://qt-project.org/doc/qt-4.8/qfile.html#exists

If it isn't loading, then your program probably doesn't have access to the right plug-in or the file doesn't exist where you think it does.

The dll for the jpeg plug-in is called qjpeg4.dll and should be in ./imageformats/

Hope that helps.

网友答案:

Did you add the image ( :/images/supremecourt.jpg ) to the resource file correctly? If you are not, watch this. Can you post your resource file's structure?

网友答案:
  1. run it from some IDE (Qt Creator) and check logs. It is almost certain that some error message is shown there.

  2. make sure that jpg plug in is loaded.

  3. make sure that something else doesn't change content of label (for example setText will clear pixmap)

相关阅读:
Top