Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Loading data into QImages
Qt 6.11 is out! See what's new in the release blog

Loading data into QImages

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    snoring.ninja
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      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.

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        snoring.ninja
        wrote on last edited by
        #3

        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);
        @

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

          [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

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved