当代理模型连接到视图时,从另一个线程修改QSortFilterProxyModel的源模型时遇到麻烦。代理模型似乎在正在显示的模型中添加了“鬼影”项目,这些空白项目无法(显然)无法与之交互
此示例代码演示了我遇到的问题,并简化为一个基本示例,其中每个代理模型和源模型都有一个视图:
from PySide2 import QtCore, QtGui, QtWidgets
import random
WordList = ["apples", "pears", "down", "stairs", "mother", "hubord", "isn't", "cupboard", "lemon", "tango", "apricot", "nuke"]
class Searcher(QtCore.QThread):
def __init__(self, model):
super(Searcher, self).__init__()
self.model = model
def run(self):
populateModel(self.model)
def populateModel(model):
for i in range(5):
item1 = QtGui.QStandardItem()
model.invisibleRootItem().appendRow(item1)
majorWord = random.choice(WordList)
item1.setData(majorWord, QtCore.Qt.DisplayRole)
for i in range(random.randint(2, 8)):
item2 = QtGui.QStandardItem()
item1.appendRow(item2)
item2.setData(os.path.join(majorWord, random.choice(WordList)), QtCore.Qt.DisplayRole)
if __name__ == '__main__':
tv = QtWidgets.QTreeView()
tv.setWindowTitle("Source Model, Threaded")
tv.show()
tv2 = QtWidgets.QTreeView()
tv2.setWindowTitle("Proxy Model, Threaded")
tv2.show()
tv3 = QtWidgets.QTreeView()
tv3.setWindowTitle("Proxy Model 2, No Threading")
tv3.show()
sourceModel = QtGui.QStandardItemModel()
tv.setModel(sourceModel)
proxyModel = QtCore.QSortFilterProxyModel()
proxyModel.setDynamicSortFilter(True)
proxyModel.setSourceModel(sourceModel)
tv2.setModel(proxyModel)
s = Searcher(sourceModel)
s.start()
sm3 = QtGui.QStandardItemModel()
pm3 = QtCore.QSortFilterProxyModel()
pm3.setSourceModel(sm3)
proxyModel.setDynamicSortFilter(True)
tv3.setModel(pm3)
populateModel(sm3)
我认为代理模型的setDynamicSortFilter
属性可能有用,但似乎没有任何作用。
任何帮助或指点表示赞赏!