QFileSystemWatcher sends 2 signals. Is there anyway to avoid that?
-
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
-
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 :)
-
@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? -
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.
-
@mrjj thank you for the answer ... how can i do that?
how du build that into the function ? -
@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-cthen 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 :)
-
@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.
-
@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(); }
-
@KroMignon @mrjj thank you so much.
@JonB i think its solved.
-
@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!