Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. update text edit at run time.
Qt 6.12 LTS is out! See what's new in the release blog

update text edit at run time.

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 6 Posters 7.4k Views 1 Watching
  • 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.
  • M mbatra

    @JonB

    Hi Jon,

    Thanks for your response. Now the code is working fine. I have created the QFileSystemWatcher object in the constructor only once...not in the button everytime.

    Thanks for correcting me.

    Now the file is getting checked and when its updated the text edit contents are also updated, but only when I click on the button, the contents are getting updated in the text edit.
    I want the text edit contents should get updated automatically (dynamically) as and when the contents in the txt file is getting changed.

    Please see the code below:

    void MainWindow::onstartButtonClicked()
    {
    textEdit->show();

    bool b = watcher->addPath("D:/Putty_Log/Putty.log");
    
    qDebug()<<"\nAddPath Return Value = "<<b;
    
    emit slotFileChanged("D:/Putty_Log/Putty.log");
    

    }

    void MainWindow::slotFileChanged(QString text)
    {
    qDebug()<<"slotFileChanged Called\n";
    qDebug()<<text<<endl;

    QFile inputFile(text);
    
    inputFile.open(QIODevice::ReadOnly);
    if (!inputFile.isOpen())
        return;
    
    QTextStream stream(&inputFile);
    
    for (QString line = stream.readLine(); !line.isNull(); line = stream.readLine())
    {
        textEdit->append(line);
    };
    

    }

    Any help would be appreciated.

    Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #15

    @mbatra said in update text edit at run time.:

    emit slotFileChanged("D:/Putty_Log/Putty.log");

    And where did you connect the signal from the file watcher to your slot? How is it supposed to work when you don't connect the slot?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    0
    • M mbatra

      @JonB

      Hi Jon,

      Thanks for your response. Now the code is working fine. I have created the QFileSystemWatcher object in the constructor only once...not in the button everytime.

      Thanks for correcting me.

      Now the file is getting checked and when its updated the text edit contents are also updated, but only when I click on the button, the contents are getting updated in the text edit.
      I want the text edit contents should get updated automatically (dynamically) as and when the contents in the txt file is getting changed.

      Please see the code below:

      void MainWindow::onstartButtonClicked()
      {
      textEdit->show();

      bool b = watcher->addPath("D:/Putty_Log/Putty.log");
      
      qDebug()<<"\nAddPath Return Value = "<<b;
      
      emit slotFileChanged("D:/Putty_Log/Putty.log");
      

      }

      void MainWindow::slotFileChanged(QString text)
      {
      qDebug()<<"slotFileChanged Called\n";
      qDebug()<<text<<endl;

      QFile inputFile(text);
      
      inputFile.open(QIODevice::ReadOnly);
      if (!inputFile.isOpen())
          return;
      
      QTextStream stream(&inputFile);
      
      for (QString line = stream.readLine(); !line.isNull(); line = stream.readLine())
      {
          textEdit->append(line);
      };
      

      }

      Any help would be appreciated.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #16

      @mbatra said in update text edit at run time.:

      emit slotFileChanged("

      Why emit?
      Simply call slotFileChanged as normal method.
      If it does not work: did you do any debugging? Is slotFileChanged called? What happens inside slotFileChanged (can the file be successfully opened?).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 2 Replies Last reply
      0
      • jsulmJ jsulm

        @mbatra said in update text edit at run time.:

        emit slotFileChanged("

        Why emit?
        Simply call slotFileChanged as normal method.
        If it does not work: did you do any debugging? Is slotFileChanged called? What happens inside slotFileChanged (can the file be successfully opened?).

        M Offline
        M Offline
        mbatra
        wrote on last edited by
        #17

        @jsulm

        Hi,

        Yes, when I call emit slotFileChanged("D:/Putty_Log/Putty.log");, its called and file is also opened. But when the file contents are getting updated, then this signal doesn't getting emitted. Its emitted only once.

        jsulmJ 1 Reply Last reply
        0
        • jsulmJ jsulm

          @mbatra said in update text edit at run time.:

          emit slotFileChanged("

          Why emit?
          Simply call slotFileChanged as normal method.
          If it does not work: did you do any debugging? Is slotFileChanged called? What happens inside slotFileChanged (can the file be successfully opened?).

          M Offline
          M Offline
          mbatra
          wrote on last edited by
          #18

          @jsulm

          If I call
          connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));

          Signal doesn't get emitted at all.

          JonBJ 1 Reply Last reply
          0
          • M mbatra

            @jsulm

            Hi,

            Yes, when I call emit slotFileChanged("D:/Putty_Log/Putty.log");, its called and file is also opened. But when the file contents are getting updated, then this signal doesn't getting emitted. Its emitted only once.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #19

            @mbatra There is NO need for emit at all!
            I just realised that your code makes no sense:

            void MainWindow::onstartButtonClicked()
            {
                textEdit->show();
                bool b = watcher->addPath("D:/Putty_Log/Putty.log");
                qDebug()<<"\nAddPath Return Value = "<<b;
                emit slotFileChanged("D:/Putty_Log/Putty.log");
            }
            

            You need to call slotFileChanged in the slot connected to the signal from the watcher. Currently you call it in onstartButtonClicked - so why should it be called everytime the file is edited? Think about the logic.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • M mbatra

              @jsulm

              If I call
              connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));

              Signal doesn't get emitted at all.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #20

              @mbatra said in update text edit at run time.:

              connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));

              This ought be all you need. When the watcher sees a file change it emits the fileChanged() signal and your slotFileChanged() slot should be called.

              M 1 Reply Last reply
              0
              • JonBJ JonB

                @mbatra said in update text edit at run time.:

                connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));

                This ought be all you need. When the watcher sees a file change it emits the fileChanged() signal and your slotFileChanged() slot should be called.

                M Offline
                M Offline
                mbatra
                wrote on last edited by
                #21

                @JonB

                Thanks Jon for your response.

                Thank you everyone for the support. Its working fine now.

                JonBJ 1 Reply Last reply
                0
                • M mbatra has marked this topic as solved on
                • M mbatra

                  @JonB

                  Thanks Jon for your response.

                  Thank you everyone for the support. Its working fine now.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #22

                  @mbatra
                  Now that you have it working, how about changing over to New Signal Slot Syntax:

                  connect(watcher, &QFileSystemWatcher::fileChanged, this, &MainWindow::slotFileChanged);
                  

                  Use this instead of SIGNAL/SLOT() syntax going forward for all your connect()s, you will get e.g. compile-time checking that the signal and slot methods and arguments match.

                  M 2 Replies Last reply
                  1
                  • JonBJ JonB

                    @mbatra
                    Now that you have it working, how about changing over to New Signal Slot Syntax:

                    connect(watcher, &QFileSystemWatcher::fileChanged, this, &MainWindow::slotFileChanged);
                    

                    Use this instead of SIGNAL/SLOT() syntax going forward for all your connect()s, you will get e.g. compile-time checking that the signal and slot methods and arguments match.

                    M Offline
                    M Offline
                    mbatra
                    wrote on last edited by
                    #23

                    @JonB

                    Hi Jon,

                    Yes, I have used the new connect mechanism.

                    Thanks.

                    1 Reply Last reply
                    1
                    • JonBJ JonB

                      @mbatra
                      Now that you have it working, how about changing over to New Signal Slot Syntax:

                      connect(watcher, &QFileSystemWatcher::fileChanged, this, &MainWindow::slotFileChanged);
                      

                      Use this instead of SIGNAL/SLOT() syntax going forward for all your connect()s, you will get e.g. compile-time checking that the signal and slot methods and arguments match.

                      M Offline
                      M Offline
                      mbatra
                      wrote on last edited by
                      #24

                      @JonB

                      Hi,

                      I am facing another issue. File contents are not getting updating dynamically. If I add anything from the keyboard into the txt file slotchanged signal gets emitted. But if I run a batch file and that batch file creates a log file which I am reading into my application, contents are getting updated in the log file but text edit not getting updated at the same time.

                      Any help would be appreciated.

                      jsulmJ JonBJ 2 Replies Last reply
                      0
                      • M mbatra

                        @JonB

                        Hi,

                        I am facing another issue. File contents are not getting updating dynamically. If I add anything from the keyboard into the txt file slotchanged signal gets emitted. But if I run a batch file and that batch file creates a log file which I am reading into my application, contents are getting updated in the log file but text edit not getting updated at the same time.

                        Any help would be appreciated.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #25

                        @mbatra Do you mean the file does not exist at the beginning, but is created to some later point in time? If that is the case then I suggest to also add the folder where the file is created to QFileSystemWatcher, so that you get a notification when the file is created in that folder.
                        Because if you read the documentation:

                        bool QFileSystemWatcher::addPath(const QString &path)
                        
                        Adds path to the file system watcher if path exists. The path is not added if it does not exist
                        

                        "if path exists. The path is not added if it does not exist"

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2
                        • M mbatra

                          @JonB

                          Hi,

                          I am facing another issue. File contents are not getting updating dynamically. If I add anything from the keyboard into the txt file slotchanged signal gets emitted. But if I run a batch file and that batch file creates a log file which I am reading into my application, contents are getting updated in the log file but text edit not getting updated at the same time.

                          Any help would be appreciated.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #26

                          @mbatra
                          Exactly as @jsulm has written. If the file to be monitored does not yet exist you cannot place a QFileSystemWatcher on it. You have to watch the directory first to see file creation via directoryChanged(). Only once it has been created can you then start to monitor the file for fileChanged(), so a two-step process for your case.

                          M 1 Reply Last reply
                          2
                          • JonBJ JonB

                            @mbatra
                            Exactly as @jsulm has written. If the file to be monitored does not yet exist you cannot place a QFileSystemWatcher on it. You have to watch the directory first to see file creation via directoryChanged(). Only once it has been created can you then start to monitor the file for fileChanged(), so a two-step process for your case.

                            M Offline
                            M Offline
                            mbatra
                            wrote on last edited by
                            #27
                            This post is deleted!
                            1 Reply Last reply
                            0

                            • Login

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