Results 1 to 19 of 19

Thread: how to use loadFromData in painter

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default how to use loadFromData in painter

    hi everyone,

    i have a RGB values in an character array. can anyone say me to use loadFromData function in QPixmap inorder to convert the RGB datas to a pixmap...?
    i did as follows:

    Qt Code:
    1. void paintEvent(QPaintEvent *)
    2. {
    3. unsigned char buffer[20]={0,0,0,0,0,255,255,255,255,255,0,0,0,0,0};
    4. QPixmap pixmap;
    5. if(pixmap.loadFromData(buffer,20,0,QPixmap::Auto);
    6. QPainter p(this);
    7. p.drawPIxmap(0,0,pixmap,0,0,-1,-1);
    8. }
    To copy to clipboard, switch view to plain text mode 
    . the code gets compiled without error. but nothing is coming on the screen.can anyone provide suggestions for this...?
    are these steps correct...? if no can any correct me...?

    thanks in advance,

    saravanan
    Last edited by wysota; 22nd January 2007 at 14:22. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to use loadFromData in painter

    You need more than just the data:
    Quote Originally Posted by QPixmap docs
    The loader attempts to read the pixmap using the specified format. If the format is not specified (which is the default), the loader probes the file for a header to guess the file format.
    You have to provide a valid header for the format you wish to use.

    In your situation it might be better to create an empty QImage and then use setPixel() or bits() to set the data.

  3. #3
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to use loadFromData in painter

    hi,

    i tried with setpixel.but still not got anything. i tried as follows:

    QImage img;
    if(img.valid(10,10))
    img.setPixel(10,10,200);
    else printf("\n coordinate not valid..");

    like this repeated for about 10 point from x=10,y=0 to x=20,y=0.
    but when i compiled and executed the code, there were following errors:

    coordinate not valid..
    QImage::setPixel: x=10 out of range

    .since i'm using depth as 32bit i gave 200 value to index_or_rgb. i gave 0 also.did not work out.

    Also i tried the following:
    QImage img;
    img.setColor(19,qRgb(255,255,0));
    *(img.scanLine(20)+20)=19;
    .but same error came.

    QImage::setColor: Index 19 out of range
    QImage::scanLine: Index 20 out of range
    Segmentation fault
    .

    Am i missing something...?Is this corect ...?can you say where am going wrong...?

  4. #4
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to use loadFromData in painter

    hi...

    thank you for the suggestion of setPixel. i got it working.

    i did as follows:
    QImage image(x,y,32);
    image.setAlphaBuffer( true );
    for ( uint j = 0; j <x; j++ )
    {
    for ( uint i = 0; i <y; i++ )
    {
    QRgb pixel;
    pixel = qRgb(0,0,0);
    image.setPixel( i, j, pixel );
    }
    }
    . this worked perfectly. earlier i did not create QImage and set the pixel value. now i created QImage and then set the pixel value.

    saravanan.

  5. #5
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to use loadFromData in painter

    hi..

    not over yet...i tried specifying the format in loadFromData as "BMP"as follows:

    unsigned char buffer[20]={255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255};
    QImage img(100,100,32);
    img.setAlphaBuffer( true );

    if(img.loadFromData(buffer,20,"BMP"))
    printf("\n the image is loaded...\n");
    else printf("\n image not loaded...\n");

    but image is not loaded.. is this correct...?

    any suggestions for this...?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to use loadFromData in painter

    I don't see a valid BMP header here...

    http://www.fastgraph.com/help/bmp_header_format.html

  7. #7
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to use loadFromData in painter

    hi...

    thanks for the response.but still a bit clarification.can you say me how to specify the BMP header in this ...?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to use loadFromData in painter

    You can't just pass the pixel data, you need to pass the data in a format of your choice. For example if you want to feed the data in BMP format, the data has to contain the BMP header. Check out the link I gave you, you have to construct a block of data according to the rules mentioned there. If you just want to set some pixels though, I suggest you use QImage::setPixel() instead.

  9. #9
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to use loadFromData in painter

    hi..

    yeah i saw the link . i had programmed like this: i'l open a bmp image, read the file header information and will allocate the temporary buffer with size given in the file header information. then feed this temproray buffer to the QImage::loadFromData ( const uchar * buf, uint len, const char * format = 0 ).

    but still could not get it.i had attached the code.

    is this correct way....?
    Attached Files Attached Files

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to use loadFromData in painter

    If the file is a valid BMP file, then just create the QImage from the file...

    Qt Code:
    1. QImage imag("/home/qtprograms/test8.bmp");
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Dec 2006
    Posts
    123
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: how to use loadFromData in painter

    hi....

    if we use like this directly then is there any need for loadFromData ... i.e. is this following correct..?

    QImage img("/home/qtprograms/text8.bmp");
    QPainter p;
    p.drawImage ( QPoint(0,0),img,0);

    ..
    sorry for the late reply. went out-of-station.

Similar Threads

  1. Strange Painter behaviour
    By cwomeijer in forum Qt Programming
    Replies: 2
    Last Post: 4th September 2006, 20:52
  2. How to manage QPainter?
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 28th April 2006, 13:20
  3. Painter not active!
    By Caius Aérobus in forum Qt Programming
    Replies: 7
    Last Post: 30th March 2006, 15:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.