Search code examples
imageqtlabelqstringqpixmap

Qt5 QString and QPixmap


My image does not appear :

QString champ("C:/champions/" + ui->comboBox->currentText() + "_1.jpg");
QPixmap image(champ);


ui->label_1->setPixmap(QPixmap(image));

I tried to solve this for 2 hours. Help me please ! Sorry if my english is bad because i'm french ^^ .


Solution

  • Qt QPixmap pointer limit

    http://qt-project.org/doc/qt-5.0/qtgui/qpixmap.html#details

    Like I mentioned in the answer above, use

    bool retVal = QPixmap::load(champ);
    

    Then check the retVal to see what happened.

    if(retVal == false)
    {
        qDebug() << "These are the supported formats:" 
                 << QImageReader::supportedImageFormats();
        if(QFile::exists(champ) == false)
        {
            qDebug() << "Unable to find file" << champ;
        }
    }
    

    Also make sure that your QPixmap isn't going out of scope. So put it as a member function in your header file. Otherwise it may not exist at the end of your constructor.

    http://qt-project.org/doc/qt-5.0/qtgui/qimagereader.html#supportedImageFormats

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

    Hope that helps.