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?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 1.0k Views
  • 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 17 Mar 2021, 11:20 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

    J 1 Reply Last reply 17 Mar 2021, 12:50
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 17 Mar 2021, 11:34 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 17 Mar 2021, 11:45 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?

        M 1 Reply Last reply 17 Mar 2021, 11:51
        0
        • D developer_61
          17 Mar 2021, 11:45

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 17 Mar 2021, 11:51 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 17 Mar 2021, 11:57 last edited by developer_61
            #5

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

            M K 2 Replies Last reply 17 Mar 2021, 12:06
            0
            • D developer_61
              17 Mar 2021, 11:57

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 17 Mar 2021, 12:06 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
                17 Mar 2021, 11:20

                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

                J Offline
                J Offline
                JonB
                wrote on 17 Mar 2021, 12:50 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
                  17 Mar 2021, 11:57

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

                  K Offline
                  K Offline
                  KroMignon
                  wrote on 17 Mar 2021, 13:39 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 17 Mar 2021, 14:29 last edited by developer_61
                    #9

                    @KroMignon @mrjj thank you so much.

                    @JonB i think its solved.

                    P 1 Reply Last reply 19 Mar 2021, 12:39
                    0
                    • D developer_61
                      17 Mar 2021, 14:29

                      @KroMignon @mrjj thank you so much.

                      @JonB i think its solved.

                      P Offline
                      P Offline
                      Pablo J. Rogina
                      wrote on 19 Mar 2021, 12:39 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

                      8/10

                      17 Mar 2021, 13:39

                      • Login

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