Search code examples
qtqfilesystemmodel

QFileSystemModel and QFileSystemWatcher delete from disk


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.


Solution

  • 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.