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. QFileSystemWatcher help
Qt 6.11 is out! See what's new in the release blog

QFileSystemWatcher help

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.4k Views 2 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.
  • timmie124T Offline
    timmie124T Offline
    timmie124
    wrote on last edited by
    #1

    I'm currently trying to write a program that reads updating chat logs. so far my program finds the files that need to be watched based on the QStringlist channelList. right now I'm trying to find a way to open the newest file modified based on that list and then read the newest line added and append it to the TextBrowser. I've been playing around with QFIleSystemWatcher and haven't had any success with it yet was wondering if someone could help me figure out how to properly implement it.

    void MainWindow::on_pushButton_clicked()
    {
        QString defaultpath = QDir::homePath() + "/Documents/EVE/logs/Chatlogs";
        QStringList channelList;
        channelList << "Lift_The_Vale" << "Corp";
        QString intel = "Lift_The_Vale";
        QDir chatLogDir;
        chatLogDir.setPath(defaultpath);
        chatLogDir.setSorting(QDir::Time);
        //chatLogDir.setNameFilters(channelList);
        QFileInfoList list = chatLogDir.entryInfoList(QDir::AllEntries);
        //ui->textBrowser->clear();
        for (int i =0 ;i<channelList.size(); i++){
            QString intel = channelList.at(i);
            for (int i = 1; i<list.size(); i++){
                if(list.at(i).fileName().startsWith(intel)){
                    QFileSystemWatcher logwatcher;
                    //logwatcher.addPath()
                    QString channelFile = defaultpath + list.at(i).fileName();
                    logwatcher.addPath(channelFile);
    
                    /*if(logwatcher.fileChanged()){
                    ui->textBrowser->append("worked");
                    }*/
    
    
    
                    //ui->textBrowser->append(channelFile);
                    //ui->textBrowser->append(list.at(i).fileName());
    
    
    
                     }
    
                }
            }
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome
      Normally you would hook up to the signal QFileSystemWatcher can emit when
      the folder change. Then you would read the files and take the newest.

      You code seems sort of reverse? not sure.

      see here for a sample:
      http://stackoverflow.com/questions/10044853/how-to-use-qfilesystemwatcher-to-monitor-a-folder-for-change

      The key point is this
      QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), mc, SLOT(showModified(QString)));

      its call your function when folder change. this way u know its time to look for files.

      timmie124T 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        To add to @mrjj, your logwatcher variable is local to your if thus it will go out of scope and get destroyed before anything useful happens. You also don't connect any signal from that watcher.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • mrjjM mrjj

          Hi and welcome
          Normally you would hook up to the signal QFileSystemWatcher can emit when
          the folder change. Then you would read the files and take the newest.

          You code seems sort of reverse? not sure.

          see here for a sample:
          http://stackoverflow.com/questions/10044853/how-to-use-qfilesystemwatcher-to-monitor-a-folder-for-change

          The key point is this
          QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), mc, SLOT(showModified(QString)));

          its call your function when folder change. this way u know its time to look for files.

          timmie124T Offline
          timmie124T Offline
          timmie124
          wrote on last edited by
          #4

          @mrjj I got it working but its behaving weird. The file I'm watching is being written by another program, and doesn't emit the signal when it adds a line of text to the file. in order for me to get the file watcher to emit the signal I have to open the file then close it. is QFileSystemWatcher the class to use for this?

          mrjjM 1 Reply Last reply
          0
          • timmie124T timmie124

            @mrjj I got it working but its behaving weird. The file I'm watching is being written by another program, and doesn't emit the signal when it adds a line of text to the file. in order for me to get the file watcher to emit the signal I have to open the file then close it. is QFileSystemWatcher the class to use for this?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @timmie124
            Hi, the docs says it should give signal when a file is changed.

            Seems the "logger" keep file open and it cannot detect changes?

            I have not tried with always open file.

            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