最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Updating QFileSystemModel when inserting a flash card - Stack Overflow

programmeradmin2浏览0评论

I am using QFileSystemModel together with QTreeView. I want QTreeView to update its state and show the flash card in the tree when I insert a flash drive into the computer.

This only works if you specify model->setRootPath(QDir::rootPath()) where QDir::rootPath() is C:\. If, for example, you use an empty string instead of QDir::rootPath(), then when you insert a flash card, QTreeView will not update its state.

I don't understand this behavior, because the documentation for setRootPath says:

Sets the directory that is being watched by the model to newPath by installing a file system watcher on it. Any changes to files and directories within this directory will be reflected in the model.

Thus, in theory, tracking should only occur for the C:\ drive, but despite this, when a flash drive is inserted, QTreeView still updates its state.

This is strange, because the flash drive has nothing to do with the C drive. Why then, after inserting the flash drive, is it displayed in QTreeView and how can I reliably implement it so that after inserting the flash drive, it is automatically displayed in the tree regardless of setRootPath?

Here is my code

#include <QApplication>
#include <QTreeView>
#include <QFileSystemModel>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QFileSystemModel *model = new QFileSystemModel;
    model->setRootPath(QDir::rootPath());
    model->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::NoSymLinks);

    QTreeView *treeView = new QTreeView;
    treeView->setModel(model);

    for (int i = 1; i < model->columnCount(); ++i) {
        treeView->setColumnHidden(i, true);
    }

    treeView->setColumnWidth(0, 250);
    treeView->setWindowTitle("QFileSystemModel Example");
    treeView->resize(600, 400);
    treeView->setHeaderHidden(true);

    treeView->show();

    return app.exec();
}
发布评论

评论列表(0)

  1. 暂无评论