Qdir的函数exist()总是返回true,即使目录不存在
Qdir's function exists() always returns true, even when the directory doesn't exist
我正在尝试对项目的第一部分进行防错,其中在指定的基本目录中创建了一个文件夹。基本目录由用户在QLineEdit中手动选择,或使用QFileDialog浏览计算机的目录。在使用以下命令在其中创建子文件夹之前,我正在尝试检查基本目录是否存在:
QString base_dir = ui->baseDir->text();
QString blank = "";
QString no_text1 = "Please enter a valid directory";
if( base_dir==no_text1 || base_dir==blank ) {
if( !QDir( base_dir ).exists() ) { //does base directory exist?
ui->baseDir->setText( no_text1 );
return;
}
}
问题是,无论我在行编辑中键入一个有效的目录,还是一个无效的目录(即随机短语),第二个if语句总是返回false,这意味着exists()总是返回true。第一个if语句工作正常。我只是用exists()错了吗?
编辑:完整代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "functions.h"
#include <QtGui/QApplication>
#include <QFileDialog>
#include <fstream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
// This is the function that handles the directory search when the 'browse' button is pressed
void MainWindow::on_findDir_clicked()
{
QString path; //declaring the path to the base directory
path = QFileDialog::getExistingDirectory( //gathering the directory from QFileDialog class
this, tr("Choose the project directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks );
ui->baseDir->setText( path ); //setting the retrieved path into the line edit box
}
// This function makes the project by creating a new directory with name 'project_name'
// in the base directory, and collects the OpenFOAM version number and simulation type
void MainWindow::on_create_clicked()
{
QString project_name, foam_version, full_dir, slash, base_dir;
base_dir = ui->baseDir->text();
project_name = ui->projectName->text(); //getting the text from the 'projectName' field
foam_version = ui->version->currentText(); //getting the selection from the 'version' drop-down box
//first checking if the fields have input in them:
QString blank = "";
QString no_text0 = "Please enter project name";
if( project_name==no_text0 || (project_name==blank) ) {
ui->projectName->setText( no_text0 );
return;
}
QString no_text1 = "Please enter a valid directory";
if( base_dir==no_text1 || base_dir==blank ) {
if( !QDir( base_dir ).exists() ) { //does base directory exist?
ui->baseDir->setText( no_text1 );
return;
}
}
slash = "/"; // needed to separate folders in the directory (can't use a literal)
full_dir = base_dir.append(slash.append(project_name));
if( !QDir(full_dir).exists() ) //check if directory already exists
QDir().mkdir(full_dir); //creating directory
QString blockmesh_filename, suffix;
suffix = "_blockmesh";
slash = "/"; //must re-define
blockmesh_filename = full_dir.append( slash.append( project_name.append(suffix) ) );
std::ofstream create_file( blockmesh_filename.toStdString().c_str() ); //creating empty blockmesh file
}
如果在文本字段中键入任何内容,则不会执行第二个if语句。第一个if中的逻辑表示,如果输入是空的或预定义的字符串,那么测试dir的存在。这意味着你总是用空字符串或罐装消息进行测试。
正确的逻辑应该是:
if( base_dir!=no_text1 && base_dir!=blank ) {
....
相关文章:
- 我们可以访问一个不存在的联盟的成员吗
- C++:对不存在的命名空间使用命名空间指令
- g++ 说函数不存在,即使包含正确的标头
- 显式 std::exception_ptr 转换为 bool 不存在.VS2010 错误?
- C++ 尝试在不存在的构造函数中引用已删除的函数(使用 rapidJson)
- 查找第一个数组中不存在的元素
- 查找不存在的键时,unordered_map返回什么
- 如何优化代码以返回最接近给定整数的数字,但给定列表中不存在?
- set::find 查找不存在的元素
- 有没有办法将字符串添加到 Vector 中,但前提是它尚不存在?->C++
- inet_ntop返回不存在的地址
- CPP 使用不存在的键访问映射
- 为什么QMediaGaplessPlaybackControl不存在?
- 如果键不存在,使用 [] 运算符访问 STL Map 元素会添加新元素
- 标记未定义的颜色,并且颜色匹配系统中不存在样品
- 为什么minhook库目录不存在
- 为什么 std::vector::p ush_front() 不存在?
- ifstream::is_open 返回 true,即使该位置不存在文件也是如此
- QMediaPlaylist::addMedia() 对于不存在的文件返回 true
- Qdir的函数exist()总是返回true,即使目录不存在