Display an image in a c++ program
-
Thanks for your answer,
I tried to add a label from the widget box, i added a widget called 'label' but it added a 'text label' what is the name of the widget i need to add from the widget box ?@JYG61 said in Display an image in a c++ program:
Thanks for your answer,
I tried to add a label from the widget box, i added a widget called 'label' but it added a 'text label' what is the name of the widget i need to add from the widget box ?The name is a bit misleading but that is the correct widget.
In your code you can simply do:
setupUi(this): QImage image("tonImage.jpg"); ui->label->setPixmap(QPixmap::fromImage(image)); -
Look at this constructor using the
QImage::Format_RGB888image format.Please take the time to read the documentation, especially the part about the buffer lifetime.
-
Hello,
I tried this code :
unsigned char tab[400];
int i;
for (i=0;i<400;i++) tab[i]=i%256;
ui->setupUi(this);
//QImage image("/home/x/passeport.jpg");
QImage image (tab, 100, 100, QImage::Format_RGB888, nullptr, nullptr);
ui->label->setPixmap(QPixmap::fromImage(image));
I just replaced le loading of the image file (commented) by an unsigned char[400], there are no errors at compilation but when i try to start it, it crashes immediately -
Hello,
I tried this code :
unsigned char tab[400];
int i;
for (i=0;i<400;i++) tab[i]=i%256;
ui->setupUi(this);
//QImage image("/home/x/passeport.jpg");
QImage image (tab, 100, 100, QImage::Format_RGB888, nullptr, nullptr);
ui->label->setPixmap(QPixmap::fromImage(image));
I just replaced le loading of the image file (commented) by an unsigned char[400], there are no errors at compilation but when i try to start it, it crashes immediately@JYG61 said in Display an image in a c++ program:
it crashes immediately
What do you expect?
An rgb image with 100x100 needs a little bit more memory than 400bytes... 100 * 100 * 3 I would guess. -
J JYG61 has marked this topic as solved