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. QFileSystemWatcher sends 2 signals. Is there anyway to avoid that?
Forum Updated to NodeBB v4.3 + New Features

QFileSystemWatcher sends 2 signals. Is there anyway to avoid that?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 1.1k Views 2 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.
  • D Offline
    D Offline
    developer_61
    wrote on last edited by
    #1

    Hello i use the QFileSystemWatcher to call a function if a data is changed outside QT.
    So everything is working fine. The only problem is that the QFileSystemWatcher sends 2 signals. So my function is called two times.
    Is there any way to avoid that? For example to say that only 1 signal should be sended ? or the 2 signal should be stopped ?

    Regards

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      As far as i know, no. its not possible.

      Can you explain what the issue is that it called twice?

      Maybe we can find a workaround :)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        developer_61
        wrote on last edited by developer_61
        #3

        @mrjj hi,
        the problem is that in the function i got a QMessage that the data is changed outside.
        If the signal is sended two times i got the message two times :/
        So with the signal my function is called.
        So i want to avoid that ... is there no chance to do that?

        mrjjM 1 Reply Last reply
        0
        • D developer_61

          @mrjj hi,
          the problem is that in the function i got a QMessage that the data is changed outside.
          If the signal is sended two times i got the message two times :/
          So with the signal my function is called.
          So i want to avoid that ... is there no chance to do that?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @developer_61

          Hi
          I not seen any flags to change what QFileSystemWatcher will consider a change.
          Or a minimum time between changes.

          But in your function, you could keep track of the time of last seen message and if very close to each other (time wise), simply ignore no 2.

          1 Reply Last reply
          1
          • D Offline
            D Offline
            developer_61
            wrote on last edited by developer_61
            #5

            @mrjj thank you for the answer ... how can i do that?
            how du build that into the function ?

            mrjjM KroMignonK 2 Replies Last reply
            0
            • D developer_61

              @mrjj thank you for the answer ... how can i do that?
              how du build that into the function ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @developer_61
              Hi
              Well i hope the function is a slot and a member of a class.

              You could use std::chrono:
              https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c

              then simply have a LastSeen as a member in class.
              and if not set, then setTimeNow and let it run the rest of the function
              else if IS set, take time now and see the difference.
              If Diff is less than some Delta ( say 1 sec or what makes sens in your app)
              then set LAstSeen and return without running the rest of the function.
              If its More than Delta, then is a real change and run function.

              Something like that. hope it makes sense :)

              1 Reply Last reply
              0
              • D developer_61

                Hello i use the QFileSystemWatcher to call a function if a data is changed outside QT.
                So everything is working fine. The only problem is that the QFileSystemWatcher sends 2 signals. So my function is called two times.
                Is there any way to avoid that? For example to say that only 1 signal should be sended ? or the 2 signal should be stopped ?

                Regards

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @developer_61 said in QFileSystemWatcher sends 2 signals. Is there anyway to avoid that?:

                The only problem is that the QFileSystemWatcher sends 2 signals

                You should say why/what is happening which causes the signals to be raised. You give no information at all.

                1 Reply Last reply
                1
                • D developer_61

                  @mrjj thank you for the answer ... how can i do that?
                  how du build that into the function ?

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #8

                  @developer_61 said in QFileSystemWatcher sends 2 signals. Is there anyway to avoid that?:

                  how du build that into the function ?

                  There are many ways to do this, it depends how you have implemented your QMessageBox().
                  One solution could be to use a QMap which has as key to filename and as value a QElepasedTimer.
                  As the signal gives you the filename, you could use it to find the corresponding QElapsedTimer:

                  void MyBeatifulClass:fileChanged(const QString& filename)
                  {
                      // mFileMap is defined as member of the class
                     // QMap<QString, QElapsedTime> mFileMap;
                  
                     auto& tmr = mFileMap[filename];
                  
                     // we have to wait at least 10 seconds between each message
                     if(tmr.isValid() && !tmr.hasExpired(10*1000))
                        return;
                  
                     int ret = QMessageBox::warning(this, tr("My Application"),
                                                 tr("The document has been modified.\n"
                                                    "Do you want to reload it?"),
                                                 QMessageBox::Yes | QMessageBox::No,
                                                 QMessageBox::Yes);
                      tmr.start();
                  }
                  

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  2
                  • D Offline
                    D Offline
                    developer_61
                    wrote on last edited by developer_61
                    #9

                    @KroMignon @mrjj thank you so much.

                    @JonB i think its solved.

                    Pablo J. RoginaP 1 Reply Last reply
                    0
                    • D developer_61

                      @KroMignon @mrjj thank you so much.

                      @JonB i think its solved.

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #10

                      @developer_61 said in QFileSystemWatcher sends 2 signals. Is there anyway to avoid that?:

                      i think its solved.

                      so don't forget to mark your post as such!

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      1

                      • Login

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