[SOLVED] Check whether a string is a valid filename with Qt

Issue

Is there a way with Qt 4.6 to check if a given QString is a valid filename (or directory name) on the current operating system ? I want to check for the name to be valid, not for the file to exist.

Examples:

// Some valid names
test
under_score
.dotted-name

// Some specific names
colon:name // valid under UNIX OSes, but not on Windows
what? // valid under UNIX OSes, but still not on Windows

How would I achieve this ? Is there some Qt built-in function ?

I’d like to avoid creating an empty file, but if there is no other reliable way, I would still like to see how to do it in a “clean” way.

Many thanks.

Solution

I don’t think that Qt has a built-in function, but if Boost is an option, you can use Boost.Filesystem’s name_check functions.

If Boost isn’t an option, its page on name_check functions is still a good overview of what to check for on various platforms.

Answered By – Josh Kelley

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *