- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试对项目的第一部分进行防错,其中在指定的基本目录中创建了一个文件夹。基本目录由用户在 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 中的逻辑表明,如果输入为空或预定义字符串,则测试目录是否存在。这意味着您始终使用空字符串或固定消息进行测试。
正确的逻辑应该是:
if( base_dir!=no_text1 && base_dir!=blank ) {
....
关于c++ - Qdir 的函数 exists() 总是返回 true,即使目录不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6296757/
我正在为我的程序编写安装脚本,它应该在 Linux/Unix 操作系统上运行。以下文件的默认目录是什么: 可执行文件(程序)。程序应通过从命令行键入其名称来执行。 共享库。 第三方共享库(程序未开源,
我有一堆用户、组和应用程序注册,我的 MVC 应用程序使用 AAD 数据进行身份验证和授权。是否可以将 Azure Active Directory 从一个租户(目录)迁移到另一个租户(目录)?如果可
查看 cljsbuild 文档 https://github.com/emezeske/lein-cljsbuild :cljsbuild { :builds [{ ; The
忽略已经版本控制的文件 如果你不小心添加了一些应该被忽略的文件,你如何将它们从版本控制中去除而不会丢失它们?或许你有自己的IDE配置文件,不是项目的一部分,但将会花费很多时间使之按照自己的方式工作。
我想使用\tableofcontents 命令,但没有目录从新页面开始或在末尾创建新页面,并且所有内容都是单倍行距。我怎样才能做到这一点?我假设使用 tocloft,但有哪些选择? 谢谢 最佳答案 试
我有一些 javascript 菜单代码,可以在单独的目录中正常工作。但是,当我尝试从同一目录中调用相同的 .js 文件时,它不会看到这些文件。 以下内容来自另一个目录: script type="t
我有这样的路径: /my/path/to/important_folder 在同一级别上,我还有其他文件和文件夹想要在达到与 important_folder 相同的级别时列出。 我的文件夹可能更深,
1、获取文件路径实现 1.1 获取当前文件路径 ? 1
我正在使用最新版本的 NTEmacs。 我写了一个名为“.dir-locals.el”的文件,如下所示。 ((nil . ((tab-width . 8) (fill-column .
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
在我的 .vimrc 中有这些行 :set foldmethod=marker :set foldmarker=SECTION:,ENDSECTION: 用于自定义代码折叠。在我的文件中,相关语言的注
在 fish 中: for x in * echo $x end *这里包括所有目录和文件,如何只列出文件(或目录)? 最佳答案 fish 没有很多花哨的通配语法。但是,目录可以像这样迭代: f
这是我的目录结构: ├── src │ ├── helpers │ │ ├── __init__.py │ │ ├── foo.py │ │ └── bar.py │
我想递归重命名文件夹/目录名称并找到 this solution所以。但是这个命令没有效果 find . -type f -exec rename 's/old/new/' '{}' \; 这是一个正
我想在相册中创建一个文件夹,并希望将图像保存在创建的相册中。 这可能吗?有什么办法可以做到这一点吗? 我已经搜索过,大多数人都说这是不可能的。 感谢您的帮助。 最佳答案 您也许可以使用AssetsLi
如何在python中使用用户定义的名称创建临时文件/目录。我知道 tempfile .但是我看不到任何以文件名作为参数的函数。 注意:我需要这个来对包含临时文件的临时目录上的 glob(文件名模式匹配
我在项目中使用JaCoCo Gradle插件。 作为问题的一个示例,我的大部分代码都在com.me.mysoftware包下。 我正在使用代码生成器来生成build/generated/java/..
我正在尝试使用 Gradle 开始运行 jar 文件 我的任务如下所示: task startServer(type: Exec) { workingDir file("${buildDir}/a
如何在 Ant 中定义一个目录集,其中包括两个目录:项目的基目录和子目录“test”? 看起来您无法使用“/”、“.”或“”专门包含目录集的根目录。例如,这包括“./test”,但不包括“.”:
我正在使用 CTAGs 包,它使用 Sublime Text 2 生成两个文件 .tags 和 .tags_sorted_by_file。 那么当我进行项目搜索(CMD + SHIFT + F)时,如
我是一名优秀的程序员,十分优秀!