QFileSystemWatcher sends 2 signals. Is there anyway to avoid that?
-
wrote on 17 Mar 2021, 11:20 last edited by
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 :)
-
wrote on 17 Mar 2021, 11:45 last edited by 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? -
@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.
-
wrote on 17 Mar 2021, 11:57 last edited by developer_61
@mrjj thank you for the answer ... how can i do that?
how du build that into the function ? -
@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 :)
-
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
wrote on 17 Mar 2021, 12:50 last edited by@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.
-
@mrjj thank you for the answer ... how can i do that?
how du build that into the function ?wrote on 17 Mar 2021, 13:39 last edited by@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(); }
-
wrote on 17 Mar 2021, 14:29 last edited by developer_61
@KroMignon @mrjj thank you so much.
@JonB i think its solved.
-
@KroMignon @mrjj thank you so much.
@JonB i think its solved.
wrote on 19 Mar 2021, 12:39 last edited by@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!
8/10