FileViewr.h
#ifndef FILEVIEWR_H #define FILEVIEWR_H #include <QtGui> #include "ui_fileviewr.h" class FileViewr : public QDialog { Q_OBJECT public: FileViewr(QWidget *parent = 0, Qt::WFlags flags = 0); ~FileViewr(); private: Ui::FileViewrClass ui; QLineEdit *LineEditDir; QListWidget *ListWidaetFile; void showFileInfoList(QFileInfoList list); private slots: void slotShow(QDir dir); void slotShowDir(QListWidgetItem *item); }; #endif // FILEVIEWR_H
FileViewr.cpp
#include "fileviewr.h" FileViewr::FileViewr(QWidget *parent, Qt::WFlags flags) : QDialog(parent, flags) { ui.setupUi(this); setWindowTitle(tr("File views")); LineEditDir=new QLineEdit(); LineEditDir->setText("/"); ListWidaetFile=new QListWidget(); QVBoxLayout *layout=new QVBoxLayout(this); layout->addWidget(LineEditDir); layout->addWidget(ListWidaetFile); connect(LineEditDir,SIGNAL(returnPressed()),this,SLOT(slotShow())); connect(ListWidaetFile,SIGNAL(itemDoubleClicked(QListWidgetItem *)),this,SLOT(slotShowDir(QListWidgetItem *))); QString root="/"; QDir rootDir(root); QStringList string; string<<"*"; QFileInfoList list=rootDir.entryInfoList(string); showFileInfoList(list); } FileViewr::~FileViewr() { } /* * slotShow 负责显示dir目录下的所有文件 **/ void FileViewr::slotShow(QDir dir){ QStringList string; string <<"*"; QFileInfoList list=dir.entryInfoList (string,QDir::AllEntries,QDir::DirsFirst); showFileInfoList(list); } void FileViewr::slotShowDir(QListWidgetItem *item){ QString str=item->text(); QDir dir; dir.setPath(LineEditDir->text()); dir.cd(str); LineEditDir->setText(dir.absolutePath()); slotShow(dir); } void FileViewr::showFileInfoList(QFileInfoList list){ ListWidaetFile->clear(); for(unsigned int i=0;i<list.count();i++){ QFileInfo temFileinfo=list.at(i); if(temFileinfo.isDir()){ QIcon icon(":/images/dir.png"); QString fileName=temFileinfo.fileName(); QListWidgetItem *tmp=new QListWidgetItem(icon,fileName); ListWidaetFile->addItem(tmp); }else{ QIcon icon(":/images/file.png"); QString fileName=temFileinfo.fileName(); QListWidgetItem *tmp=new QListWidgetItem(icon,fileName); ListWidaetFile->addItem(tmp); } } }
main.cpp
#include "fileviewr.h" #include <QtGui/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); FileViewr *w=new FileViewr; w->show(); return a.exec(); }