简体   繁体   English

QFileSystemModel 不发出 fileRenamed 信号

[英]QFileSystemModel doesn't emit fileRenamed signal

I am trying to watch the changes in a directory using QFileSystemModel.我正在尝试使用 QFileSystemModel 观察目录中的更改。 Whenever I rename a file in the root path, only the directoryLoaded() signal is emitted.每当我重命名根路径中的文件时,只会发出 directoryLoaded() 信号。 I want the fileRenamed() signal to be emitted so that I know which file is renamed to a new name.我希望发出 fileRenamed() 信号,以便我知道哪个文件被重命名为新名称。 Here is my code:这是我的代码:

model = new QFileSystemModel;
model->setRootPath("C:/test/");
QObject::connect(model, SIGNAL(fileRenamed(const QString&, const QString&, const QString&)), this, SLOT(updateRename()));
QObject::connect(model, SIGNAL(directoryLoaded(const QString&)), this, SLOT(loadDir()));

I am afraid you expect too much from this QFileSystemModel class. It does not and cannot catch if the renaming operation happens outside of the model. I looked up all uses of fileRenamed() signal and it seems that the only place where it is emitted is here: https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qfilesystemmodel.cpp.html#933恐怕您对这个QFileSystemModel class 期望过高。如果重命名操作发生在 model 之外,它不会也无法捕获。我查看了fileRenamed()信号的所有用途,似乎它唯一发出的地方是这里: https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qfilesystemmodel.cpp.html#933

And if you go a few lines above, you can see that this is triggered when the renaming happens inside this function https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qfilesystemmodel.cpp.html#873 In other words, if you use QFileSystemModel::setData() to set a name to an item, it will rename the item and emit the signal.如果你 go 上面几行,你可以看到当重命名发生在这个 function https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qfilesystemmodel.cpp.html#873换句话说,如果您使用QFileSystemModel::setData()为项目设置名称,它将重命名该项目并发出信号。 And it is the only way to have the signal emitted.这是发出信号的唯一方法。

And this is logical, if renaming happens outside of your program, then there is no certain way to find out that a certain file was renamed.这是合乎逻辑的,如果重命名发生在你的程序之外,那么就没有确定的方法来发现某个文件被重命名了。 Your application only observes that some file disappeared and another file with a different name emerged.您的应用程序只观察到一些文件消失了,另一个具有不同名称的文件出现了。 Of course, you can check whether the timestamp and size of the file is the same, whether they are also in the same parent folder, maybe also check the file content... and only if these things match and the only difference is in the file names, then you can conclude that the file was renamed.当然,您可以检查文件的时间戳和大小是否相同,它们是否也在同一个父文件夹中,也许还可以检查文件内容......只有当这些东西匹配时,唯一的区别在于文件名,那么您可以断定该文件已重命名。 But this is something you must program yourself.但这是你必须自己编程的东西。 QFileSystemModel will not do it for you because it does not know your specific intentions. QFileSystemModel不会为你做这件事,因为它不知道你的具体意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM