1

I'm looking to create a simple 'tail' type program which prints out new lines appended onto a file. Instead of constantly polling the file stat for an updated modified date, is there a way to catch a signal when:

  1. The file has been appended to
  2. The file has been renamed
  3. A new file of a given name appears

Those are the three requirements I need to design for. I found the QFileSystemWatcher will give me a signal for these 3 (I think)...but the signal is simple...no details as to what has changed so I still have to call stat. Any way to get this info from QFileSystemWatcher?

TSG
  • 4,242
  • 9
  • 61
  • 121

1 Answers1

0

Maybe you are looking for QFileSystemWatcher and its fileChanged signal.

You could hold a QHash<QString, QFileInfo> in your application, which mapps a filepath to its QFileInfo. In the slot which is connected to the QFileSystemWatcher::fileChanged(const QString & path) signal you could create a QFileInfo from the changed file and compare it to the one in the hash. After that set it as new QFileInfointo the hash.

// myapplicationobjec.h
class MyApplicationObject 
{
    // ...
private:
    QHash<QString, QFileInfo> m_fileInfos;
};

// myapplicationobjec.cpp

// Slot that is connected to QFileSystemWatcher::fileChanged signal
void MyApplicationObject::fileHasChanged(const QString & path)
{
    QFileInfo newFileInfo(path);
    QFileInfo oldFileInfo(m_fileInfos.value(path));

    // Compare size for example
    if (newFileInfo.size() != oldFileInfo.size()) {
        // do something if file size has changed
    } 

    // Replace old file info with new file info
    m_fileInfos.insert(path, newFileInfo);
}
tomvodi
  • 5,577
  • 2
  • 27
  • 38
  • But how do I know if the change is due to: data appended to file, file was renamed, there was no file and now one was created? Does QFileSystemWatcher offer that data somehow? (Thats the question) – TSG Oct 07 '13 at 13:48
  • I was hoping the file watcher would expose some member/method that offers more details....I guess not. (Unless someone else jumps in) – TSG Oct 07 '13 at 20:16