Qt中如何获得文件和文件夹的路径信息

 一、添加头文件

#include 
#include 
#include 

二、具体操作

void MainWindow::ShowFilePath()
{
    ui->textEdit->append("请选择文件夹路径...");
    QString FileDirPath = QFileDialog::getExistingDirectory(this,"请选择文件夹路径...","./");
    if(FileDirPath.isEmpty()) return;
    ui->textEdit->append(FileDirPath);
    QString filefull;
    QString filename;
    QString filepath;
    QString filesuffix ;
    QFileInfo fileinfo;
    ui->textEdit->append("请选择文件路径...");
    file_full = QFileDialog::getOpenFileName(
                this,
                "",
                ".",//. 代表程序运行目录 /代表当前盼复的根目录 打开程序运行目录下的Data文件夹作为默认打开路径,这里应该写成"./Data/",
                "Python Files(*.py)",
                0,
                0);

    fileinfo = QFileInfo(file_full);

    //文件名
    filename = fileinfo.fileName();

    //文件后缀
    filesuffix = fileinfo.suffix();

    //绝对路径
    filepath = fileinfo.absolutePath();

    ui->textEdit->append("文件名...");
    ui->textEdit->append(file_name);
    ui->textEdit->append("文件后缀...");
    ui->textEdit->append(file_suffix);
    ui->textEdit->append("绝对路径...");
    ui->textEdit->append(file_path);
}

 

你可能感兴趣的:(QtCreator)