QFileSystemWatcher help



  • 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());
    
    
    
                     }
    
                }
            }
    }
    

  • Qt Champions 2016

    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.


  • Lifetime Qt Champion

    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.



  • @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?


  • Qt Champions 2016

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.