16,799
社区成员




Widget::Widget(QWidget *parent)
{
QString strRootPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
QFileSystemModel *pFileSystemModel = new QFileSystemModel(this);
pFileSystemModel->setRootPath(strRootPath);
QFileSystemModel *pDirSystemModel = new QFileSystemModel(this);
pDirSystemModel->setRootPath(strRootPath);
pDirSystemModel->setFilter(QDir::AllDirs|QDir::Dirs|QDir::NoDotAndDot);
QSplitter *pSplitter = new QSplitter(this);
m_pTreeView = new QTreeView(pSplitter);
m_pTreeView->setModel(pDirSystemModel);
m_pTreeView->setCurrentIndex(pDirSystemModel->index(strRootPath));
connect(m_pTreeView, &QTreeView::expanded, this, &Widget::changeListViewRootPath);
connect(m_pTreeView, &QTreeView::clicked, this, &Widget::changeListViewRootPath);
connect(m_pTreeView, &QTreeView::doubleClicked, this, &Widget::changeListViewRootPath);
m_pListView = new QListView(pSplitter);
m_pListView->setModel(pFileSystemModel);
m_pListView->setRootIndex(pFileSystemModel->index(strRootPath));
connect(m_pListView, &QListView::doubleClicked, [this]{
if (pFileSystemModel->isDir(index)){
QString strDir = pFileSystemModel->filePath(index);
m_pTreeView->setCurrentIndex(pDirSystemModel->index(strDir));
m_pListView->setRootIndex(index);
}
});
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setWidget(pSplitter);
setLayout(layout);
}
void Widget::changeListViewRootPath(const QModelIndex &index)
{
QString strDir = pDirSystemModel->filePath(index);
m_pListView->setRootIndex(pFileSystemModel->index(strDir));
}