函數名
<code> //獲取保存的文件
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);/<code>
函數體
<code>QString QUIHelper::getSaveName(const QString &filter, QString defaultDir)
{
return QFileDialog::getSaveFileName(0, "選擇文件", defaultDir, filter);
}
QString QUIHelper::getFileName(const QString &filter, QString defaultDir)
{
return QFileDialog::getOpenFileName(0, "選擇文件", defaultDir, filter);
}
QString QUIHelper::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 QUIHelper::getFileNames(const QString &filter, QString defaultDir)
{
return QFileDialog::getOpenFileNames(0, "選擇文件", defaultDir, filter);
}
QString QUIHelper::getFolderName()
{
return QFileDialog::getExistingDirectory();
}
QString QUIHelper::getFileNameWithExtension(const QString &strFilePath)
{
QFileInfo fileInfo(strFilePath);
return fileInfo.fileName();
}
QStringList QUIHelper::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 QUIHelper::folderIsExist(const QString &strFolder)
{
QDir tempFolder(strFolder);
return tempFolder.exists();
}
bool QUIHelper::fileIsExist(const QString &strFile)
{
QFile tempFile(strFile);
return tempFile.exists();
}
bool QUIHelper::copyFile(const QString &sourceFile, const QString &targetFile)
{
bool ok;
ok = QFile::copy(sourceFile, targetFile);
//將複製過去的文件只讀屬性取消
ok = QFile::setPermissions(targetFile, QFile::WriteOwner);
return ok;
}
void QUIHelper::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());
}
}
}/<code>
閱讀更多 Qt自定義控件 的文章