Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QFileSystemWatcher reports change twice
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QFileSystemWatcher reports change twice

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    butterface
    wrote on 14 May 2014, 17:59 last edited by
    #1

    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?

    Thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 14 May 2014, 18:16 last edited by
      #2

      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

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • B Offline
        B Offline
        butterface
        wrote on 15 May 2014, 11:03 last edited by
        #3

        Here I add the file path to the watcher.

        @watcher.addPath(config->GetFileName().c_str());
        connect(&watcher, SIGNAL(fileChanged(QString)), this,
        SLOT(OnConfigurationFileChanged(QString)));@

        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()))
        return;
        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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on 15 May 2014, 12:28 last edited by
          #4

          Hi,

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

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • B Offline
            B Offline
            butterface
            wrote on 15 May 2014, 14:38 last edited by
            #5

            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.

            M 1 Reply Last reply 22 Jul 2020, 09:15
            1
            • B butterface
              15 May 2014, 14:38

              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.

              M Offline
              M Offline
              Muby
              wrote on 22 Jul 2020, 09:15 last edited by
              #6

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

              J 1 Reply Last reply 22 Jul 2020, 10:17
              0
              • M Muby
                22 Jul 2020, 09:15

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

                J Offline
                J Offline
                JonB
                wrote on 22 Jul 2020, 10:17 last edited by
                #7

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

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved