QFileSystemModel+QTreeView如何让目录树只显示本地磁盘,不显示外部存储设备?

lwei2 2023-05-08 17:42:24

如题,QFileSystemModel+QTreeView如何实现只显示本地磁盘,不显示外部存储设备的目录树。各位大佬们,具体代码如下
 

FrmMain::FrmMain(QWidget *parent) : QMainWindow(parent)
{
    m_pTreeView = new QTreeView(this);
    dir = new QFileSystemModel(this);
    dir->setReadOnly(false);
    dir->setRootPath(QDir::currentPath());
    m_usbModel = new USBDriveFilterProxyModel(this);
    m_usbModel->setSourceModel(dir);
    m_usbModel->setDynamicSortFilter(true);
    m_pTreeView->setModel(m_usbModel);
    m_pTreeView->header()->setStretchLastSection(true);
    m_pTreeView->header()->setSortIndicatorShown(true);
    m_pTreeView->setHeaderHidden(true);
    m_pTreeView->hideColumn(1);
    m_pTreeView->hideColumn(2);
    m_pTreeView->hideColumn(3);
    m_pTreeView->sortByColumn(0, Qt::DescendingOrder);
    m_pTreeView->collapseAll();
    m_pTreeView->resizeColumnToContents(0);
    this->setCentralWidget(m_pTreeView);
}


#include "usbdrivefilter.h"
#include <windows.h>
USBDriveFilterProxyModel::USBDriveFilterProxyModel(QObject *parent) :
    QSortFilterProxyModel(parent)
{
    getMountedRemovables();
    // will eventually also register for changes to mounted removables
    // but need to get passed my current issue of not displaying only USBs.
}
bool USBDriveFilterProxyModel::filterAcceptsRow(int sourceRow,
                                                const QModelIndex &sourceParent) const
{
    QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
    // Since drive string can have more than just "<DriveLetter>:", need
    // to check each entry in the usb list for whether it is contained in
    // the current drive string.
    for (int i = 0; i < drivers.size(); i++)
    {
        if (sourceModel()->data(index0).toString().contains(drivers[i]))
        {
            return true;
        }
    }
    return false;
}
void USBDriveFilterProxyModel::getMountedRemovables()
{
    DWORD test = GetLogicalDrives();
    DWORD mask = 1;
    UINT type = 0;
    WCHAR wdrive[] = L"C:\\"; // use as a drive letter template
    for (int i = 0; i < 32; i++)
    {
        if (test & mask)
        {
            wdrive[0] = (char)('A' + i); // change letter in template
            type = GetDriveType(wdrive);
            switch (type) {
            case DRIVE_REMOVABLE:
            {
                QString qdrive = QString((char)('A' + i)) + ":";
                removables.append(qdrive);
                break;
            }
            case DRIVE_FIXED:
            {
                QString qdrive = QString((char)('A' + i)) + ":";
                drivers.append(qdrive);
            }
                break;
            default:
                break;
            }
        }
        mask = mask << 1;
    }
}

#include <QApplication>
#include "frmmain.h"
int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    FrmMain w;
    w.resize(500, 500);
    w.show();
    return app.exec();
}


 

...全文
96 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

16,249

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧