Hello,

I have a GUI display that shows data values from an external file. I want to be able to update the display whenever the values in the file change. I'm trying to use QFileSystemWatcher fileChanged() to emit a signal to the SLOT in qml that will update the display. I can't seem to figure out the sending of the signal when the file is read.


Qt Code:
  1. //readJSON.h
  2.  
  3. class readJSON :public QObject
  4. {
  5. Q_OBJECT
  6.  
  7. public:
  8. readJSON();
  9.  
  10. public slots:
  11. QString readingJson(const QString &name);
  12.  
  13. };
  14.  
  15.  
  16. //main.cpp
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.  
  21.  
  22. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  23.  
  24. QGuiApplication app(argc, argv);
  25.  
  26. QQmlApplicationEngine engine;
  27. const QUrl url(QStringLiteral("qrc:/main.qml"));
  28. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
  29. &app, [url](QObject *obj, const QUrl &objUrl) {
  30. if (!obj && url == objUrl)
  31. QCoreApplication::exit(-1);
  32. }, Qt::QueuedConnection);
  33.  
  34.  
  35.  
  36. watcher.addPath("data.json");
  37.  
  38. readJSON* myGlobal = new readJSON();
  39.  
  40. engine.rootContext()->setContextProperty("myGlobalObject", myGlobal);
  41.  
  42.  
  43. QObject::connect(&watcher, SIGNAL(fileChanged()),
  44. myGlobal, SLOT(updateDisplay()));
  45.  
  46. engine.load(url);
  47.  
  48. return app.exec();
  49. }
  50.  
  51. //main.qml
  52.  
  53. Window {
  54. visible: true
  55. width: 800
  56. height: 800
  57. title: qsTr("Screen Display")
  58.  
  59.  
  60. DisplaysForm {
  61.  
  62. function callCPP(text) {
  63. var dataOne = myGlobalObject.readingJson(text)
  64. dataOne = parseFloat(dataOne)
  65.  
  66. return dataOne
  67. }
  68.  
  69. function updateDisplay() {
  70.  
  71. var value = callCPP("Engine_Spd")
  72. engSpd_txt.text = value
  73.  
  74. var value1 = callCPP("Engine_Power")
  75. engPwr_txt.text = value1
  76. }
To copy to clipboard, switch view to plain text mode