#include "QFileList.h" #include #include #include QFileList::QFileList(QWidget *parent) : QDockWidget(parent) { ui.setupUi(this); this->setWindowTitle(_S("文件列表")); m_pFileModel = new QStandardItemModel(0, 1, ui.m_treeViewFiles); ui.m_treeViewFiles->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu); //设置上下文菜单测量 ui.m_treeViewFiles->setHeaderHidden(true); ui.m_treeViewFiles->setModel(m_pFileModel); ui.m_treeViewFiles->setAlternatingRowColors(true); // 双击事件 connect(ui.m_treeViewFiles, &QTreeView::doubleClicked, this, &QFileList::OnDoubleClicked); // 更新路径 connect(ui.lineEdit, &QLineEdit::textChanged, this, &QFileList::DisplayFileList); // 调用系统打开文件夹 connect(ui.pushButton, &QPushButton::clicked, this, &QFileList::OnOpenDirWithSys); } QFileList::~QFileList() { } void QFileList::OnOpenDirWithSys() { QDesktopServices::openUrl(QUrl::fromLocalFile(m_CurrentPath)); } void QFileList::DisplayFileList(QString path) { QDir varDir(path); if (!varDir.exists()) { return; } varDir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks); QFileInfoList varList = varDir.entryInfoList(); m_vecFileList.clear(); foreach(QFileInfo varInfo, varList) { m_vecFileList.push_back(varInfo.fileName()); } // 过滤文件,保留符合标准的文件 int nPos = 0; foreach(QString str, m_vecFileList) { //str.toLower(); QString es = "raw$|xraw$|rawx$|GD$|avi$|mp4$|yuv|MP4|AVI";//by wcw04046 nPos = str.indexOf(QRegExp(es), 0); if (-1 == nPos) { m_vecFileList.removeOne(str); } } // 将文件显示到文件列表 int nIndex = 0; int cnt = m_pFileModel->rowCount(); m_pFileModel->removeRows(0, cnt); foreach(QString str, m_vecFileList) { m_pFileModel->insertRow(nIndex); m_pFileModel->setData(m_pFileModel->index(nIndex, 0), str); nIndex++; } ui.m_treeViewFiles->resizeColumnToContents(0); ui.m_treeViewFiles->setAlternatingRowColors(false);//by wcw04046 关闭行交替背景,方便区分选中 ui.m_treeViewFiles->setEditTriggers(QAbstractItemView::NoEditTriggers);//关闭编辑 m_CurrentPath = path; //将当前路径设置为打开路径 ui.lineEdit->setText(path); } QString QFileList::GetSelectedFilePath() { //QString strSelectedFileName = QString(tr("")); //if (m_varSelectedModelIndex.row() >= 0 // && m_varSelectedModelIndex.row() < m_vecFileList_Other.size()) //{ //} return m_pFileModel->data(m_varSelectedModelIndex).toString(); } QString QFileList::GetCurrentDir() { return m_CurrentPath; } void QFileList::OnDoubleClicked(const QModelIndex& index) { m_varSelectedModelIndex = index; QDir varDir(m_CurrentPath); if (m_varSelectedModelIndex.isValid()) { QString fileFullPath = varDir.absoluteFilePath(GetSelectedFilePath()); emit(playSelectFile(fileFullPath)); qDebug() << fileFullPath; } }