2011-01-24 61 views
3

我已经在Python中使用PyQt4实现了一个非常简单的日志查看器。使用文件观察刷新使用PyQt4日志查看器

我有兴趣使用它来跟踪程序的执行情况,因此在将新行附加到日志文件时必须刷新列表视图。

这里是我的执行(不表):

import sys 
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 

class LogEntryModel(QAbstractListModel): 
    def __init__(self, logfile, parent=None): 
     super(LogEntryModel, self).__init__(parent) 
     self.slurp(logfile) 

    def rowCount(self, parent=QModelIndex()): 
     return len(self.entries) 

    def data(self, index, role): 
     if index.isValid() and role == Qt.DisplayRole: 
      return QVariant(self.entries[index.row()]) 
     else: 
      return QVariant()   

    def slurp(self, logfile): 
     self.entries = []   
     with open(logfile, 'rb') as fp: 
      for line in fp.readlines(): 
       tokens = line.strip().split(' : ') 
       sender = tokens[2] 
       message = tokens[4] 
       entry = "%s %s" % (sender, message) 
       self.entries.append(entry) 

class LogViewerForm(QDialog): 
    def __init__(self, logfile, parent=None): 
     super(LogViewerForm, self).__init__(parent) 

     # build the list widget 
     list_label = QLabel(QString("<strong>MoMo</strong> Log Viewer")) 
     list_model = LogEntryModel(logfile)   
     self.list_view = QListView() 
     self.list_view.setModel(list_model) 
     list_label.setBuddy(self.list_view) 

     # define the layout 
     layout = QVBoxLayout() 
     layout.addWidget(list_label) 
     layout.addWidget(self.list_view) 
     self.setLayout(layout) 

if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    form = LogViewerForm(sys.argv[1]) 
    form.show() 
    app.exec_() 

作为呈现,应用程序按预期工作:打开文件,分析内容(拆分为' : ',并创建一个列表),并显示列表使用QListView

有一个QFileSystemWatcher类发出fileChanged信号,但我不知道在哪里connect它以及如何触发行添加到的数据,并刷新视图事件。

任何帮助?

感谢。

+0

我已经使用过Qt C++库,我只能告诉你,你应该将QFileSystemWatcher信号连接到你想要的插槽,然后在文件改变时调用它。稍后,只需阅读有关QListView的文档即可添加一行并在该插槽上刷新它。请记住,插槽可以是任何对象的方法(在你的情况下,一个def)。 – webbi 2011-01-29 21:24:09

回答

0

我很新的Python和PyQt的,但这个“作品”在这里:

import sys 
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 

class LogEntryModel(QAbstractListModel): 
    def __init__(self, logfile, parent=None): 
     super(LogEntryModel, self).__init__(parent) 
     self.slurp(logfile) 
     self.logfile = logfile 

    def rowCount(self, parent=QModelIndex()): 
     return len(self.entries) 

    def data(self, index, role): 
     if index.isValid() and role == Qt.DisplayRole: 
      return QVariant(self.entries[index.row()]) 
     else: 
      return QVariant() 

    def slurp(self, logfile): 
     self.entries = [] 
     with open(logfile, 'rb') as fp: 
      for line in fp.readlines(): 
       tokens = line.strip().split(' : ') 
       sender = tokens[2] 
       message = tokens[4] 
       entry = "%s %s" % (sender, message) 
       self.entries.append(entry) 

class LogViewerForm(QDialog): 
    def __init__(self, logfile, parent=None): 
     super(LogViewerForm, self).__init__(parent) 

     self.watcher = QFileSystemWatcher([logfile], parent=None) 
     self.connect(self.watcher, SIGNAL('fileChanged(const QString&)'), self.update_log) 

     # build the list widget 
     list_label = QLabel(QString("<strong>MoMo</strong> Log Viewer")) 
     list_model = LogEntryModel(logfile) 
     self.list_model = list_model 
     self.list_view = QListView() 
     self.list_view.setModel(self.list_model) 
     list_label.setBuddy(self.list_view) 

     # define the layout 
     layout = QVBoxLayout() 
     layout.addWidget(list_label) 
     layout.addWidget(self.list_view) 
     self.setLayout(layout) 

    def update_log(self): 
     print 'file changed' 
     self.list_model.slurp(self.list_model.logfile) 
     self.list_view.updateGeometries() 


if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    form = LogViewerForm(sys.argv[1]) 
    form.show() 
    app.exec_() 

但要注意,这可能不是做一个好办法。 您可能想要传输日志文件... 也许更有经验的人可以帮忙。