Reputation: 870
Hello world, I am working on a project where i can serialize a n object in a file and read it later but when i write the values(boolean value), it works but when i try to read them, this is what i get:
binary '>>' : no operator found which takes a right-hand operand of type bool(or there is no acceptable conversion)
This is my code
void MainWindow::writeSettings()
{
QFile *settingsFile = new QFile(":/images/settings_file.txt");
if(!settingsFile->open(QIODevice::WriteOnly))
{
qDebug() << "File not responsing";
} else
{
QDataStream out(settingsFile);
out.setVersion(QDataStream::Qt_5_3);
out << settings->getEnableWordWrap();
out << settings->getShowStatusbar();
out << settings->getShowToolbar();
}
settingsFile->flush();
settingsFile->close();
}
Now if i try to read, get the error
QFile selc(":/images/settings_file.txt");
if(!selc.open(QIODevice::ReadOnly))
{
qDebug() << "File not responding";
} else
{
QDataStream in(&selc);
in >> settings->getEnableWordWrap() >> settings->getShowStatusbar() >> settings->getShowToolbar();
}
selc.close();
getEnableWordWrap()
, getShowStatusbar()
and getShowToolbar()
are all boolean return types.
Upvotes: 0
Views: 577
Reputation: 2969
You are trying to change a value, the one provide by your getter.
You should prefer extracting your bool values from your stream, before passing them to the right setter:
QDataStream in(&selc);
bool tmp;
in >> tmp;
settings->setEnableWordWrap(tmp);
in >> tmp;
settings->setShowStatusbar(tmp);
in >> tmp;
settings->setShowToolbar(tmp);
A more oriented object way to achieve that with Qt is to define these two methods to handle QDataStream serialization:
QDataStream &operator<<(QDataStream &, const MySettings &);
QDataStream &operator>>(QDataStream &, MySettings &);
Hence, you can deserialize your settings like this:
QDataStream in(&selc);
in >> settings;
Upvotes: 3