开发者

Display image in Qt that is coming from an unsigned char**

I am trying to display an image in Qt that is coming in as data from another function.

That function stores the rows of an image as an unsigned char**

I read somewhere I could somehow store it as a QByteArray and then create a QPixMap then set the QPixMap of a label to display it, but I am not having much luck.

This is what I had:

unsigned char* fullCharArray = new unsigned char[imheight * imwidth];
for (int i = 0 ; i < imheight ; i++)
for (int j = 0 ; j < imwidth ; j++)
fullCharArray[i*j+j] = imageData[i ][j];

QPixmap *p = new QPixmap(reinterpret_cast<const char *>(fullCharArray));
ui->viewLabel->setPixMap(p);

But this seems to give me an error, and may be the wrong thing anyway. When i tried to call setPixMap(p[]) the error goes away, but the image does not get displayed in the label.

imageData is the 2D array that is populated by my other function. Best I 开发者_StackOverflowfigured in order to create a new QPixMap I had to convert that to a 1D array, and do the index calculations manually there. That is what the double For loop is doing.

So is there a better way of displaying this image data in Qt?


Use QImage. This constructor should help you: http://doc.qt.io/qt-5/qimage.html#QImage-4


Pixmaps are not 2D arrays of pixels. They are actually defined as an array of character strings describing the image.

For example, a 4x4 pixels image with center 4 pixels black, and 4 corner pixels red is:

/* XPM */
static char * sample_xpm[] = {
"4 4 3 1",
"   c #FF0000",
".  c #FFFFFF",
"+  c #000000",
" .. ",
".++.",
".++.",
" .. "};

It is a convenient file format to store small images (e.g. for toolbar buttons) that can be integrated as is into a C source file using a standard #include directive, to be compiled along with the rest of the program.


You use QPixmap if the data is already in an image format like PNG or jpeg with a header.

To create an image from pixel values use Qimage, then convert this to a pixmap (Qpixmap.fromImage() - if needed to save it or display it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜