PyQt4を使用してPythonで非常に単純なログビューアを実装しました。
プログラムの実行を追跡するために使用することに興味があるので、ログファイルに新しい行が追加されたときにリストビューを更新する必要があります。
これが私の実装です(時計なし):
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
何か助けはありますか?
ありがとう。