Nominate our 2022 Qt Champions!

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;
        QFileInfoList list = chatLogDir.entryInfoList(QDir::AllEntries);
        for (int i =0 ;i<channelList.size(); i++){
            QString intel =;
            for (int i = 1; i<list.size(); i++){
                    QFileSystemWatcher logwatcher;
                    QString channelFile = defaultpath +;

  • Lifetime Qt Champion

    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:

    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


    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?

  • Lifetime Qt Champion

    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