Please nominate your Qt Champions for 2021!

[SOLVED] QFileSystemWatcher reports change twice

  • Hi,

    on Windows I use the QFileSystemWatcher to watch one file. If I now change this file and press the save button my slot is called through the fileChanged(QString) signal but not only once but twice.
    Any ideas what can cause such a behaviour?


  • Hi,

    some questions:

    Are you sure you using QFileSystemWatcher in the right way? (coudl you post your code?

    This happens for every file? Or for a specified application files (Word, Excel, ....)?

    In any case could you use QFileInfo class to check if really there are something changed in file

  • Here I add the file path to the watcher.

    connect(&watcher, SIGNAL(fileChanged(QString)), this,

    In this case I only have one file with the extension .ini. And the slot is

    @void Core::OnConfigurationFileChanged(QString configFile)
    if (configFile != QString(config->GetFileName().c_str()))
    LOG_INFO("The configuration has changed. Reload where possible.");

    It also does not happen everytime but most of the time. So it is quite indeterministic.

  • Hi,

    you should check (using QFileInfo class) if really something is changed (size, permission, time parameters, ....)

  • For some reasons if I use Notepad++ to edit my file it seems that it creates a file because then the size is 0 which the file watcher recognizes. After that the file is written again with content. This will give the second notification.

    I never managed to get this behaviour for example with the Windows editor so it seems to me that this comes from the way notepad++ is writing the files.

  • @butterface I have the same issue, did you find any solution to avoid saving twice with notepad++ ?

  • @Muby
    It is unlikely that @butterface is around to answer 6 years after he last posted.

    If your editor behaviour is to save the file first with 0 length and then with its content then you will indeed get two QFileSystemWatcher notifications. Why is this a problem? If necessary, code to recognise the first/second and ignore one as necessary, e.g. if the first one is 0 size you could recognise that, or set off a timer with a short delay when the first notification arrives to wait to see if you get the second notification on the same file within that time period. Not sure I see a problem.

Log in to reply