初始化:图形化界面骨架(文件选择、语法高亮、自动缩进、查找/替换)
This commit is contained in:
51
ui/filebrowserwidget.cpp
Normal file
51
ui/filebrowserwidget.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user