admin管理员组文章数量:1399466
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();
}
本文标签: cUpdating QFileSystemModel when inserting a flash cardStack Overflow
版权声明:本文标题:c++ - Updating QFileSystemModel when inserting a flash card - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744175744a2593991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论