PDA

View Full Version : valid a path name



klaus1111
23rd July 2006, 12:00
When a user enters a non valid path name for a folder (for example containing not allowed characters) so that I can not create the path via QDir().mkpath(pathname), qt tells me via the false return that this was not possible.
Is there a way allready included in Qt to tell the user why he can not create the folder? (invalid characters in path name, target write protected...)
Klaus

wysota
23rd July 2006, 12:34
Hi Klaus!

You'd have to do all the checks yourself, before calling mkPath(). I think you'd have to iterate or recurse through deeper and deeper components of the path and do the checks using QFileInfo or something like that. Remember that mkPath can create multiple directories (if parents to the target directory don't exist), so it might be wiser to use mkDir() instead and create all those dirs yourself one at a time.

A snippet that more or less should do it:


QString pathtocreate; // (absolute)
QStringList components = pathtocreate.split('/');
QDir dir("/");
foreach(QString component, components){
if(dir.exists(component)){
dir.cd(component);
} else {
QFileInfo finfo(dir, component);
// validate path
// ...
// for example:
// if(!finfo.isWritable()) return QString("Directory not writable");
bool r = dir.mkDir(component);
if(!r) return QString("Could not create directory %1").arg(dir.absoluteFilePath(component));
r = dir.cd(component);
if(!r) return QString("Directory %1 not accessible").arg(dir.absoluteFilePath(component));

}
}

Cesar
23rd July 2006, 13:45
I suppose you'd better use QDir::separator() instead of '/'.

QStringList components = pathtocreate.split(QDir::separator());

wysota
23rd July 2006, 15:07
I suppose you'd better use QDir::separator() instead of '/'.

That's part of the "more or less" statement ;)