[Development] New feature to qstring | QString::toBool()
fransklaver at gmail.com
Tue Oct 25 22:53:02 CEST 2011
On Tue, 25 Oct 2011 22:19:29 +0200, Antonis Tsiapaliokas
<kok3rs at gmail.com> wrote:
> Also, please read the comment from the John Brooks, which i think that
> he isright. So what do you think?
I would say he is right. He strongly hints at the possibility that bool is
too context dependent. And I agree with that.
This thing is tricky to tackle. QString was created to easily handle
localization. Chances are you are going to not only interpret true/false,
but also ja/(nee|nein|nej), oui/non, si/no and whatever may mean true or
yes (or OK?) in whatever language. What if a language introduces a new
word to mean true? You just don't have to handle natural language issues
with doubles, floats and integers, which is why that can be done by
QString hands down.
I think deciding on the meaning of a string is one of the things better
left to its user. Outside the string you know exactly what you expect. A
number, a word, what language the word is going to be in, which words are
going to mean 'true', which are 'false' and which should produce an error.
bool yes = (str.toInt() != 0);
bool yes = (str.compare(tr("true"), Qt::CaseInsensitive) == 0);
bool yes = (str.compare("true", Qt::CaseInsensitive) == 0);
bool yes = QVariant(str).toBool(); // tricky but might work
Based on the fact that you need a lot of information to correctly
interpret the string into a boolean value, I would say this can't really
cover a lot of common cases without being a hackish blob of code. It is
probably better to not include this in QString.
More information about the Development