#ifndef FOLDERFINDER_H
#define FOLDERFINDER_H
#include <QDir>
class FolderFinder
{
public:
QString absoluteFolderPath; //要转换的绝对路径
QString FolderPath; //要定位的文件夹名称
FolderFinder(const QString & filePath,const QString & folderPath)
{
FolderPath=folderPath;
RecursiveFolder(filePath);
}
private:
QString getAbsolutePathFrom(const QString & filePath)
{
QDir tempInfo;
QString absoluteFilePath=tempInfo.absoluteFilePath(filePath).replace("/./","/"); //获取绝对路径
return absoluteFilePath;
}
void RecursiveFolder(const QString & path)
{
QDir dir(path);
// dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
// dir.setSorting(QDir::Size | QDir::Reversed);
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
if(fileInfo.fileName()=="." || fileInfo.fileName()=="..")
{
i++;
continue;
}
if(fileInfo.isDir())
{
if(fileInfo.fileName()==FolderPath)
{
absoluteFolderPath=getAbsolutePathFrom(fileInfo.filePath());
break;
}
RecursiveFolder(fileInfo.filePath());
}
}
}
};
#endif // FOLDERFINDER_H
使用方法
FolderFinder folder("C:\\qtconsoletest-build-desktop","Politics"); //初始递归路径
cout<<qPrintable(QString("%1").arg(folder.absoluteFolderPath));
###
QStringList items; items << "All" << "Weekend Plus" ; ui->listWidgetTimeSet->addItems(items);
QListWidgetItem* all_item = new QListWidgetItem( "All" );
QListWidgetItem* wp_item = new QListWidgetItem( "Weekend Plus" );
listWidgetTimeSet->addItem( all_item );
listWidgetTimeSet->addItem( wp_item ); listWidgetTimeSet->setCurrentItem( wp_item );