QT中的文件浏览
2012-04-10 19:34 Rollen Holt 阅读(4248) 评论(0) 编辑 收藏 举报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(); }
==============================================================================
本博客已经废弃,不在维护。新博客地址:http://wenchao.ren
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
==============================================================================