PDA

View Full Version : QSettings from stream



PepeMoreno
19th April 2011, 19:08
I have a custom datatype called "Repository", which has a QSettings object as a member. I'm implementing the >> stream operator and having trouble while retrieving the QSettings object from the stream as I can't stream directly to QSettings (or so it seems). Is it possible to stream data to a QSettings object? If so, how should I proceed?

The relevant code is:


class Repository::Private {
public:
QString location;
QSettings* settings;

Private()
: location()
, settings(0)
{}
};

Repository::Repository(QObject *parent)
: QObject(parent)
, data(new Private)
{}

QDataStream& operator>> (QDataStream& stream, Repository::Ptr& repository)
{
QString location;
QSettings* settings;

stream >> location;
stream >> settings; /* Can't stream to QSettings */

if (stream.status() != QDataStream::Ok) {
Core::EncodingError e;
e.setDescription("The policy configuration could not be decoded");
throw e;
}
repository->data->location = location;
repository->data->settings = settings;

return stream;
}

The error message:

../../../Workdir/policy-control/policy-control/policy/repository.cpp: In function ‘QDataStream& Policy::operator>>(QDataStream&, Policy::Repository*&)’:
../../../Workdir/policy-control/policy-control/policy/repository.cpp:517: error: no match for ‘operator>>’ in ‘stream >> settings’
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:141: note: candidates are: QDataStream& QDataStream::operator>>(qint8&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:215: note: QDataStream& QDataStream::operator>>(quint8&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:143: note: QDataStream& QDataStream::operator>>(qint16&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:218: note: QDataStream& QDataStream::operator>>(quint16&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:145: note: QDataStream& QDataStream::operator>>(qint32&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:221: note: QDataStream& QDataStream::operator>>(quint32&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:147: note: QDataStream& QDataStream::operator>>(qint64&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:224: note: QDataStream& QDataStream::operator>>(quint64&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:150: note: QDataStream& QDataStream::operator>>(bool&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:151: note: QDataStream& QDataStream::operator>>(float&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:152: note: QDataStream& QDataStream::operator>>(double&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatastream.h:153: note: QDataStream& QDataStream::operator>>(char*&)
../../../Workdir/policy-control/policy-control/policy/repository.cpp:499: note: QDataStream& Policy::operator>>(QDataStream&, Policy::Repository*&)
../../../install/qtsdk-2010.02/qt/include/QtCore/quuid.h:183: note: QDataStream& operator>>(QDataStream&, QUuid&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qrect.h:622: note: QDataStream& operator>>(QDataStream&, QRectF&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qrect.h:200: note: QDataStream& operator>>(QDataStream&, QRect&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qsize.h:256: note: QDataStream& operator>>(QDataStream&, QSizeF&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qsize.h:103: note: QDataStream& operator>>(QDataStream&, QSize&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qline.h:417: note: QDataStream& operator>>(QDataStream&, QLineF&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qline.h:206: note: QDataStream& operator>>(QDataStream&, QLine&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qpoint.h:238: note: QDataStream& operator>>(QDataStream&, QPointF&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qpoint.h:104: note: QDataStream& operator>>(QDataStream&, QPoint&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatetime.h:321: note: QDataStream& operator>>(QDataStream&, QDateTime&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatetime.h:319: note: QDataStream& operator>>(QDataStream&, QTime&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qdatetime.h:317: note: QDataStream& operator>>(QDataStream&, QDate&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qbitarray.h:164: note: QDataStream& operator>>(QDataStream&, QBitArray&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qurl.h:277: note: QDataStream& operator>>(QDataStream&, QUrl&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qvariant.h:531: note: QDataStream& operator>>(QDataStream&, QVariant::Type&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qvariant.h:529: note: QDataStream& operator>>(QDataStream&, QVariant&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qtextstream.h:287: note: QTextStream& operator>>(QTextStream&, QTextStream& (*)(QTextStream&))
../../../install/qtsdk-2010.02/qt/include/QtCore/qlocale.h:669: note: QDataStream& operator>>(QDataStream&, QLocale&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qstringlist.h:245: note: QDataStream& operator>>(QDataStream&, QStringList&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qregexp.h:155: note: QDataStream& operator>>(QDataStream&, QRegExp&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qstring.h:1070: note: QDataStream& operator>>(QDataStream&, QString&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qbytearray.h:577: note: QDataStream& operator>>(QDataStream&, QByteArray&)
../../../install/qtsdk-2010.02/qt/include/QtCore/qchar.h:390: note: QDataStream& operator>>(QDataStream&, QChar&)
../../../Workdir/policy-control/policy-control/policy/repository.cpp:510: warning: unused variable ‘m_effectiveCurrentPolicyOrigin’
make[1]: *** [repository.o] Error 1

mcosta
20th April 2011, 07:46
A solution could be save the contents of DataStream in a QTemporaryFile and read it with QSettings

Suppaman
10th February 2012, 12:53
Hi

I want to use QSettings in the same way too. Solution using temporary file doesn't like me a lot. I checked the source code of this object but it seem there is no way also ereditating the class. Someone know how is possible to use QSettings object in such streaming way?

Thank you

wysota
10th February 2012, 13:03
The code in this thread is invalid. Streaming to a pointer to an object doesn't make sense. At best you'll get an address the settings object had when it was being serialized. This address will not be valid anymore of course.

Suppaman
10th February 2012, 13:12
Hi

Regarding my needs (that could be different from the original author of this thread) I want to download a remote file from a web site. This file will have a ini format in the body but since I download the content of them I have in memory the full body and I want to parse it using QSettings object. Using stream way was the first idea I had but every suggestion is welcomed. Basically I have a string formatted in ini format, how can I pass this string to QSettings jumping the steps save to a file/read from file?

Thank you

wysota
10th February 2012, 15:42
Streaming won't help you here. What you need to do is to work around QFile API so that QFile thinks your memory buffer is a real file. To do that you need to implement QAbstractFileEngine interface that will wrap your memory buffer. However this is more work than implementing your own ini parser.