Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

loading image file into label



  • Hi,

    i loaded via designer a "jpeg" file into a label and everything is fine.
    but after compiling the nothing shows up.

    i used the code code from this topic:
    link text

    QImageReader read("123.jpeg");
    QImage image = read.read();
    
    if (!image.isNull()){
        QMessageBox::information(this, "is loaded","",QMessageBox::Ok);
    }
    else{
        QMessageBox::information(this, "not loaded","",QMessageBox::Ok);
    }
    

    but the "not" loaded Messagebox appears.

    the image file resides in the project folder.

    do you need to do something special like in Visual Studio, add to Recources?
    i have added the file into the project as "add eisting file".

    kind regards.


  • Lifetime Qt Champion

    Hi
    Just as a note.
    Unless you really want QImage first and use QImageReader etc then
    this is also working

    ui->label_picture->setPixmap(QPixmap(":/123.jpeg"));
    Let the pixmap load it directly by self.


  • Lifetime Qt Champion

    Hi
    The path is wrong
    When the program is run, the current folder is the build folder
    and not the project folder so it won't be able to find the "123.jpeg" in the project folder.

    The easy way is to include a resource file to the project and then add the image to the resource file
    and then use the correct syntax

    QImageReader read(":/123.jpeg");
    notice the :/ in front


  • Moderators

    to add to @mrjj
    here are further information about Qt's resource system

    https://doc.qt.io/qt-5/resources.html

    also make sure to rerun qmake after adding a new resource file and/or resource to your resource file



  • @mrjj

    thnx, that worked.

        QPixmap pixmap;
        QImageReader read(":/123.jpeg");
        QImage image = read.read();
    
        if (!image.isNull()){
            QMessageBox::information(this, "is loaded","",QMessageBox::Ok);
            ui->label_picture->pixmap(pixmap.fromImage(image));
        }
        else{
            QMessageBox::information(this, "not loaded","",QMessageBox::Ok);
        }
    

    the image is loaded, but ...

    ui->label_picture->pixmap(pixmap.fromImage(image));
    

    results in:

    error: no matching function for call to 'QLabel::pixmap(QPixmap)'
             ui->label_picture->pixmap(pixmap.fromImage(image));
                                                              ^
    

    i also tried:

    ui->label_picture->pixmap.load(image);
    

    but also no working:

    error: '((MainWindow*)this)->MainWindow::ui->Ui::MainWindow::<anonymous>.Ui_MainWindow::label_picture->QLabel::pixmap' does not have class type
             ui->label_picture->pixmap.load(image);
             ^
    

  • Moderators

    @Natural_Bugger

    setPixmap is the setter function.

    ui->label_picture->setPixmap(Pixmap::fromImage(image));
    


  • @J-Hilk

    thnx for your help.

            QPixmap pixmap;
            QImageReader read(":/123.jpeg");
            QImage image = read.read();
            pixmap.fromImage(image);
        
            if (!image.isNull()){
                QMessageBox::information(this, "is loaded","",QMessageBox::Ok);
    
                ui->label_picture->setPixmap(pixmap);
            }
            else{
                QMessageBox::information(this, "not loaded","",QMessageBox::Ok);
            }
    

    QMessageBox = "is loaded"
    but still doesn't show up in the label / user interface, although it compiles without errors and executes.


  • Moderators

    @Natural_Bugger
    I deliberitly wrote Pixmap::fromImage(image) because fromPixmap returns a pixmap. It does not apply it on itself!


  • Lifetime Qt Champion

    Hi
    Just as a note.
    Unless you really want QImage first and use QImageReader etc then
    this is also working

    ui->label_picture->setPixmap(QPixmap(":/123.jpeg"));
    Let the pixmap load it directly by self.



  • @J-Hilk

    i tried that, but ...

    ui->label_picture->setPixmap(Pixmap::fromImage(image));
    

    results in:

    error: 'Pixmap' has not been declared
             ui->label_picture->setPixmap(Pixmap::fromImage(image));
                                          ^
    

    i tried to solve it and i changed Pixmap into pixmap, which resulted in:

    error: 'pixmap' is not a class, namespace, or enumeration
             ui->label_picture->setPixmap(pixmap::fromImage(image));
                                          ^
    

    i'm rusty


  • Moderators

    @Natural_Bugger
    I'm terribly sorry, I think the auto correction removed the Qfrom QPixmap

    ui->label_picture->setPixmap(QPixmap::fromImage(image));
    


  • @mrjj

    thnx, that worked.

    @J-Hilk

    thnx, now it's working.


Log in to reply