PDA

View Full Version : Reading data from file: QFile, Qdatasteam, QImage, QGraphicsscene, QGraphicsview



rno
17th July 2011, 20:06
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:



m_scene->clear();
QVector<quint16> MyArray;

QFile file(temp); //temp is QString with the path and file name

if (file.open(QIODevice::ReadOnly))
{
file.seek(dlgHeaderBytes);
QDataStream in(&file);

quint16 item;
for (int i=0;i<((file.size()-HeaderBytes)/2);i++)
{
in >> item;
MyArray <<item;
}
m_scene->addText("The file "+temp+" was opened succesfully.\n");
QImage MyImage( dlgNX, dlgNY, QImage::Format_Indexed8 );
QVector<QRgb> colorTable;
for (int i = 0; i < 256; i++) colorTable.push_back(QColor(i, i, i).rgb());
MyImage.setColorTable(colorTable);

for (int i=0;i<NX;i++)
{
for (int j=0;j<NY;j++)
{
MyImage.setPixel(i, j, uint(MyArray[i+j]/256)); //this to be changed but only when previous steps are working
}
}

m_scene->setBackgroundBrush(MyImage);

}


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.

mvuori
18th July 2011, 12:58
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

rno
24th July 2011, 22:01
and the problem was the index for the array (i+j) is a simple mistake but hard to spot.