52 lines
1.2 KiB
C++
52 lines
1.2 KiB
C++
#include "filebrowserwidget.h"
|
|
|
|
#include <QTreeView>
|
|
#include <QVBoxLayout>
|
|
#include <QDir>
|
|
|
|
FileBrowserWidget::FileBrowserWidget(QWidget *parent)
|
|
: QWidget(parent)
|
|
{
|
|
setupUi();
|
|
connectSignals();
|
|
|
|
// 默认浏览当前工作目录
|
|
setRootPath(QDir::currentPath());
|
|
}
|
|
|
|
void FileBrowserWidget::setupUi()
|
|
{
|
|
m_model = new QFileSystemModel(this);
|
|
m_model->setRootPath(QString()); // 稍后在 setRootPath 里设置
|
|
|
|
m_view = new QTreeView(this);
|
|
m_view->setModel(m_model);
|
|
m_view->setHeaderHidden(true);
|
|
m_view->setAnimated(true);
|
|
|
|
auto *layout = new QVBoxLayout(this);
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
layout->addWidget(m_view);
|
|
setLayout(layout);
|
|
}
|
|
|
|
void FileBrowserWidget::connectSignals()
|
|
{
|
|
connect(m_view, &QTreeView::doubleClicked,
|
|
this, [this](const QModelIndex &index) {
|
|
if (!m_model)
|
|
return;
|
|
QString path = m_model->filePath(index);
|
|
if (QFileInfo(path).isFile())
|
|
emit fileOpenRequested(path);
|
|
});
|
|
}
|
|
|
|
void FileBrowserWidget::setRootPath(const QString &path)
|
|
{
|
|
if (!m_model)
|
|
return;
|
|
QModelIndex idx = m_model->setRootPath(path);
|
|
m_view->setRootIndex(idx);
|
|
}
|