获取文件路径和文件名、后缀名

时间:2022-05-21 19:24:29
1. 打开文件获取文件路径+文件名
    QString filePath = QFileDialog::getOpenFileName(this,tr("请选择文件"),"C://Users//Administrator//Desktop");
    qDebug() << filePath;

2. 打开多个文件获取文件路径列表
    //QStringList filePathList = QFileDialog::getOpenFileNames(this,tr("请选择多个文件"),"C://Users//Administrator//Desktop");

3. 从路径获取文件名(有后缀)
    QFileInfo fileinfo = QFileInfo(filePath);
    QString filename = fileinfo.fileName();
    qDebug() << filename;
    
4. 获取文件名(无后缀)
    QString filename2 = fileinfo.fileName();
    qDebug() << filename2;

5. 获取文件名的后缀
    QString filefinalName = fileinfo.suffix();
    qDebug() << filefinalName;

6. 获取文件的绝对路径
    QString absolutePath = fileinfo.absolutePath();
    qDebug() << absolutePath;

    /*
     *  输出结果如下:
     *  1. "C:/Users/Administrator/Desktop/mysql.txt"
     *  3. "mysql.txt"
     *  4. "mysql"
     *  5. "txt"
     *  6. "C:/Users/Administrator/Desktop"
    */