Loading data into QImages
I’m fairly new to Qt so I hope this doesn’t sound like a really stupid question.
I spend nearly 4 hours trying to load data into a QImage with no luck. Here’s my code
- uchar data[100];
- for(int i=0;i<100;i++)
- data[i]=200;
- QPixmap Image;
- Image.fromImage(I);
- ui->imgHolder->setPixmap(Image);
My code compiles without any errors but does nothing. It should display a 10X10 square, instead nothing happens. Any clue to what I’m doing wrong would be greatly appreciated.
Thanks in advance
3 replies
There are a couple of problems with that snippet:
- QPixmap::fromImage is a static method that returns the pixmap
- the QImage ctor is wrong:
- being a 10×10 image, the format is not mono but 8bpp;
- you must specify the byte stride, or QImage assumes that each scanline is 32bit aligned;
- you have to be sure that the data base address is 32bit aligned.
Thanks for that. I followed your first and 2nd point but could you elaborate a little more on byte stride and data base address. Would both those issues be resolved if I used ARGB data (which is what I intend to use.)
Of course I didn’t mean “database address”, but “data” base address. See for instance http://doc.trolltech.com/4.7/qimage.html#QImage-4 versus http://doc.trolltech.com/4.7/qimage.html#QImage-6
You must log in to post a reply. Not a member yet? Register here!


