c++qtqstringqtcoreqchar

How can non-ASCII characters be detected in a QString?


I want to detect if the user has inputted a non-ASCII (otherwise incorrectly known as Unicode) character (for example, り) in a file save dialog box. As I am using Qt, any non-ASCII characters are properly saved in a QString, but I can't figure out how to determine if any of the characters in that string are non-ASCII before converting the string to ASCII. That character above ends up getting written to the filesystem as ã‚Š.


Solution

  • There is no such a built-in feature in my understanding.

    About 1-2 years ago, I was proposing an isAscii() method for QString/QChar to wrap the low-level Unix isacii() and the corresponding Windows function, but it was rejected. You could have written then something like this:

    bool isUnicode = !myString.at(3).isAcii();
    

    I still think this would be a handy feature if you can convince the maintainer. :-)

    Other than that, you would need to check against the ascii boundary yourself, I am afraid. You can do this yourself as follows:

    bool isUnicode = myChar.unicode() > 127; 
    

    See the documentation for details:

    ushort QChar::unicode () const

    This is an overloaded function.