以下内容适用于QWidget应用程序项目
QFileHelper.h
#ifndef QFileHelper_H
#define QFileHelper_H
#include <QObject>
#include <QGuiApplication>
class QFileHelper : public QObject
{
Q_OBJECT
public:
explicit QFileHelper(QObject *parent = nullptr);
static QString getSaveName(const QString &filter, QString defaultDir = QCoreApplication::applicationDirPath());
static QString getFileName(const QString &filter, QString defaultDir = QCoreApplication::applicationDirPath());
static QString saveFileName(const QString &filter, const QString &defaultDir = "", const QString &fileName = "");
static QStringList getFileNames(const QString &filter, QString defaultDir = QCoreApplication::applicationDirPath());
static QString getFolderName();
static QString getFileNameWithExtension(const QString &strFilePath);
static QStringList getFolderFileNames(const QStringList &filter);
static bool folderIsExist(const QString &strFolder);
static bool fileIsExist(const QString &strFile);
static bool copyFile(const QString &sourceFile, const QString &targetFile);
static void deleteDirectory(const QString &path);
signals:
};
#endif
QFileHelper.cpp
#include "qfilehelper.h"
#include <QFileDialog>
QFileHelper::QFileHelper(QObject *parent) : QObject(parent)
{
}
QString QFileHelper::getSaveName(const QString &filter, QString defaultDir)
{
return QFileDialog::getSaveFileName(0, "选择文件", defaultDir, filter);
}
QString QFileHelper::getFileName(const QString &filter, QString defaultDir)
{
return QFileDialog::getOpenFileName(0, "选择文件", defaultDir, filter);
}
QString QFileHelper::saveFileName(const QString &filter, const QString &defaultDir, const QString &fileName)
{
QString file;
QFileDialog dialog;
dialog.setFixedSize(900, 600);
dialog.setWindowModality(Qt::WindowModal);
dialog.setWindowTitle("保存文件");
dialog.setLabelText(QFileDialog::Accept, "保存(&S)");
dialog.setLabelText(QFileDialog::Reject, "取消(&C)");
dialog.selectFile(fileName);
dialog.setNameFilter(filter);
dialog.setDirectory(defaultDir);
if (dialog.exec() == 1) {
file = dialog.selectedFiles().value(0);
file = QFileInfo(file).suffix().isEmpty() ? "" : file;
}
return file;
}
QStringList QFileHelper::getFileNames(const QString &filter, QString defaultDir)
{
return QFileDialog::getOpenFileNames(0, "选择文件", defaultDir, filter);
}
QString QFileHelper::getFolderName()
{
return QFileDialog::getExistingDirectory();
}
QString QFileHelper::getFileNameWithExtension(const QString &strFilePath)
{
QFileInfo fileInfo(strFilePath);
return fileInfo.fileName();
}
QStringList QFileHelper::getFolderFileNames(const QStringList &filter)
{
QStringList fileList;
QString strFolder = QFileDialog::getExistingDirectory();
if (!strFolder.length() == 0) {
QDir myFolder(strFolder);
if (myFolder.exists()) {
fileList = myFolder.entryList(filter);
}
}
return fileList;
}
bool QFileHelper::folderIsExist(const QString &strFolder)
{
QDir tempFolder(strFolder);
return tempFolder.exists();
}
bool QFileHelper::fileIsExist(const QString &strFile)
{
QFile tempFile(strFile);
return tempFile.exists();
}
bool QFileHelper::copyFile(const QString &sourceFile, const QString &targetFile)
{
bool ok;
ok = QFile::copy(sourceFile, targetFile);
ok = QFile::setPermissions(targetFile, QFile::WriteOwner);
return ok;
}
void QFileHelper::deleteDirectory(const QString &path)
{
QDir dir(path);
if (!dir.exists()) {
return;
}
dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
QFileInfoList fileList = dir.entryInfoList();
foreach (QFileInfo fi, fileList) {
if (fi.isFile()) {
fi.dir().remove(fi.fileName());
} else {
deleteDirectory(fi.absoluteFilePath());
dir.rmdir(fi.absoluteFilePath());
}
}
}