Qt文件操作之——QFileDialog保存文件

    QFileDialog m_QFileDialog;

    //!-- 得到原文件的后缀名
    QString suffix_save = QFileInfo(FilePathName).suffix();
    //!--- 设置过滤匹配文件格式
    QString setFilter = "*.doc;;*.docx;;*.jpg;;*.bmp;;*.xml;;*.txt;;*.xls;;*.pdf;;*.xlsx;;*.ppt;;*.pptx;;*.*;;";
    //!--- selectFilter为弹出对话框中的文件格式,dirString为文件路径,这里显示文件名
    QString selectFilter,dirString;

    if( FilePathName.isEmpty() ) return;
    else dirString = QFileInfo(FilePathName).fileName();

    //!--- 判断文件后缀名以及文件格式,看是否匹配,没有做出相应操作
    if( !suffix_save.isEmpty() && setFilter.contains( selectFilter ) ) selectFilter = suffix_save.insert(0,"*.");
    if( !suffix_save.isEmpty() && !setFilter.contains( selectFilter ) ) selectFilter = "*.*";

    QString saveFileName = m_QFileDialog.getSaveFileName(this,"保存文件",dirString,setFilter,&selectFilter,
                              QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);

    if( saveFileName.isEmpty() ) return;

    QFile file(saveFileName);

    if( selectFilter.compare("*.*") ) saveFileName = saveFileName+selectFilter.remove(0,1);

    //!--- 复制文件
    bool copy_error =  file.copy( FilePathName,saveFileName );

    Q_UNUSED(copy_error);


你可能感兴趣的:(qt,QFileDialog保存文件,getSaveFileName)