Thanks, the QDataStream precision was the problem with the size of my QByteArray. Unfortunetly I am still having problems with my >> operator, << seems to be working fine. Here are my two classes:
{
Q_OBJECT
public:
{
this->sImage = new QVector<float>();
}
QVector<float> *sImage;
float sImageSum;
float sImage2Sum;
};
{
stream << *(storage->sImage) << sImageSum << sImage2Sum;
return stream;
}
{
storage = new siStorage();
stream >> *(storage->sImage) >> sImageSum >> sImage2Sum;
return stream;
}
class siStorage: public QObject
{
Q_OBJECT
public:
siStorage(QObject *parent = 0) : QObject(parent)
{
this->sImage = new QVector<float>();
}
QVector<float> *sImage;
float sImageSum;
float sImage2Sum;
};
QDataStream &operator<<(QDataStream &stream, const siStorage *storage)
{
stream << *(storage->sImage) << sImageSum << sImage2Sum;
return stream;
}
QDataStream &operator>> (QDataStream &stream, siStorage *storage)
{
storage = new siStorage();
stream >> *(storage->sImage) >> sImageSum >> sImage2Sum;
return stream;
}
To copy to clipboard, switch view to plain text mode
{
Q_OBJECT
public:
{
stack = new QList<siStorage*>();
}
void populateStack() { ... }
void pushToDataBase() {
//converts to stack to QByteArray and loads to a database
out.
setFloatingPointPrecision(QDataStream::SinglePrecision);
out << *(this);
... // Database code
}
void pullFromDataBase(){
//selects BLOB from database, converts to QByteArray, and then places in stack
//... DB code to populate stackByteArray
in.
setFloatingPointPrecision(QDataStream::SinglePrecision);
in >> *(this);
}
QList<siStorage*> *stack;
};
{
stream << *(stack.stack);
return stream;
}
{
stream >> *(stack.stack);
return stream;
}
class siStack: public QObject
{
Q_OBJECT
public:
siStack(QObject *parent=0) : QObject(parent)
{
stack = new QList<siStorage*>();
}
void populateStack() { ... }
void pushToDataBase() {
//converts to stack to QByteArray and loads to a database
QByteArray stackByteArray;
QBuffer buffer(&stackByteArray);
buffer.open(QIODevice::WriteOnly);
QDataStream out(&buffer);
out.setFloatingPointPrecision(QDataStream::SinglePrecision);
out << *(this);
... // Database code
}
void pullFromDataBase(){
//selects BLOB from database, converts to QByteArray, and then places in stack
QByteArray stackByteArray;
//... DB code to populate stackByteArray
QBuffer buffer(&stackByteArray);
buffer.open(QIODevice::ReadOnly);
QDataStream in(&buffer);
in.setFloatingPointPrecision(QDataStream::SinglePrecision);
in >> *(this);
}
QList<siStorage*> *stack;
};
QDataStream &operator<<(QDataStream &stream, const siStack &stack)
{
stream << *(stack.stack);
return stream;
}
QDataStream &operator>>(QDataStream &stream, siStack &stack)
{
stream >> *(stack.stack);
return stream;
}
To copy to clipboard, switch view to plain text mode
But when I test my code it will not allow me to access a siStorage stored in siStack::stack. Here is a short example.
siStack *stack = new siStack();
stack->populateStack();
qDebug() << stack->stack->size; //Example size = 5;
qDebug() << *(stack->stack->at(5)->sImage); // displays ok;
stack->pushToDataBase();
stack->stack->clear;
qDebug() <<stack->stack->size; size = 0;
stack->pullFromDataBase();
qDebug() <<stack->stack->size; // size = 5 as above!
qDebug() << *(stack->stack->at(5)->sImage); // code crashes with error code -1073741819
stack->pullFromDataBase();
siStack *stack = new siStack();
stack->populateStack();
qDebug() << stack->stack->size; //Example size = 5;
qDebug() << *(stack->stack->at(5)->sImage); // displays ok;
stack->pushToDataBase();
stack->stack->clear;
qDebug() <<stack->stack->size; size = 0;
stack->pullFromDataBase();
qDebug() <<stack->stack->size; // size = 5 as above!
qDebug() << *(stack->stack->at(5)->sImage); // code crashes with error code -1073741819
stack->pullFromDataBase();
To copy to clipboard, switch view to plain text mode
Bookmarks