Results 1 to 3 of 3

Thread: Reading data from file: QFile, Qdatasteam, QImage, QGraphicsscene, QGraphicsview

  1. #1
    Join Date
    May 2011
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Reading data from file: QFile, Qdatasteam, QImage, QGraphicsscene, QGraphicsview

    Hi,

    I am trying to read data from a file, convert it to an image and display it. The trouble starts with the file: it is made of a txt format header (often 4092 bytes) and the rest is unsigned int (0 to 65535, 2 bytes). The unsigned int represent the data that I wish to display as an image (grayscale).
    I am a newbie and the code may not make much more sense after few days of trials and mostly errors.

    So my goal is to read the file, skip the text part (I can get the size of the text header), read the data into some array, convert the array to an image, display the image. All of that the fastest possible.

    Here is my not working code:
    Qt Code:
    1. m_scene->clear();
    2. QVector<quint16> MyArray;
    3.  
    4. QFile file(temp); //temp is QString with the path and file name
    5.  
    6. if (file.open(QIODevice::ReadOnly))
    7. {
    8. file.seek(dlgHeaderBytes);
    9. QDataStream in(&file);
    10.  
    11. quint16 item;
    12. for (int i=0;i<((file.size()-HeaderBytes)/2);i++)
    13. {
    14. in >> item;
    15. MyArray <<item;
    16. }
    17. m_scene->addText("The file "+temp+" was opened succesfully.\n");
    18. QImage MyImage( dlgNX, dlgNY, QImage::Format_Indexed8 );
    19. QVector<QRgb> colorTable;
    20. for (int i = 0; i < 256; i++) colorTable.push_back(QColor(i, i, i).rgb());
    21. MyImage.setColorTable(colorTable);
    22.  
    23. for (int i=0;i<NX;i++)
    24. {
    25. for (int j=0;j<NY;j++)
    26. {
    27. MyImage.setPixel(i, j, uint(MyArray[i+j]/256)); //this to be changed but only when previous steps are working
    28. }
    29. }
    30.  
    31. m_scene->setBackgroundBrush(MyImage);
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

    I don't think that I am reading the data properly from the file. I am not sure on how to handle qdatastream. I have tried while qdatastream.atend() but nothing seems to be read...

    Any help very appreciated.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Reading data from file: QFile, Qdatasteam, QImage, QGraphicsscene, QGraphicsview

    This code probable does too much for anyone to try to read, understand and find bugs in it.

    I would suggest that you
    1) Use another API instead of one that you are not comfortable with -- like read() instead of QDataStream
    2) Create a couple of small test files and hone your file reading to perfection so that you know where the problems are not

  3. #3
    Join Date
    May 2011
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Reading data from file: QFile, Qdatasteam, QImage, QGraphicsscene, QGraphicsview

    and the problem was the index for the array (i+j) is a simple mistake but hard to spot.

Similar Threads

  1. Replies: 3
    Last Post: 8th June 2011, 06:36
  2. Reading data from xls file
    By addu in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2010, 09:33
  3. reading data from file
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2010, 10:31
  4. QFile, QDataStream reading binary data
    By yren in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2009, 06:34
  5. Reading File using QFile and QTextStream
    By atm in forum Qt Programming
    Replies: 4
    Last Post: 13th July 2006, 23:12

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
  •  
Qt is a trademark of The Qt Company.