使用文件监视刷新使用PyQt4的日志查看器

2024-03-29 09:01:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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它,以及如何触发一个向数据添加一行并刷新view事件。在

有什么帮助吗?在

谢谢。在


Tags: importselfview列表returninitdefsys
1条回答
网友
1楼 · 发布于 2024-03-29 09:01:18

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

但请注意,这可能不是一个好方法。 你可能想流化日志文件。。。 也许更有经验的人能帮上忙。在

相关问题 更多 >