目录
QFileSystemModel *model = new QFileSystemModel;model->setRootPath(QDir::currentPath());//静态函数QDir::currentPath()获取应用程序的当前路径。
QFileSystemModel? *model;
MainWindow::MainWindow(QWidget *parent) :
????QMainWindow(parent),
????ui(new Ui::MainWindow)
{
????ui->setupUi(this);
? //????setCentralWidget(ui->splitterMain);??//设置主布局,填充满窗口
? //????model=new QDirModel(this);//QDirModel无单独线程
????model=new QFileSystemModel(this); //QFileSystemModel提供单独线程,推荐使用
????model->setRootPath(QDir::currentPath()); //设置根目录
????ui->treeView->setModel(model); //设置数据模型
????ui->listView->setModel(model); //设置数据模型
????ui->tableView->setModel(model); //设置数据模型
//信号与槽关联,treeView单击时,其目录设置为listView和tableView的根节点
????connect(ui->treeView,SIGNAL(clicked(QModelIndex)),
????????????ui->listView,SLOT(setRootIndex(QModelIndex)));
????connect(ui->treeView,SIGNAL(clicked(QModelIndex)),
????????????ui->tableView,SLOT(setRootIndex(QModelIndex)));
}
void MainWindow::on_treeView_clicked(const QModelIndex &index)
{
????ui->chkIsDir->setChecked(model->isDir(index));
????ui->LabPath->setText(model->filePath(index));
????ui->LabType->setText(model->type(index));
????ui->LabFileName->setText(model->fileName(index));
????int sz=model->size(index)/1024;
????if (sz<1024)
????????ui->LabFileSize->setText(QString("%1 KB").arg(sz));
????else
????????ui->LabFileSize->setText(QString::asprintf("%.1f MB",sz/1024.0));
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
????QApplication a(argc, argv);
????MainWindow w;
????w.show();
????return a.exec();
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
//#include????<QDirModel>
#include????<QFileSystemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
????Q_OBJECT
private:
//????QDirModel????*model;
????QFileSystemModel????*model; //定义数据模型变量
public:
????explicit MainWindow(QWidget *parent = 0);
????~MainWindow();
private slots:
//????void on_treeView_doubleClicked(const QModelIndex &index);
????void on_treeView_clicked(const QModelIndex &index);
private:
????Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
????QMainWindow(parent),
????ui(new Ui::MainWindow)
{
????ui->setupUi(this);
//????setCentralWidget(ui->splitterMain);??//设置主布局,填充满窗口
//????model=new QDirModel(this);//QDirModel无单独线程
????model=new QFileSystemModel(this); //QFileSystemModel提供单独线程,推荐使用
????model->setRootPath(QDir::currentPath()); //设置根目录
????ui->treeView->setModel(model); //设置数据模型
????ui->listView->setModel(model); //设置数据模型
????ui->tableView->setModel(model); //设置数据模型
//信号与槽关联,treeView单击时,其目录设置为listView和tableView的根节点
????connect(ui->treeView,SIGNAL(clicked(QModelIndex)),
????????????ui->listView,SLOT(setRootIndex(QModelIndex)));
????connect(ui->treeView,SIGNAL(clicked(QModelIndex)),
????????????ui->tableView,SLOT(setRootIndex(QModelIndex)));
}
MainWindow::~MainWindow()
{
????delete ui;
}
void MainWindow::on_treeView_clicked(const QModelIndex &index)
{
????ui->chkIsDir->setChecked(model->isDir(index));
????ui->LabPath->setText(model->filePath(index));
????ui->LabType->setText(model->type(index));
????ui->LabFileName->setText(model->fileName(index));
????int sz=model->size(index)/1024;
????if (sz<1024)
????????ui->LabFileSize->setText(QString("%1 KB").arg(sz));
????else
????????ui->LabFileSize->setText(QString::asprintf("%.1f MB",sz/1024.0));
}