Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QFileSystemWatcher fileChanged() signal to QML slot
QtWS25 Last Chance

QFileSystemWatcher fileChanged() signal to QML slot

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlqfilesystemwatcsignal & slotconnect slotc++ to qml
3 Posts 3 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.
  • T Offline
    T Offline
    texasRanger
    wrote on last edited by texasRanger
    #1

    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.

    //readJSON.h
    
    class readJSON :public QObject
    {
        Q_OBJECT
    
    public:
        readJSON();
    
    public slots:
        QString readingJson(const QString &name);
    
    };
    
    
    //main.cpp
    
    int main(int argc, char *argv[])
    {
    
    
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
    
    
    
        QFileSystemWatcher watcher;
        watcher.addPath("data.json");
    
        readJSON* myGlobal = new readJSON();
    
        engine.rootContext()->setContextProperty("myGlobalObject", myGlobal);
    
    
        QObject::connect(&watcher, SIGNAL(fileChanged()),
                         myGlobal, SLOT(updateDisplay()));
        
        engine.load(url);
    
        return app.exec();
    }
    
    //main.qml
    
    Window {
        visible: true
        width: 800
        height: 800
        title: qsTr("Screen Display")
    
    
        DisplaysForm {
    
            function callCPP(text) {
                var dataOne = myGlobalObject.readingJson(text)
                dataOne = parseFloat(dataOne)
    
                return dataOne
            }
    
           function updateDisplay() {
    
                var value = callCPP("Engine_Spd")
                engSpd_txt.text = value
    
                var value1 = callCPP("Engine_Power")
                engPwr_txt.text = value1
           }
    
    
    ODБOïO 1 Reply Last reply
    0
    • T texasRanger

      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.

      //readJSON.h
      
      class readJSON :public QObject
      {
          Q_OBJECT
      
      public:
          readJSON();
      
      public slots:
          QString readingJson(const QString &name);
      
      };
      
      
      //main.cpp
      
      int main(int argc, char *argv[])
      {
      
      
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                           &app, [url](QObject *obj, const QUrl &objUrl) {
              if (!obj && url == objUrl)
                  QCoreApplication::exit(-1);
          }, Qt::QueuedConnection);
      
      
      
          QFileSystemWatcher watcher;
          watcher.addPath("data.json");
      
          readJSON* myGlobal = new readJSON();
      
          engine.rootContext()->setContextProperty("myGlobalObject", myGlobal);
      
      
          QObject::connect(&watcher, SIGNAL(fileChanged()),
                           myGlobal, SLOT(updateDisplay()));
          
          engine.load(url);
      
          return app.exec();
      }
      
      //main.qml
      
      Window {
          visible: true
          width: 800
          height: 800
          title: qsTr("Screen Display")
      
      
          DisplaysForm {
      
              function callCPP(text) {
                  var dataOne = myGlobalObject.readingJson(text)
                  dataOne = parseFloat(dataOne)
      
                  return dataOne
              }
      
             function updateDisplay() {
      
                  var value = callCPP("Engine_Spd")
                  engSpd_txt.text = value
      
                  var value1 = callCPP("Engine_Power")
                  engPwr_txt.text = value1
             }
      
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      hi

      @texasRanger said in QFileSystemWatcher fileChanged() signal to QML slot:

      QObject::connect(&watcher, SIGNAL(fileChanged()),
      myGlobal, SLOT(updateDisplay()));

      you are trying to connect the signal fileChanged of watcher to the slot updateDisplay of myGlobal, but myGlobal has no slot
      updateDisplay, that function (slot) is defined in QML

      here is an example how to connect c++ signal to qml function
      https://forum.qt.io/topic/116084/c-signal-to-a-qml-file/4

      also you sould use "new" signal/slot syntax
      https://wiki.qt.io/New_Signal_Slot_Syntax

      1 Reply Last reply
      0
      • eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by eyllanesc
        #3

        Export watcher:

        int main(int argc, char *argv[])
        {
        
        
            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        
            QGuiApplication app(argc, argv);
        
            QQmlApplicationEngine engine;
            const QUrl url(QStringLiteral("qrc:/main.qml"));
            QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                             &app, [url](QObject *obj, const QUrl &objUrl) {
                if (!obj && url == objUrl)
                    QCoreApplication::exit(-1);
            }, Qt::QueuedConnection);
        
        
        
            QFileSystemWatcher watcher;
            watcher.addPath("data.json");
        
            readJSON myGlobal;
        
            engine.rootContext()->setContextProperty("myGlobalObject", &myGlobal);
            engine.rootContext()->setContextProperty("watcher", &watcher);
            
            engine.load(url);
        
            return app.exec();
        }
        

        and use Connections:

        Window {
            visible: true
            width: 800
            height: 800
            title: qsTr("Screen Display")
        
        
            DisplaysForm {
        
                function callCPP(text) {
                    var dataOne = myGlobalObject.readingJson(text)
                    dataOne = parseFloat(dataOne)
        
                    return dataOne
                }
        
               function updateDisplay() {
        
                    var value = callCPP("Engine_Spd")
                    engSpd_txt.text = value
        
                    var value1 = callCPP("Engine_Power")
                    engPwr_txt.text = value1
               }
            Connections{
                target: watcher
                function onFileChanged(path){
                        console.log(path)
                        updateDisplay()
                }
            }
        }
        

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply
        1

        • Login

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