3

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

何か助けはありますか?

ありがとう。

4

1 に答える 1

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_()

ただし、これはおそらく適切な方法ではないことに注意してください。ログファイルをストリーミングすることをお勧めします...多分もっと経験豊富な誰かが助けることができます。

于 2011-01-29T22:28:54.010 に答える