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;
    QImage I(data,10,10,QImage::Format_Mono);
    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



  • 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 10x10 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.)

    This is the new code for anyone else interested. Find anymore mistakes?
    @
    uchar data[100];
    for(int i=0;i<100;i++)
    data[i]=200;
    QPixmap Image;
    QImage I(data,50,2,QImage::Format_Indexed8);
    Image=Image.fromImage(I);
    ui->imgHolder->setPixmap(Image);
    @



  • [quote author="snoring.ninja" date="1294769905"]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.)[/quote]

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.