Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. update text edit at run time.
Forum Updated to NodeBB v4.3 + New Features

update text edit at run time.

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 6 Posters 3.3k 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
    This is the code that I have written:

    void MainWindow::slotFileChanged(QString text)
    {
    qDebug()<<text<<endl;
    }

    void MainWindow::onstartButtonClicked()
    {
    QFileSystemWatcher watcher(this);
    watcher.addPath("D:\MB\Selection\Resources\log.log");

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

    }

    Gojir4G Offline
    Gojir4G Offline
    Gojir4
    wrote on last edited by Gojir4
    #6

    Your code doesn't work because the instance of QFileSystemWatcher is destroyed when onStartButtonClicked is terminated. So basically the watcher is created and immediately destroyed, so you'll never get any signal emitted from it. You must keep an instance of QFileSystemWatcher as a member of your MainWindow class.

    M 1 Reply Last reply
    2
    • Gojir4G Gojir4

      Your code doesn't work because the instance of QFileSystemWatcher is destroyed when onStartButtonClicked is terminated. So basically the watcher is created and immediately destroyed, so you'll never get any signal emitted from it. You must keep an instance of QFileSystemWatcher as a member of your MainWindow class.

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

      @Gojir4

      Hi,

      Thanks for your response. Now, I have added the below code:

      .h file:
      QFileSystemWatcher *watcher;
      public slots:
      void slotFileChanged(QString &text);

      .cpp file:

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

      void MainWindow::onstartButtonClicked()
      {
      watcher = new QFileSystemWatcher(this);
      watcher->addPath(":/log.log");

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

      }

      is it correct now.

      Still I am not getting the messages displayed in the slotchanged function.

      J.HilkJ M JonBJ 4 Replies Last reply
      0
      • M mbatra

        @Gojir4

        Hi,

        Thanks for your response. Now, I have added the below code:

        .h file:
        QFileSystemWatcher *watcher;
        public slots:
        void slotFileChanged(QString &text);

        .cpp file:

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

        void MainWindow::onstartButtonClicked()
        {
        watcher = new QFileSystemWatcher(this);
        watcher->addPath(":/log.log");

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

        }

        is it correct now.

        Still I am not getting the messages displayed in the slotchanged function.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #8

        @mbatra please check the bool return value of addPath call, because I don't know of a file system that uses :as a valid beginning for a path


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        M 1 Reply Last reply
        1
        • M mbatra

          @Gojir4

          Hi,

          Thanks for your response. Now, I have added the below code:

          .h file:
          QFileSystemWatcher *watcher;
          public slots:
          void slotFileChanged(QString &text);

          .cpp file:

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

          void MainWindow::onstartButtonClicked()
          {
          watcher = new QFileSystemWatcher(this);
          watcher->addPath(":/log.log");

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

          }

          is it correct now.

          Still I am not getting the messages displayed in the slotchanged function.

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

            @Gojir4

            Hi,

            Thanks for your response. Now, I have added the below code:

            .h file:
            QFileSystemWatcher *watcher;
            public slots:
            void slotFileChanged(QString &text);

            .cpp file:

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

            void MainWindow::onstartButtonClicked()
            {
            watcher = new QFileSystemWatcher(this);
            watcher->addPath(":/log.log");

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

            }

            is it correct now.

            Still I am not getting the messages displayed in the slotchanged function.

            M Offline
            M Offline
            mbatra
            wrote on last edited by
            #10
            This post is deleted!
            1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @mbatra please check the bool return value of addPath call, because I don't know of a file system that uses :as a valid beginning for a path

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

              @J-Hilk

              Hi,

              I have added this now, it returns false;

              bool b = watcher->addPath("D:\Putty_Log\log.log");

              Can we specify the full path like this. Does it accepts.

              It also gives this error message:

              QObject::connect: No such signal QFileSystemWatcher::fileChanged(QString &)
              QObject::connect: (receiver name: 'MainWindow')

              Thanks.

              Gojir4G 1 Reply Last reply
              0
              • M mbatra

                @J-Hilk

                Hi,

                I have added this now, it returns false;

                bool b = watcher->addPath("D:\Putty_Log\log.log");

                Can we specify the full path like this. Does it accepts.

                It also gives this error message:

                QObject::connect: No such signal QFileSystemWatcher::fileChanged(QString &)
                QObject::connect: (receiver name: 'MainWindow')

                Thanks.

                Gojir4G Offline
                Gojir4G Offline
                Gojir4
                wrote on last edited by
                #12

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

                bool b = watcher->addPath("D:\Putty_Log\log.log");

                @mbatra When using backslash in string you must escape using double \\, which gives you D:\\Putty_Log\\log.log as a path.

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

                void MainWindow::onstartButtonClicked()
                {
                watcher = new QFileSystemWatcher(this);
                watcher->addPath(":/log.log");
                connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));

                }

                You don't need necessarily to use a pointer for your QFileSystemWatcher member object. As you have done here this will create a new instance every time onstartButtonClicked is called, which is probaly not what you want. But it will probably work anyway.

                1 Reply Last reply
                2
                • M mbatra

                  @Gojir4

                  Hi,

                  Thanks for your response. Now, I have added the below code:

                  .h file:
                  QFileSystemWatcher *watcher;
                  public slots:
                  void slotFileChanged(QString &text);

                  .cpp file:

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

                  void MainWindow::onstartButtonClicked()
                  {
                  watcher = new QFileSystemWatcher(this);
                  watcher->addPath(":/log.log");

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

                  }

                  is it correct now.

                  Still I am not getting the messages displayed in the slotchanged function.

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

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

                  watcher->addPath(":/log.log");

                  This is a Qt resource path. It won't be accepted for watching, and in any case Qt resource files are read-only so it will never change.

                  bool b = watcher->addPath("D:\Putty_Log\log.log");

                  Single backslash characters must be doubled in a C++ literal string.

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

                  QObject::connect: No such signal QFileSystemWatcher::fileChanged(QString &)

                  It is much easier if you use the "new"-style connect() syntax, which was introduced over a decade ago.

                  The signature is QFileSystemWatcher::fileChanged(const QString &), I don't know that SLOT() let's you get away without that const.

                  As @Gojir4 says, you should not create a new QFileSystemWatcher object each time a button is pressed.

                  M 1 Reply Last reply
                  3
                  • JonBJ JonB

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

                    watcher->addPath(":/log.log");

                    This is a Qt resource path. It won't be accepted for watching, and in any case Qt resource files are read-only so it will never change.

                    bool b = watcher->addPath("D:\Putty_Log\log.log");

                    Single backslash characters must be doubled in a C++ literal string.

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

                    QObject::connect: No such signal QFileSystemWatcher::fileChanged(QString &)

                    It is much easier if you use the "new"-style connect() syntax, which was introduced over a decade ago.

                    The signature is QFileSystemWatcher::fileChanged(const QString &), I don't know that SLOT() let's you get away without that const.

                    As @Gojir4 says, you should not create a new QFileSystemWatcher object each time a button is pressed.

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

                    @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 jsulmJ 2 Replies 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.

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      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

                                          • Login

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