Is there any way to recognize if a data is changed from outside?
-
Hi !
I have a QTComboBox and can choose datas there. The input of the data will shown on my UI.
So what i want is:
If i'm loading the data, and open the data outside from the UI (QT) i want to recognize if the data is changed from outside.
So for example:- example.docx is selected and loaded in the UI.
- example.docx is opened outside,changed and saved.
- The UI recognize that the data is changed and give a message for that.
The data is shown by a QTreeView.
Regards!
-
@ollarch yes, already see it. But i have no idea how to use the file changed signal. Maybe you can give me an example.
-
Hi
Its just a signal so you just hook it up to ta slot. -
yes i tried like that:
header:QFileSystemWatcher* watcher;
cpp:
watcher->addPath(file); connect(watcher, &QFileSystemWatcher::fileChanged, this, &MainWindow::showmessage);
But i get an error on that connect point.
-
@developer_61 said in Is there any way to recognize if a data is changed from outside?:
But i get an error on that connect point.
Would you like us to guess what that error message states?
-
Hi
at any given time,you get error, it should always be included in the posts.. :)did you make
showmessage take
const QString &path?
-
@developer_61
hi
that is not a compile "connect" error !
that is runtime error. huge difference.did you do
watcher = new QFileSystemWatcher(); -
@developer_61 said in Is there any way to recognize if a data is changed from outside?:
Exception thrown: Read Access Violation
It helps if you tell us this in the first place, to save time....
Are you sure this runtime error does not happen on the
watcher->addPath(file);
statement? -
Well it might be normal as it depends what you do to trigger it,
Say first file is created and then closed. then open and written to.So it might be expected and how it should be :)
-
@mrjj I open the file change a letter and save it. After that the function is called two times.
Really confusing. But what you mean the file is created and closed? I mean the file is already existing. -
@developer_61 if you simply want to check on start of your own application, if the file was modified, than you could use
https://doc.qt.io/qt-5/qfileinfo.html#lastModifiedand check if the lastModified date correlates with your own last modification on it
-
@J-Hilk no i don't want to check it on start of my application. It has to been happen during the application. Evertything is working fine. Only the function showMessage is called two times.
-
Hi
well both the file content and "last modified" flag is changed so i dont think its an error but
simply how it works. QFileSystemWatcher sends 2 signals. -
@mrjj and is there a way to avoid that?
maybe with a static or something like that ? -
does anybody have an idea?
-
Hi
No there is no way to avoid that as far as i know as its correctly reported as
the file do change twice. So QFileSystemWatcher is correct for it report it.If its a huge issue for you, you could just use a timer and check "last modified" on the file
from time to time instead of using QFileSystemWatcher