Solved Display color image using Qlabel
-
@sankar-110 OK, I do a similar thing when getting a pixmap from an image i have changed.
I use an RGBA8888 format but when I use the QRgb i use the qRgba macro and set the alpha to zero like this.QRgb col = qRgba(r,g,b,0);
then i use the QPixmap::fromImage(the Image); with the default conversion argument. I don't put it on a QLabel but it does get used
by a QPainter on a window eventually.Hope that might help.
-
@kenchan , sorry, but can you explain in more detail as I am still a beginner to QT.
If I do not display the image on a label, how will the image be displayed on the UI? -
Hi
Is it possible to get a copy of the image to experiment with ? -
@sankar-110 Nothing wrong with putting a QPixmap on a QLabel.
I think you should play around with different pixel formats and the appropriate qRgb functions. The code looks OK to me but as you can see the image is not being shown as expected. -
@sankar-110
I just noticed you code again here...uint8_t *arr = new uint8_t[size]; where 'size' is uint32_t size = g_qt_width * g_qt_high *sizeof(RGB);
Should that not be uint32_t size = g_qt_width * g_qt_high *sizeof(RGB) * 4 if you are using a QImage::Format_RGB32?
or am i missing what RGB means? -
@kenchan RGB is just my structure here
typedef struct
{
uint8_t r;
uint8_t g;
uint8_t b;
}RGB; -
@sankar-110
I just run this basic code#include <QApplication> #include <QLabel> int main(int argc, char *argv[]) { QApplication a(argc, argv); Qt::ImageConversionFlags flag1 = Qt::ColorOnly; QImage img(100,100,QImage::Format_RGB32); for(int i(0); i < 100; i++){ for(int j(0); j < 100; j++){ QRgb value = qRgb(255,0,0); img.setPixel(i,j,value); } } QLabel lbl; lbl.show(); lbl.resize(100,100); lbl.setPixmap(QPixmap::fromImage(img, flag1)); return a.exec(); }
works perfectly fine.
Try to show your QPixmap in a new, independent QLabel, maybe yours gets overwritten/ modfied later in the code?
Btw,
setPixel
very "heavy" operation, keep that in mind when you plan do modify images multiple times per second or something like that. -
This post is deleted! -
@sankar-110 What @mrjj meant, I believe, is the content of your Array
arr
so that we could create the , supposedly correct, QImage ourself and test it. -
@mrjj , @J-Hilk
typedef struct
{
uint8_t r;
uint8_t g;
uint8_t b;
}RGB;
uint32_t get_video_data(RGB *data,uint32_t *size)
{
const RGB BAR_COLOUR[8] =
{
{ 255, 255, 255 }, // 100% White
{ 255, 255, 0 }, // Yellow
{ 0, 255, 255 }, // Cyan
{ 0, 255, 0 }, // Green
{ 255, 0, 255 }, // Magenta
{ 255, 0, 0 }, // Red
{ 0, 0, 255 }, // Blue
{ 0, 0, 0 }, // Black
};
}
uint32_t size = g_qt_width * g_qt_high *sizeof(RGB);
uint8_t *arr = new uint8_t[size];
RGB *video_frame = (RGB *)malloc(size);
get_video_data(video_frame,&video_size);
memcpy(arr,video_frame,size);
This is the content of my arr.