[Qt-interest] removing specialcharacters for filenames

Ben Swerts benswerts at telenet.be
Wed Sep 2 21:55:55 CEST 2009


Hi Alex,

The characters that are invalid for a filename differ between platforms. I
use this function to replace the invalid characters with an underscore for
filenames under Windows as specified in
http://support.microsoft.com/kb/177506:

QString filename = ...
filename.replace( QRegExp( "[" + QRegExp::escape( "\\/:*?\"<>|" ) + "]" ),
QString( "_" ) );

If you want it to be cross-platform, you'll have to replace/remove the
superset of invalid characters.

Hope it helps.
Greets,


	Ben

> HI all,
> 
> is there a best practice for removing special characters, white space and
> so
> on when 'deriving'  filenames from QStrings?
> I have a user provides string and want to derive a sane filename from that
> (ensuring that the preprocessed string will work as filename)
> 
> I found QString::simplified which remose all the ASCII
> characters '\t', '\n', '\v', '\f', '\r', and ' '.
> But how about all the usually not used special characters in filenames?
> 
> 
> alex




More information about the Qt-interest-old mailing list