Python 2.7 如何在Qtableview中像在Qtextedit中一样启用自动滚动?
我使用QtableView和QStandardItemModel在GUI上显示日志,以保持适当的间距并过滤日志。我创建了模型并将数据插入其中。用于筛选字符串的QSortFilterProxy模型Python 2.7 如何在Qtableview中像在Qtextedit中一样启用自动滚动?,python-2.7,pyqt,pyqt4,qt-designer,qtableview,Python 2.7,Pyqt,Pyqt4,Qt Designer,Qtableview,我使用QtableView和QStandardItemModel在GUI上显示日志,以保持适当的间距并过滤日志。我创建了模型并将数据插入其中。用于筛选字符串的QSortFilterProxy模型 self.tableView = QtGui.QTableView(self) self.model = QtGui.QStandardItemModel(self) self.proxy = QtGui.QSortFilterProxyModel(self) self.proxy.setSourceM
self.tableView = QtGui.QTableView(self)
self.model = QtGui.QStandardItemModel(self)
self.proxy = QtGui.QSortFilterProxyModel(self)
self.proxy.setSourceModel(self.model)
self.tableView.setModel(self.proxy)
在一秒钟内,预计将有近100个日志显示在GUI上。添加新日志时,视图不会自动滚动,滑块仅停留在顶部。它不提供日志记录的实时感觉,用户需要手动滚动到最后。为了克服这个问题,我使用了以下语法
self.model.rowsInserted.connect(lambda: QtCore.QTimer.singleShot(5, self.tableView.scrollToBottom))
它为日志提供了实时感觉,但滑块始终保持在底部,我无法向上滚动查看以前的日志。每当我尝试移动滑块时,它会立即再次降到底部。所以这个语法不符合我的要求。在QTextEdit中,自动滚动是正确且用户友好的。我想在QtableView上看到同样的场景。是否有类似QTextEdit的自动滚动选项 要获得所需的行为,您只能在上一个滚动位置位于底部时自动滚动。这样,每当用户从底部滚动时,自动滚动将被禁用;但当它们滚动到底部时,自动滚动将重新启用。注意:要快速重新启用自动滚动,请右键单击滚动条并从关联菜单中选择“底部” 下面是一个简单的演示:
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.table = QtGui.QTableView(self)
self.model = QtGui.QStandardItemModel(self)
self.table.setModel(self.model)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.table)
self._scroll = True
self.model.rowsAboutToBeInserted.connect(self.beforeInsert)
self.model.rowsInserted.connect(self.afterInsert)
def beforeInsert(self):
vbar = self.table.verticalScrollBar()
self._scroll = vbar.value() == vbar.maximum()
def afterInsert(self):
if self._scroll:
self.table.scrollToBottom()
def addRow(self):
self.model.appendRow([QtGui.QStandardItem(c) for c in 'ABC'])
if __name__ == '__main__':
app = QtGui.QApplication([''])
window = Window()
window.setGeometry(500, 50, 400, 300)
window.show()
timer = QtCore.QTimer()
timer.timeout.connect(window.addRow)
timer.start(200)
app.exec_()
QTableView或QTreeView?QTableView@eyllanesc为什么使用代理?用于筛选日志字符串如果您注意到在qtexedit中,滚动条的位置取决于光标的位置,我正考虑对所选行的位置执行相同的操作,但对于您的筛选逻辑,这是不可能的。