Results 1 to 7 of 7

Thread: Constructing QImage from QBytearray

  1. #1
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Constructing QImage from QBytearray

    Hi

    I am reading a raw binary data from a file into QBytearray. I want to use the QBytearray to construct a QImage. My code is shown below.

    Qt Code:
    1. QFile fileIn (fileName);
    2. fileIn.open(QFile::ReadOnly);
    3. QByteArray data = fileIn.readAll();
    4. QImage img(2048,1024,QImage::Format_Indexed8);
    5. img = QImage::fromData(data,QImage::Format_Indexed8);
    6. QPixmap qp=QPixmap::fromImage(img);
    7. BPlaneIN->setPixmap(qp);
    To copy to clipboard, switch view to plain text mode 

    error: conversion from `QByteArray' to `const uchar*' is ambiguous
    Could some tell me why I get this error message. I have used scanline to get this working but am confused as to why this method of loading the buffer doesnt

    Regards
    Last edited by jpn; 18th December 2008 at 17:12. Reason: missing [code] tags

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Constructing QImage from QBytearray

    try something like this
    Qt Code:
    1. ...
    2. img = QImage::fromData(data,"PNG");//the second param is format name
    3. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Constructing QImage from QBytearray

    Thanks Spirit

    But it isnt "PNG" format, it is raw 8bit data. The intention is to create an empty QImage, the load QByteArray with data from a file. Then use QImage::fromData to load the QImage buffer. I dont understand why I need to specify a file format?

    It does stop the compiler warning. Why is that?

    Regards

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Constructing QImage from QBytearray

    Quote Originally Posted by dbrmik View Post
    I dont understand why I need to specify a file format?
    how do you think QImage recognize image format? from special field in data.
    so, if you data have no specifiyed format then you need to fill QImage pixel by pixel. on this forum this is question was discussed many times use search by forum.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Constructing QImage from QBytearray

    Hi

    But I am specifying the image format it is "Format_Indexed8"

    Qt Code:
    1. //load pixel data
    2. QByteArray data = fileIn.readAll();
    3.  
    4. //empty image
    5. QImage img(2048,1024,QImage::Format_Indexed8);
    6.  
    7. //load pixel data into empty image from QByteArray
    8. img = QImage::fromData(data,QImage::Format_Indexed8);
    To copy to clipboard, switch view to plain text mode 

    I just dont understand why the above does not work?

    Regards
    Last edited by jpn; 18th December 2008 at 17:13. Reason: missing [code] tags

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Constructing QImage from QBytearray

    read this reading and writting image files.
    QImage::Format is not format of file, this is pixel format.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Constructing QImage from QBytearray

    Hi

    Thanks, Yes I've read it. The data loaded from the file is raw 8bit binary data. I want to load the data into QByteArray and then load the image buffer with the contents of QByteArray.

    Is this QImage member funxction

    bool QImage::loadFromData ( const QByteArray & data, const char * format = 0 )
    equivalent to
    Qt Code:
    1. void loadImageData(uchar *data, int rows,int cols,QImage *rawImage)
    2. {
    3. uchar* row;
    4. for (int i = 0; i < rows; i++)
    5. {
    6. //access each row of data values
    7. row= rawImage->scanLine(i);
    8. for (int j = 0; j < cols; j++) //width
    9. row[j] =data[i * cols + j] ;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your advice
    Last edited by jpn; 18th December 2008 at 17:14. Reason: missing [code] tags

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 17:37
  2. QImage to QByteArray
    By navi1084 in forum Qt Programming
    Replies: 5
    Last Post: 15th October 2008, 10:36
  3. QImage to QString
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 7th October 2008, 08:29
  4. Multiple QPainters on QImage
    By fire in forum Qt Programming
    Replies: 3
    Last Post: 14th August 2008, 14:10
  5. QShared
    By sabeesh in forum Qt Programming
    Replies: 11
    Last Post: 11th October 2007, 13:40

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.