开发者

Qdir's function exists() always returns true, even when the directory doesn't exist

I'm trying to error-proof the first part of a project, in which a folder is created in a specified base directory. The base directory is selected by the user either manually in QLineEdit, or by browsing through the computer's directories using QFileDialog. I'm trying to check if the base directory exists before I make a sub-folder in it using the following commands:

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;
    }
}

The issue is that whether I type a valid directory into the line edit, or an invalid one (ie. a random phrase) the second if statement always returns false, meaning exists() always returns true. The first if statement works properly. Am I just using exists() wrong?

Edit: The full code

#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::ShowDirs开发者_如何转开发Only
        | 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

}


Your second if statement will not be executed if you type in anything in the text field. Your logic in the first if states that if the input is empty or a predefine string, then test for the existence of the dir. Which means you are always testing with an empty string or the canned message.

The correct logic should be:

if( base_dir!=no_text1 && base_dir!=blank ) {
    ....
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜