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.2k 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 Offline
    M Offline
    mbatra
    wrote on last edited by
    #1

    Hi,

    I have created a Qt Widget application. I am using a text edit to update the text read from a txt file. I want to update the text edit at run time as and when user enters the text in the txt file. or as an log file is getting created, I want to read the text from the log file and update the text edit at the same time.

    Any help would be appreciated.

    Thanks.

    JonBJ 1 Reply Last reply
    0
    • M mbatra

      Hi,

      I have created a Qt Widget application. I am using a text edit to update the text read from a txt file. I want to update the text edit at run time as and when user enters the text in the txt file. or as an log file is getting created, I want to read the text from the log file and update the text edit at the same time.

      Any help would be appreciated.

      Thanks.

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

      @mbatra
      If a file is updated externally to your Qt application and you want to know that the file has changed, so that you can e.g. read it back in to put in a widget, see QFileSystemWatcher Class.

      M 1 Reply Last reply
      0
      • JonBJ JonB

        @mbatra
        If a file is updated externally to your Qt application and you want to know that the file has changed, so that you can e.g. read it back in to put in a widget, see QFileSystemWatcher Class.

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

        @JonB
        Hi,

        Thanks for the reply.

        I have used QFileSystemWatcher class. I added signal and slot for the same. But the signal doesn't get emitted on changing the file at run time.

        Thanks.

        JonBJ 1 Reply Last reply
        0
        • M mbatra

          @JonB
          Hi,

          Thanks for the reply.

          I have used QFileSystemWatcher class. I added signal and slot for the same. But the signal doesn't get emitted on changing the file at run time.

          Thanks.

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

          @mbatra
          Well, it should do, that's what it's there for. You can find other people using it on the web. You can post minimal code if you want us to check you are doing the right thing.

          It can only notify when changes are actually written to the file, e.g. not if they are still buffered in memory by the changing application.

          M 1 Reply Last reply
          0
          • JonBJ JonB

            @mbatra
            Well, it should do, that's what it's there for. You can find other people using it on the web. You can post minimal code if you want us to check you are doing the right thing.

            It can only notify when changes are actually written to the file, e.g. not if they are still buffered in memory by the changing application.

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

            @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 1 Reply Last reply
            0
            • 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 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

                                          • Login

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