close
Step1. design UI
t : treeView, l : listView
Lay Out : Horizontally (Ctrl + H)
Step2. dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QtCore>
#include <QtGui>
#include <QDirModel>
#include <QInputDialog>
#include <QFileSystemModel>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = nullptr);
~Dialog();
private slots:
void on_treeView_clicked(const QModelIndex &index);
private:
Ui::Dialog *ui;
QFileSystemModel *dirmodel;
QFileSystemModel *filemodel;
};
#endif // DIALOG_H
Step2. dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QString strPath = "E:/";
dirmodel = new QFileSystemModel(this);
dirmodel->setFilter(QDir::NoDotAndDotDot| QDir::AllDirs);
dirmodel->setRootPath(strPath);
ui->treeView->setModel(dirmodel);
filemodel = new QFileSystemModel(this);
filemodel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
filemodel->setRootPath(strPath);
ui->listView->setModel(filemodel);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_treeView_clicked(const QModelIndex &index)
{
// setup List View
// When the user clicks on a node in the tree view,
// we want to be able to a extract the path out of that
// tree view and be set that path into our ListView
// absolute file path that will give us the full path that the user just clicked on.
QString strPath = dirmodel->fileInfo(index).absoluteFilePath();
ui->listView->setRootIndex(filemodel->setRootPath(strPath));
}
全站熱搜
留言列表