QDir mkdir with absolutepath

2019-04-20 10:44发布

问题:

I have problem with the creation of dir with Qt. I would like to create a dir in documents'dir so, I make some things like that :

QString path("C:/Users/Me/Documents/MyApp/profiles/");
Qdir dir = QDir::root();
dir.mkdir(path);

But that doesn't work! I have test with "/" and "\" for the separators but in the two cases that not work.

How I can create my dir?

Thank you.

回答1:

Try to use QDir::mkpath as dir.mkpath(path);



回答2:

You can do this:

QDir dir(path);
if (!dir.exists()){
  dir.mkdir(".");
}


回答3:

QDir dir = QDir::root() creates an instance of QDir configured to point to root and copies that setting to dir. To avoid the extra copy and code , you can use QDir dir(QDir::root);. On Windows it will point to the root of the system drive, usually C:\.

dir.mkdir(path); will attempt to create a subdirectory named path in the currently configured directory (root). This method expects a single directory name and not a full path. It also returns a bool result that you should be checking.

You probably want to be calling dir.mkpath(path) which will attempt to create the subdirectory specified along with all the necessary parent directories leading up to it. Again, you should check the result to see if it was successful.



回答4:

please check the following links where they have described how to create the new directory..

http://www.qtcentre.org/threads/19253-QDir-mkpath

http://www.qtforum.org/article/2210/qdir.html

http://www.developer.nokia.com/Community/Wiki/How_to_use_QDir_in_Qt