January 11, 2011

snoring.ninja snoring.ninja
Lab Rat
2 posts

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

  1.     uchar data[100];
  2.     for(int i=0;i<100;i++)
  3.         data[i]=200;
  4.     QPixmap Image;
  5.     QImage I(data,10,10,QImage::Format_Mono);
  6.     Image.fromImage(I);
  7.     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

January 11, 2011

peppe peppe
Ant Farmer
1025 posts

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.

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

January 11, 2011

snoring.ninja snoring.ninja
Lab Rat
2 posts

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.)

This is the new code for anyone else interested. Find anymore mistakes?

  1.     uchar data[100];
  2.     for(int i=0;i<100;i++)
  3.         data[i]=200;
  4.     QPixmap Image;
  5.     QImage I(data,50,2,QImage::Format_Indexed8);
  6.     Image=Image.fromImage(I);
  7.     ui->imgHolder->setPixmap(Image);

January 11, 2011

peppe peppe
Ant Farmer
1025 posts
snoring.ninja wrote:
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

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

 
  ‹‹ Mainwindow layout problem with QDockWidget.      QGraphicsView && QGraphicsPixmapItem && effect while changing image ››

You must log in to post a reply. Not a member yet? Register here!