文件相关操作¶
1 得到文件下所有文件和其路径¶
bool FindFile(const QString &path);
bool FormConnect::FindFile(const QString &path) {
QDir dir(path);
if (!dir.exists()) {
return false;
}
dir.setFilter(QDir::Dirs | QDir::Files);
dir.setSorting(QDir::DirsFirst);
QFileInfoList list = dir.entryInfoList();
int i = 0;
do {
QFileInfo fileInfo = list.at(i);
if (fileInfo.fileName() == "." | fileInfo.fileName() == "..") {
i++;
continue;
}
bool bisDir = fileInfo.isDir();
if (bisDir) {
FindFile(fileInfo.filePath());
} else {
blog_file_paths_ << fileInfo.path();
blog_file_names_ << fileInfo.fileName();
qDebug() << QString(" %2 %3 ")
.arg(fileInfo.path())
.arg(fileInfo.fileName());
nFiles++;
}
i++;
} while (i < list.size());
return true;
}
2 获取保存的文件¶
QString QUIHelper::getSaveName(const QString &filter, QString defaultDir) {
return QFileDialog::getSaveFileName(0, "选择文件", defaultDir, filter);
}
3 获取选择的文件¶
QString QUIHelper::getFileName(const QString &filter, QString defaultDir) {
return QFileDialog::getOpenFileName(0, "选择文件", defaultDir, filter);
}
4 取选择的文件集合¶
QStringList QUIHelper::getFileNames(const QString &filter, QString defaultDir) {
return QFileDialog::getOpenFileNames(0, "选择文件", defaultDir, filter);
}
5 获取选择的目录¶
6 获取文件名,含拓展名¶
QString QUIHelper::getFileNameWithExtension(const QString &strFilePath) {
QFileInfo fileInfo(strFilePath);
return fileInfo.fileName();
}
7 获取选择文件夹中的文件¶
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;
}
8 文件夹是否存在¶
bool QUIHelper::folderIsExist(const QString &strFolder) {
QDir tempFolder(strFolder);
return tempFolder.exists();
}
9 文件是否存在¶
bool QUIHelper::fileIsExist(const QString &strFile) {
QFile tempFile(strFile);
return tempFile.exists();
}
10 复制文件¶
bool QUIHelper::copyFile(const QString &sourceFile, const QString &targetFile) {
bool ok;
ok = QFile::copy(sourceFile, targetFile);
//将复制过去的文件只读属性取消
ok = QFile::setPermissions(targetFile, QFile::WriteOwner);
return ok;
}
11 删除文件夹下所有文件¶
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());
}
}
}
12 Qt 实现 打开 文件 文件夹 同一个接口¶
打开 文件/文件夹 需要两个函数分别实现
//-----------------------------------------------
void SLot_OpenDicomFolder() {
QSettings s;
QString p = s.value(OPEN_DIR_PATH, ".").toString();
p = QFileDialog::getExistingDirectory(this, tr("Open dicom directory"), p);
if (!p.isEmpty()) {
s.setValue(OPEN_DIR_PATH, p);
ui->thumbnailBar->setImagePaths(QStringList() << p);
}
}
//-----------------------------------------------
void Slot_OpenDicomFile() {
QSettings s;
QString p = s.value(OPEN_FILE_PATH).toString();
QStringList fs = QFileDialog::getOpenFileNames(this, tr("Open dicom files"), p);
if (!fs.isEmpty()) {
s.setValue(OPEN_DIR_PATH, fs.first());
ui->thumbnailBar->setImagePaths(fs);
}
}
把每一个 **path**当成目录,如果不存在就是文件,存在的话向下遍历。
void appendImagePaths(const QStringList &paths, bool clear_old) {
QStringList path_list = paths;
QStringList files;
QStringList unloaded_files;
while (!path_list.isEmpty()) {
QString p = path_list.takeFirst();
QDir dir(p);
if (dir.exists()) {
QStringList subs = dir.entryList(QDir::Files);
foreach (const QString &s, subs) {
files += p + QDir::separator() + s;
}
subs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
foreach (const QString &s, subs) {
path_list += p + QDir::separator() + s;
}
} else {
files += p;
}
}
if (clear_old) {
unloaded_files = files;
clear();
} else {
foreach (const QString &file, files) {
bool found = false;
foreach (DicomImageLabel *label, imageLabelList) {
if (label->HasImage(file)) {
found = true;
break;
}
}
if (!found) {
unloaded_files << file;
}
}
}
ImageLoadThread *t = new ImageLoadThread(unloaded_files);
connect(t, &ImageLoadThread::resultReady,
this, &ThumbnailBarWidget::Slot_ImageReady);
connect(t, &FileWatcherThread::finished,
t, &FileWatcherThread::deleteLater);
t->start();
}