2011-01-24 99 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_() 

但要注意,這可能不是做一個好辦法。 您可能想要傳輸日誌文件... 也許更有經驗的人可以幫忙。