python - PyQt4 强制 View 从 QAbstractItemModel 获取更多

标签 python qt pyqt qtableview qabstractitemmodel

我有一个 QTableView,它从继承 QAbstractItemModel 的自定义模型动态加载数据。该模型同时实现了 fetchMore 和 canFetchMore。

问题是我希望能够为小型数据集选择所有行,但如果我在 View 中按 ctrl-a,它只会选择当前加载的行。

是否有某种机制可以强制 QTableView 获取更多行?理想情况下,我想显示一个进度条,指示已从模型加载的数据部分。每隔几秒钟我就想强制模型加载更多的数据,但我仍然想让用户与目前已加载的数据进行交互。这样,当进度条完成时,用户可以按 ctrl-a 并确信已选择所有数据。


编辑:我还有另一个激励用例。我想跳转到特定行,但如果未加载该行,我的界面什么也不做。

如何强制 QAbstractItemModel 获取更多(或最多特定行)然后强制 QTableView 显示它?

如果我不实现 fetchMore 和 canFetchMore,以前的功能可以工作,但加载表格非常慢。当我实现这些方法时,情况恰恰相反。没有这个问题的答案会导致我的 qt 界面的可用性出现问题,所以我正在为这个问题悬赏。

这是我用来选择特定行的方法。

def select_row_from_id(view, _id, scroll=False, collapse=True):
    """
        _id is from the iders function (i.e. an ibeis rowid)
        selects the row in that view if it exists
    """
    with ut.Timer('[api_item_view] select_row_from_id(id=%r, scroll=%r, collapse=%r)' %
                  (_id, scroll, collapse)):
        qtindex, row = view.get_row_and_qtindex_from_id(_id)
        if row is not None:
            if isinstance(view, QtWidgets.QTreeView):
                if collapse:
                    view.collapseAll()
                select_model = view.selectionModel()
                select_flag = QtCore.QItemSelectionModel.ClearAndSelect
                #select_flag = QtCore.QItemSelectionModel.Select
                #select_flag = QtCore.QItemSelectionModel.NoUpdate
                with ut.Timer('[api_item_view] selecting name. qtindex=%r' % (qtindex,)):
                    select_model.select(qtindex, select_flag)
                with ut.Timer('[api_item_view] expanding'):
                    view.setExpanded(qtindex, True)
            else:
                # For Table Views
                view.selectRow(row)
            # Scroll to selection
            if scroll:
                with ut.Timer('scrolling'):
                    view.scrollTo(qtindex)
            return row
    return None

如果用户手动滚动到有问题的行,则此功能有效。然而,如果用户没有看到特定的行,这个函数只是滚动回到 View 的顶部。

最佳答案

这里的答案可能为时已晚,但也许它在未来仍会对某些人有益。

下面可以找到一个列表模型的工作示例,其中包含 canFetchMorefetchMore 方法 + 带有几个自定义方法的 View :

  1. 尝试从模型中加载更多项目的方法,如果模型有一些东西还没有加载
  2. 能够从模型中获取尚未加载的特定行的方法

示例中的 QMainWindow 子类有一个计时器,用于重复调用上述方法中的第一个,每次都强制将另一批项目从模型加载到 View 中。在较小的时间间隔内批量加载项目可以避免完全阻塞 UI 线程,并且能够编辑目前加载的项目而几乎没有延迟。该示例包含一个进度条,显示到目前为止已加载的部分项目。

QMainWindow 子类还有一个旋转框,它允许选择特定的行显示在 View 中。如果已经从模型中获取了相应的项目,则 View 会简单地滚动到它。否则,它首先从模型中以同步方式(即 UI 阻塞方式)获取该行的项目。

这是解决方案的完整代码,使用 python 3.5.2 和 PyQt5 进行了测试:

import sys
from PyQt5 import QtWidgets, QtCore

class DelayedFetchingListModel(QtCore.QAbstractListModel):
    def __init__(self, batch_size=100, max_num_nodes=1000):
        QtCore.QAbstractListModel.__init__(self)
        self.batch_size = batch_size
        self.nodes = []
        for i in range(0, self.batch_size):
            self.nodes.append('node ' + str(i))
        self.max_num_nodes = max(self.batch_size, max_num_nodes)

    def flags(self, index):
        if not index.isValid():
            return QtCore.Qt.ItemIsEnabled
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable;

    def rowCount(self, index):
        if index.isValid():
            return 0
        return len(self.nodes)

    def data(self, index, role):
        if not index.isValid():
            return None
        if role != QtCore.Qt.DisplayRole:
            return None
        row = index.row()
        if row < 0 or row >= len(self.nodes):
            return None
        else:
            return self.nodes[row]

    def setData(self, index, value, role):
        if not index.isValid():
            return False
        if role != QtCore.Qt.EditRole:
            return False
        row = index.row()
        if row < 0 or row >= len(self.nodes):
            return False
        self.nodes[row] = value
        self.dataChanged.emit(index, index)
        return True

    def headerData(self, section, orientation, role):
        if section != QtCore.Qt.Horizontal:
            return None
        if section != 0:
            return None
        if role != QtCore.Qt.DisplayRole:
            return None
        return 'node'

    def canFetchMore(self, index):
        if index.isValid():
            return False
        return (len(self.nodes) < self.max_num_nodes)

    def fetchMore(self, index):
        if index.isValid():
            return
        current_len = len(self.nodes)
        target_len = min(current_len + self.batch_size, self.max_num_nodes)
        self.beginInsertRows(index, current_len, target_len - 1)
        for i in range(current_len, target_len):
            self.nodes.append('node ' + str(i))
        self.endInsertRows()

class ListView(QtWidgets.QListView):
    def __init__(self, parent=None):
        QtWidgets.QListView.__init__(self, parent)

    def jumpToRow(self, row):
        model = self.model()
        if model == None:
            return False
        num_rows = model.rowCount()
        while(row >= num_rows):
            res = fetchMoreRows(QtCore.QModelIndex())
            if res == False:
                return False
            num_rows = model.rowCount()
        index = model.index(row, 0, QtCore.QModelIndex())
        self.scrollTo(index, QtCore.QAbstractItemView.PositionAtCenter)
        return True

    def fetchMoreRows(self, index):
        model = self.model()
        if model == None:
            return False
        if not model.canFetchMore(index):
            return False
        model.fetchMore(index)
        return True

class MainForm(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        # Setup the model
        self.max_num_nodes = 10000
        self.batch_size = 100
        self.model = DelayedFetchingListModel(batch_size=self.batch_size, max_num_nodes=self.max_num_nodes)
        # Setup the view
        self.view = ListView()
        self.view.setModel(self.model)
        # Update the currently selected row in the spinbox
        self.view.selectionModel().currentChanged.connect(self.onCurrentItemChanged)
        # Select the first row in the model
        index = self.model.index(0, 0, QtCore.QModelIndex())
        self.view.selectionModel().clearSelection()
        self.view.selectionModel().select(index, QtCore.QItemSelectionModel.Select)
        # Setup the spinbox
        self.spinBox = QtWidgets.QSpinBox()
        self.spinBox.setMinimum(0)
        self.spinBox.setMaximum(self.max_num_nodes-1)
        self.spinBox.setSingleStep(1)
        self.spinBox.valueChanged.connect(self.onSpinBoxNewValue)
        # Setup the progress bar showing the status of model data loading
        self.progressBar = QtWidgets.QProgressBar()
        self.progressBar.setRange(0, self.max_num_nodes)
        self.progressBar.setValue(0)
        self.progressBar.valueChanged.connect(self.onProgressBarValueChanged)
        # Add status bar but initially hidden, will only show it if there's something to say
        self.statusBar = QtWidgets.QStatusBar()
        self.statusBar.hide()
        # Collect all this stuff into a vertical layout
        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.view)
        self.layout.addWidget(self.spinBox)
        self.layout.addWidget(self.progressBar)
        self.layout.addWidget(self.statusBar)
        self.window = QtWidgets.QWidget()
        self.window.setLayout(self.layout)
        self.setCentralWidget(self.window)
        # Setup timer to fetch more data from the model over small time intervals
        self.timer = QtCore.QBasicTimer()
        self.timerPeriod = 1000
        self.timer.start(self.timerPeriod, self)

    def onCurrentItemChanged(self, current, previous):
        if not current.isValid():
            return
        row = current.row()
        self.spinBox.setValue(row)

    def onSpinBoxNewValue(self, value):
        try:
            value_int = int(value)
        except ValueError:
            return
        num_rows = self.model.rowCount(QtCore.QModelIndex())
        if value_int >= num_rows:
            # There is no such row within the model yet, trying to fetch more
            while(True):
                res = self.view.fetchMoreRows(QtCore.QModelIndex())
                if res == False:
                    # We shouldn't really get here in this example since out
                    # spinbox's range is limited by exactly the number of items
                    # possible to fetch but generally it's a good idea to handle
                    # cases like this, when someone requests more rows than 
                    # the model has
                    self.statusBar.show()
                    self.statusBar.showMessage("Can't jump to row %d, the model has only %d rows" % (value_int, self.model.rowCount(QtCore.QModelIndex())))
                    return
                num_rows = self.model.rowCount(QtCore.QModelIndex())
                if value_int < num_rows:
                    break;
        if num_rows < self.max_num_nodes:
            # If there are still items to fetch more, check if we need to update the progress bar
            if self.progressBar.value() < value_int:
                self.progressBar.setValue(value_int)
        elif num_rows == self.max_num_nodes:
            # All items are loaded, nothing to fetch more -> no need for the progress bar
            self.progressBar.hide()
        # Update the selection accordingly with the new row and scroll to it
        index = self.model.index(value_int, 0, QtCore.QModelIndex())
        selectionModel = self.view.selectionModel()
        selectionModel.clearSelection()
        selectionModel.select(index, QtCore.QItemSelectionModel.Select)
        self.view.scrollTo(index, QtWidgets.QAbstractItemView.PositionAtCenter)
        # Ensure the status bar is hidden now
        self.statusBar.hide()

    def timerEvent(self, event):
        res = self.view.fetchMoreRows(QtCore.QModelIndex())
        if res == False:
            self.timer.stop()
        else:
            self.progressBar.setValue(self.model.rowCount(QtCore.QModelIndex()))
            if not self.timer.isActive():
                self.timer.start(self.timerPeriod, self)

    def onProgressBarValueChanged(self, value):
        if value >= self.max_num_nodes:
            self.progressBar.hide()

def main():
    app = QtWidgets.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

我还想指出的另一件事是,此示例要求 fetchMore 方法同步执行其工作。但在更复杂的方法中,fetchMore 实际上不必这样做。如果您的模型从数据库加载其项目,那么在 UI 线程中与数据库同步对话将不是一个好主意。相反,fetchMore 实现可以启动信号/槽通信的异步序列,其中某些对象处理与发生在某些后台线程中的数据库的通信。

关于python - PyQt4 强制 View 从 QAbstractItemModel 获取更多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38506808/

相关文章:

linux - QtWebKit Linux Phonon 支持状态

qt - qt 4.8.4 中的文本渲染

python - 正确使用 setBackground

python - QtGui.QMessageBox.information 和自定义

python - SWIG 忽略类中的 %mutable;如何解决?

Python:如何抓取 ESPN 的比赛比赛信息

c++ - 具有虚函数的未解析外部符号

python - PyQt 中的 os.walk 模拟

Python,字符串(由变量和字符串组成,连接)用作新变量名?

python - 是否可以在不使用 jenkins 上的 "PyAutoGUI"库的情况下使用键盘操作?