I need to know about the conversion for QTextStream to a bool variable. Have a look at my code :
QFile file(SOME FILENAME);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) qDebug() << "FILE COULD NOT BE OPENED";
QTextStream stream(&file);
bool dBm = false;
if (!stream.atEnd()) stream >> dbM;
As my above expression is throwing the error for which I need help.
There is no
>>operator forQTextStreamandboolout of the box.The doc. of QTextStream mentions a lot of input operators but none for
bool:However, it's not that complicated to add one for the personal joy.
The most complicated about this is what @Frank Osterfeld already mentioned in this comment – to define an adequate textual representation of
trueandfalse.For my MCVE, I used simply
0and1.testQTextStreamInputBool.cc:testQTextStreamInputBool.pro:Compiled and tested in
bashof cygwin64:Of course, it would make sense to overload the
operator<<(QTextStream&, bool)as well to have a matching custom output for the custom input.If not, the compiler will convert the
booltoint, implicitly, and useQTextStream::operator<<(int). Hence,falsewill be written as0,trueas1.That's just what my overloaded bool stream input operator expects...