0

I open a file in a Qlistview, If the file changes, maybe a line removed or a line changes, I'd like it to show on the qlistview as well. How can I do this?

My qlistview is pretty standard as defined below. Basically what I'm asking for is a way to automatically refresh the qlistview while changes are detected somehow...

with open(filex, "r") as f:
    for line in f:
        self.item = QtGui.QStandardItem(line)
        self.item.setCheckable(True)
        self.item.setCheckState(QtCore.Qt.Unchecked)
        self.model.appendRow(self.item)
    self.list_view.setModel(self.model)
answerSeeker
  • 2,692
  • 4
  • 38
  • 76

1 Answers1

0

Qt has a QFileSystemWatcher class that can monitor if a file has changed. Simply register your file with the addPath method and connect the fileChanged signal to a slot that updates your list model.

Be aware that monitoring files can consume resources, so be sure to read the notes about this in the detailed description of the QFileSystemWatcher.

BTW: in case the QFileSystemWatcher doesn't work out, there is also this post on how to monitor file changes in Python in general.

Community
  • 1
  • 1
titusjan
  • 5,376
  • 2
  • 24
  • 43