Windows的资源管理器,大家很熟悉吧,可以放回上一级,查找到下一级………Qt中QDir 类可以实现显示文件系统目录的效果。
笔者的机器环境:Windows8.1
看代码~~
fileview_widget.h
#ifndef FILEVIEWWIDGET_H
class FileViewWidget : public QWidget
{
Q_OBJECT
public:
FileViewWidget(QWidget *parent=0);
//---用双击浏览器中显示的目录进入下一级,或者返回上一级目录。
void showFileInfoList(QFileInfoList list);
public slots:
//--显示当前目录下的所有文件
void slotShow(QDir dir);
//----根据选择显示下一级目录下的文件,
void slotDirShow(QListWidgetItem *Item);
private:
QLineEdit *fileLineEdit;
QListWidget *fileListWidget;
QVBoxLayout *vLayout;
QFileInfoList list;
};
#include "fileview_widget.h"
FileViewWidget::FileViewWidget(QWidget parent/=0*/) :QWidget(parent)
{
//-----实例化对象
fileLineEdit = new QLineEdit("/", this);
fileListWidget = new QListWidget(this);
//---布局
vLayout = new QVBoxLayout(this);
vLayout->addWidget(fileLineEdit);
vLayout->addWidget(fileListWidget);
//--设置对应信号与槽
connect(fileLineEdit, SIGNAL(returnPressed()),
this, SLOT(slotDirShow(QDir)));
connect(fileListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
this, SLOT(slotDirShow(QListWidgetItem*)));
QString rootStr = "/";
QDir rootDir(rootStr);
QStringList stringlist;
stringlist << "*";
list = rootDir.entryInfoList(stringlist);
showFileInfoList(list);
//---显示布局
setLayout(vLayout);
//----设置窗口属性
setWindowTitle("File View");
}
//–显示当前目录下的所有文件
void FileViewWidget::slotShow(QDir dir)
{
QStringList stringList;
stringList << “*”;
QFileInfoList InfoList = dir.entryInfoList(stringList, QDir :: AllEntries, QDir :: DirsFirst);
showFileInfoList(InfoList);
}
//—用双击浏览器中显示的目录进入下一级,或者返回上一级目录。
void FileViewWidget::showFileInfoList(QFileInfoList list)
{
//–清空列表控件
fileListWidget->clear();
//----取出所有项,按照目录,文件方式添加到控件内
for (unsigned int i = 0; i < list.count(); i++)
{
QFileInfo tmpFileInfo = list.at(i);
if (tmpFileInfo.isDir())
{
QIcon icon("dir.png");
QString fileName = tmpFileInfo.fileName();
QListWidgetItem*tmpListWidgetItem = new QListWidgetItem(icon, fileName);
fileListWidget->addItem(tmpListWidgetItem);
}
else
{
QIcon icon("file.png");
QString fileName = tmpFileInfo.fileName();
QListWidgetItem*tmpListWidgetItem = new QListWidgetItem(icon, fileName);
fileListWidget->addItem(tmpListWidgetItem);
}
}
}
//—-根据用户的选择显示下一级目录下的文件,
void FileViewWidget::slotDirShow(QListWidgetItem *Item)
{
//—-保存下一级目录名
QString string = Item->text();
QDir dir;
//—-设置路径为当前目录路径
dir.setPath(fileLineEdit->text());
//—–重新设置路径
dir.cd(string);
//—-更新当前显示路径, 这里获取的是绝对路径
fileLineEdit->setText(dir.absolutePath());
//—显示当前文件目录下的所有文件
slotShow(dir);
}
main.cpp
#pragma once
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FileViewWidget win;
win.show();
return a.exec();
}
下面是运行结果
这里笔者列出关于QDir::Filter 的过滤方式,信息来源:assistant
Constant Value Description
QDir::Dirs 0x001 List directories that match the filters.
QDir::AllDirs 0x400 List all directories; i.e. don't apply the filters to directory names.
QDir::Files 0x002 List files.
QDir::Drives 0x004 List disk drives (ignored under Unix).
QDir::NoSymLinks 0x008 Do not list symbolic links (ignored by operating systems that don't support symbolic links).
QDir::NoDotAndDotDot NoDot | NoDotDot Do not list the special entries "." and "..".
QDir::NoDot 0x2000 Do not list the special entry ".".
QDir::NoDotDot 0x4000 Do not list the special entry "..".
QDir::AllEntries Dirs | Files | Drives List directories, files, drives and symlinks (this does not list broken symlinks unless you specify System).
QDir::Readable 0x010 List files for which the application has read access. The Readable value needs to be combined with Dirs or Files.
QDir::Writable 0x020 List files for which the application has write access. The Writable value needs to be combined with Dirs or Files.
QDir::Executable 0x040 List files for which the application has execute access. The Executable value needs to be combined with Dirs or Files.
QDir::Modified 0x080 Only list files that have been modified (ignored on Unix).
QDir::Hidden 0x100 List hidden files (on Unix, files starting with a ".").
QDir::System 0x200 List system files (on Unix, FIFOs, sockets and device files are included; on Windows, .lnk files are included)
QDir::CaseSensitive 0x800 The filter should be case sensitive.
转载自:http://blog.csdn.net/hk_5788/article/details/42435787