QT创建文件夹

QDir *folder = new QDir;
//判断创建文件夹是否存在
bool exist = folder->exists("C:/ASYS/BCI/Import");
if(exist)
            {
                QMessageBox::warning(this,tr("创建文件夹"),tr("文件夹已经存在!"));
            }
            else //如果不存在,创建文件夹
            {
                //创建文件夹
                bool ok = folder->mkpath("C:/ASYS/BCI/Import");
                //判断是否成功
                   if(ok)
                   {
                       QMessageBox::warning(this,tr("创建文件夹"),tr("文件夹创建成功!"));
                   }
                   else
                   {
                       QMessageBox::warning(this,tr("创建文件夹"),tr("文件夹创建失败!"));
                   }
            }

重要的问题是mkpath和mkdir的区别,有很多博主使用的是mkdir,如果您跟我一样需要创建一个父目录不存在的目录,请使用mkpath,否则会导致新建文件夹不成功

下面贴一下qt帮助里关于mkdir和mkdir的区别

bool QDir::mkdir(const QString &dirName) const

Creates a sub-directory called dirName.

Returns true on success; otherwise returns false.

If the directory already exists when this function is called, it will return false.

See also rmdir().

bool QDir::mkpath(const QString &dirPath) const

Creates the directory path dirPath.

The function will create all parent directories necessary to create the directory.

Returns true if successful; otherwise returns false.

If the path already exists when this function is called, it will return true.

See also rmpath().

会发现mkpath该函数将创建创建目录所需的所有父目录。

而mkdir只会在已有的目录下创建一个文件夹

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值