[Solved] Save for QGenericMatrix
-
Using :
@QGenericMatrix<10,10,int> MyMatrix;@
I intend to implement a function to walkthrough the matrix and save it to a file to be able to reload it afterwards.
While it seems to be quite standard, I'd like an expert advice to implement this Save method according to the recommanded Qt spirit. . I've tried the "copydatato ":http://qt-project.org/doc/qt-4.8/qgenericmatrix.html#copyDataTo but it crashed. -
Hi,
What error are you getting ?
Can you show the code where you use copyDataTo ?
-
Hi,
I'm using it within a method like :
@
void MyClass::Save_To_File(QString Filename)
{
int *buffer;
MyMattrix.copyDataTo(buffer):
}@ -
buffer is not initialized
-
Indeed,but there is not a better way to parse and save it to a file ?
I was mostly thinking about iterate through the matrix for example, -
Why not use:
@
QFile file(filename);
//file open and check
QDataStream stream(&file);
stream << myMatrix;
@?
-
Thought there were something different.
Ok, thank you Samuel -
Do you mean more simple/complicated/verbose ?
You're welcome
-
well....yes ;) something more verbose, this simplicity is puzzling. I don't say it's good or wrong but I was thinking about something traversing the matrix like an iterator. Afterall, it does it anyway.
Edit : I couldn't have the matrix representation in the hex file. For instance, save the matrix only will be ok if I use an array like this:
@int MyClass::Save_To_File(QString Filename)
{
int val[20]={};
unsigned short index ;
QDataStream out(&File_Ptr) ;
QFile File_Ptr(Filename) ;MyMatrix.copyDataTo( val );
if (!File_Ptr.open(QIODevice::WriteOnly ) )
return 1 ;for ( index=0; index<20;index++)
{
out << (unsigned char)(val[index]) ;
}
File_Ptr.close();
return 0 ;
}@ -
What do you mean by "I couldn’t have the matrix representation in the hex file" ?
-
yes, I should have been more explicit: when I use the same class than previously writen with the 1s cell containin '1' ie, matrix[0][0]=1 looking it with hexdump :
000000 0100 0000 ....
while changing the code with :
out << MyMatrix ;change the same data in hexdump to :
000000 f03f 0000 .... -
One of the problem that you have is that you are casting an int to an unsigned char which are not the same size so you could be losing data at some point. The stream operator sends the matrix content as double to the stream.
-
Actually the working code is the one in the post here using the cast , And if I comment that code to use
@stream << myMatrix;@Instead of writing 0100 the file contains f03f