QFileSystemModel 和 QFileSystemWatcher 从磁盘删除

发布于 11-29 07:53 字数 330 浏览 0 评论 0原文

我有一个 QTreeView,它是通过 QFileSystemModel 的重新实现来填充的。据我所知,QFileSystemModel 在 rootPath 上安装了 QFileSystemWatcher。我想做的是在 rootPath 上直接删除文件时在我的程序中发出通知,但我还没有找到任何信号或重新实现的函数来为我提供该信息。

我的应用程序通过 ftp 连接上传一些文件,当文件完全上传时,我将其从该位置删除,因此我希望在直接删除文件时(而不是通过删除方法或类似的方法)重新实现 QFileSystemModel 发出通知。

我希望你能帮助我。我在网上搜索了很多,但找不到任何东西。

干杯。

I have a QTreeView which is populated through a reimplementation of QFileSystemModel. As far as I know, QFileSystemModel installs a QFileSystemWatcher on the rootPath. What I'm trying to do is notify in my program when a file is being deleted directicly on the rootPath but i havent found any signal o reimplemented function which provides me that information.

My application upload some files thrugh an ftp connection and when the file is fully uploaded i remove it from the location, so i want a notification from the reimplementation of QFileSystemModel when the file is deleted directicly (not from the remove method or something similar).

I Hope you can help me. I have searched a lot on the web but I cant find anything.

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不乱于心2024-12-06 07:53:12

您可以使用 FileSystemModelrowsAboutToBeRemoved< /code>信号(继承自 QAbstractItemModel)。

每当从模型中删除一行时都会触发它。 parentstartend 参数允许您获取文件名(在子项的第 0 列中)。

示例代码:(

// once you have your model set up:
...
QObject::connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)),
         receiver, SLOT(toBeRemoved(const QModelIndex&, int, int)));
...
// in receiver class:
public slots:
 void toBeRemoved(const QModelIndex &parent, int start, int end) {
  std::cout << start << " -> " << end << std::endl;
  std::cout << parent.child(start, 0).data().typeName() << std::endl;
  std::cout << qPrintable(parent.child(start, 0).data().toString()) << std::endl;
 }

我认为使用 std::cout 并不是 Qt 的好习惯,这只是为了让您开始。)

来自 QAbstractItemModel 的其他 aboutToBe... 信号可用于其他文件系统上发生的事件。

You can use the FileSystemModel's rowsAboutToBeRemoved signal (inherited from QAbstractItemModel).

It will be fired whenever a row is removed from the model. The parent, start and end parameters allow you to get to the filename (in column 0 of the children).

Sample code:

// once you have your model set up:
...
QObject::connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)),
         receiver, SLOT(toBeRemoved(const QModelIndex&, int, int)));
...
// in receiver class:
public slots:
 void toBeRemoved(const QModelIndex &parent, int start, int end) {
  std::cout << start << " -> " << end << std::endl;
  std::cout << parent.child(start, 0).data().typeName() << std::endl;
  std::cout << qPrintable(parent.child(start, 0).data().toString()) << std::endl;
 }

(Using std::cout isn't good practice with Qt I think, this is just to get you started.)

The other aboutToBe... signals from QAbstractItemModel can be used for the other events that happen on the filesystem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文