How to create a new File with full path in Qt?

后端 未结 2 1579
鱼传尺愫
鱼传尺愫 2021-01-07 17:58

I am a Qt beginner and just got stuck with the problem. I am looking for a file SomePath/NewDirectoryA/NewFile.kml (NewFile.kml will be the only fi

2条回答
  •  暖寄归人
    2021-01-07 18:41

    AFAIK it is not possible to create the file and the directory directly with QFile. You have to first create the directory (QDir::mkpath will create the full path) and then the file (QFile::open).

    QString path("SomePath/NewDirectoryA/");
    QDir dir; // Initialize to the desired dir if 'path' is relative
              // By default the program's working directory "." is used.
    
    // We create the directory if needed
    if (!dir.exists(path))
        dir.mkpath(path); // You can check the success if needed
    
    QFile file(path + "NewFile.kml");
    file.open(QIODevice::WriteOnly); // Or QIODevice::ReadWrite
    

提交回复
热议问题