QDir mkdir with absolutepath

倖福魔咒の 提交于 2019-12-06 21:15:09

问题


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



来源:https://stackoverflow.com/questions/9641807/qdir-mkdir-with-absolutepath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!