0

There are some 'similar' questions in stackoverflow, but can't quite implement them. In pyqt, I'm trying to pipe the output of a logfile (which is updating real-time) into a QTextEdit widget. The code I have so far is:

    file = QFile('tmp')
    fh = file.open(QIODevice.ReadOnly)
    stream = QTextStream(file)
    while not stream.atEnd():
        line = stream.readLine()
        self.logTextEdit.append(line)
    file.close()

which processes the current contents, but not any subsequent changes. Ideally, a Qt signal would alert me to read another line as it's available and write it directly to TextEdit.

Paul Nelson
  • 1,291
  • 4
  • 13
  • 20
  • QIODevice (and thus QFile) has a readyRead() signal you could connect to. – Frank Osterfeld Mar 06 '14 at 21:15
  • I've added the following to the code above: – Paul Nelson Mar 06 '14 at 21:54
  • QObject.connect(file, SIGNAL("readyRead()"), self.blap) def blap(self): print "I SAW that change" – Paul Nelson Mar 06 '14 at 21:55
  • I've also tried the 'bytesWritten() signal. I removed the file.close, just in case. I then at the command line did: echo 'foo'>> tmp but it didn't trigger the signal. What am I missing? – Paul Nelson Mar 06 '14 at 21:58
  • @FrankOsterfeld `QFile` does not emit such signals when others modify the file. In fact, `QFile` does not emit any signals period. Yes, it has them, but never emits them. You really wouldn't want it to happen by default, since on many systems file change notifications are very expensive and you'd pay that price on every file open through QFile. That would be **really, really bad** in practice. As far as I'm concerned, running Qt apps with lots of open files would be a nice denial-of-service against the machine they run on :) – Kuba hasn't forgotten Monica Mar 06 '14 at 22:30
  • @Kuba Ober: right, didn't read the requirements properly... I thought it's about reading an existing file incrementally, but that's of course not what this is about. – Frank Osterfeld Mar 07 '14 at 06:51

1 Answers1

0

A QFile offers no mechanism of monitoring changes in the file contents. You need to use QFileSystemWatcher to get notified about changes to the file. You also need to intelligently handle the fact that you might read partial lines at the end of the file.

Make sure that you give a path to a file (not a folder) if you use the fileChanged signal, and that addPath returns True.

You may try monitoring both the file path and and the directory path - on some systems one will work while the other will fail.

If you're on Unices other than OS X, you might simply have filesystem notification daemons turned off (if such are necessary), or notifications turned off for given filesystem etc. As you can see, depending on working file system watcher is fraught with danger. If the watcher fails, you have to have a fallback of polling the file size and modification time (not very often!).

A QTextEdit is rather poorly-performing when appended to. You should use a model and a QListView instead, with some caveats.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313