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 textQImageReader 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.
-
Hi
Just as a note.
Unless you really want QImage first and use QImageReader etc then
this is also workingui->label_picture->setPixmap(QPixmap(":/123.jpeg"));
Let the pixmap load it directly by self. -
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 syntaxQImageReader read(":/123.jpeg");
notice the :/ in front -
to add to @mrjj
here are further information about Qt's resource systemhttps://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
-
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); ^
-
setPixmap is the setter function.
ui->label_picture->setPixmap(Pixmap::fromImage(image));
-
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. -
@Natural_Bugger
I deliberitly wrotePixmap::fromImage(image)
because fromPixmap returns a pixmap. It does not apply it on itself! -
Hi
Just as a note.
Unless you really want QImage first and use QImageReader etc then
this is also workingui->label_picture->setPixmap(QPixmap(":/123.jpeg"));
Let the pixmap load it directly by self. -
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
-
@Natural_Bugger
I'm terribly sorry, I think the auto correction removed theQ
from QPixmapui->label_picture->setPixmap(QPixmap::fromImage(image));