StandardManageView.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "StandardManageView.h"
  2. #include <QFileIconProvider>
  3. #include <QResizeEvent>
  4. #include <QVBoxLayout>
  5. #include <QDesktopServices>
  6. StandardManageView::StandardManageView(QWidget *parent) : QWidget(parent)
  7. {
  8. QVBoxLayout *vLayout = new QVBoxLayout;
  9. m_dir = "D:\\FireFly\\QFD2\\文档";
  10. m_fileModel = new QFileSystemModel();
  11. m_fileModel->setRootPath("D:/");
  12. QStringList nameFilter = { "*.doc", "*.docx", "*.xml", "*.xlsx", "*.xls" };
  13. m_fileModel->setNameFilterDisables(false);
  14. m_fileModel->setNameFilters(nameFilter);
  15. m_fileModel->iconProvider()->setOptions(QFileIconProvider::DontUseCustomDirectoryIcons);
  16. m_treeView = new QTreeView(this);
  17. m_treeView->setModel(m_fileModel);
  18. m_treeView->setRootIndex(m_fileModel->index(m_dir));
  19. vLayout->addWidget(m_treeView);
  20. // Demonstrating look and feel features
  21. m_treeView->setAnimated(false);
  22. m_treeView->setIndentation(20);
  23. m_treeView->setSortingEnabled(true);
  24. CDelegate *pDelegate = new CDelegate(this);
  25. m_treeView->setItemDelegate(pDelegate);
  26. connect(m_treeView, &QTreeView::doubleClicked, this, &StandardManageView::onDoubleClick);
  27. setLayout(vLayout);
  28. }
  29. void StandardManageView::resizeEvent(QResizeEvent *event)
  30. {
  31. m_treeView->setColumnWidth(0, this->width() / 3);
  32. }
  33. void StandardManageView::onDoubleClick(const QModelIndex &index)
  34. {
  35. if (index.row() < 0 || index.column() < 0) {
  36. return;
  37. }
  38. QString filePath = m_fileModel->filePath(index);
  39. // qDebug() << filePath;
  40. if (!filePath.isEmpty()) {
  41. QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
  42. }
  43. }