一、Qt创建文件夹
1、按照指定目录创建文件夹
QString dir_path = "./temp";
if(!QDir(dir_path).exists()){
QDir().mkdir(dir_path);
}
2、在应用程序工作目录创建文件夹
QString app_path = QCoreApplication::applicationDirPath();
QString new_dir_path = app_path.append("/temp");
if(!QDir(new_dir_path).exists()){
QDir().mkdir(dir_path);
}
3、在用户目录下创建文件夹
QString user_path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
QString new_dir_path = user_path.append("/temp");
if(!QDir(new_dir_path).exists()){
QDir().mkdir(dir_path);
}
二、Qt创建文件到选择的目录
1、使用QFileDialog选择目录并创建文件
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open file"), "/", "Text files (*.txt)");
foreach(QString file, fileNames)
{
QFile myFile(file);
if(!myFile.open(QIODevice::ReadWrite | QIODevice::Text))
continue;
QTextStream out(&myFile);
out << "This is a test file." << endl;
myFile.close();
}
2、使用QLineEdit指定目录并创建文件
QString filePath = ui->lineEdit->text();
QFile file(filePath);
if(file.open(QIODevice::WriteOnly)){
QTextStream stream(&file);
stream << "Qt create file success!" << endl;
file.close();
}
else{
qDebug() << "Qt create file error.";
}
3、使用QFileDevice指定目录并创建文件
QString filePath = "./temp/test.txt";
QFileDevice::Permissions permission = QFileDevice::WriteUser | QFileDevice::ReadUser | QFileDevice::WriteGroup | QFileDevice::ReadGroup | QFileDevice::WriteOther | QFileDevice::ReadOther;
QFile file(filePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Append))
{
QTextStream stream(&file);
stream << "Qt create file success!" << endl;
file.close();
}
else
{
qDebug() << "Qt create file error.";
}
file.setPermissions(permission);
三、Qt创建文件txt
1、使用QFile创建txt文件
QString filePath = "./temp/test.txt";
QFile file(filePath);
if(file.open(QIODevice::WriteOnly)){
QTextStream stream(&file);
stream << "Qt create file success!" << endl;
file.close();
}
else{
qDebug() << "Qt create file error.";
}
2、使用QTextDocument创建txt文件
QString filePath = "./temp/test.txt";
QFile file(filePath);
if(file.open(QIODevice::WriteOnly)){
QTextDocument doc;
doc.setPlainText("Qt create file success!");
QTextStream stream(&file);
stream << doc.toPlainText();
file.close();
}
else{
qDebug() << "Qt create file error.";
}
3、使用QIODevice创建txt文件
QString filePath = "./temp/test.txt";
QFileDevice::Permissions permission = QFileDevice::WriteUser | QFileDevice::ReadUser | QFileDevice::WriteGroup | QFileDevice::ReadGroup | QFileDevice::WriteOther | QFileDevice::ReadOther;
QFile file(filePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Append))
{
QTextStream stream(&file);
stream << "Qt create file success!" << endl;
file.close();
}
else
{
qDebug() << "Qt create file error.";
}
file.setPermissions(permission);
四、Qt保存文件对话框
使用QFileDialog打开保存文件对话框并保存文件
QString saveFilePath = QFileDialog::getSaveFileName(this, tr("Save file"), "./temp/test.txt");
if(saveFilePath.isEmpty()){
qDebug() << "Save file path is empty.";
}
else{
QFile file(saveFilePath);
if(!file.open(QIODevice::ReadWrite | QIODevice::Text)){
qDebug() << "Open save file failed.";
}
else{
QTextStream out(&file);
out << "Qt save file success." << endl;
file.close();
}
}
五、Qt创建文件夹失败
判断文件夹是否存在,不存在则创建
QString dir_path = "./temp";
if(!QDir(dir_path).exists()){
if(QDir().mkdir(dir_path)){
qDebug() << "Create directory success.";
}
else{
qDebug() << "Create directory failed.";
}
}
六、Qt创建一个文件.jpg
使用QFile创建jpg文件,然后写入数据
QString filePath = "./temp/test.jpg";
QFile file(filePath);
if(file.open(QIODevice::WriteOnly)){
QDataStream out(&file);
QImage image("./1.png");
QByteArray bts;
QBuffer buf(&bts);
image.save(&buf, "PNG");
out.writeRawData(bts.data(), bts.size());
file.close();
}
else{
qDebug() << "Qt create file error.";
}
七、Qt创建文件夹函数
以下是一个通用的创建文件夹的函数
void createDir(QString dirPath)
{
QDir dir(dirPath);
if (!dir.exists())
{
bool ok = dir.mkpath(dirPath);
if (!ok)
{
qDebug()<< QString("Creat folder Failed: %1").arg(dirPath);
return;
}
qDebug()<< "Creat folder Success:" << dirPath;
}
}
八、Qt创建文件对话框
使用QFileDialog打开文件创建对话框
QString filePath = QFileDialog::getSaveFileName(this, tr("Save File"), "./temp/test.txt");
if(filePath.isEmpty()){
qDebug() << "Save file path is empty.";
}
else{
QFile file(filePath);
if(!file.open(QIODevice::ReadWrite | QIODevice::Text)){
qDebug() << "Open save file failed.";
}
else{
QTextStream out(&file);
out << "Qt create file success." << endl;
file.close();
}
}
九、Qt创建文件并写入
创建文件并写入内容
QString filePath = "./temp/test.txt";
QFile file(filePath);
if(file.open(QIODevice::WriteOnly)){
QTextStream stream(&file);
stream << "Qt create file success!" << endl;
file.close();
}
else{
qDebug() << "Qt create file error.";
}
十、Qt创建文件夹程序
包含GUI的Qt程序可以使用如下代码创建文件夹
void MainWindow::on_pushButton_clicked()
{
QString newDirPath = ui->lineEdit->text();
if(!QDir(newDirPath).exists()){
QDir().mkdir(newDirPath);
QMessageBox::information(this, tr("Information"), tr("Create Directory Success!"));
}
else{
QMessageBox::information(this, tr("Information"), tr("Directory already exists!"));
}
}
通过以上的阐述,相信大家已经学会了如何在Qt中进行文件操作,不仅可以创建文件、创建文件夹,还可以方便地保存文件、打开文件对话框等等,可以让我们更加方便地进行开发。