我有一个源模型,顶部有几个级联QSortFilterProxyModel
。对于我的应用程序,我需要modelReset
信号从底部到顶部冒出气泡,而不是反过来,我该怎么做?
mwe:
from PyQt5 import QtWidgets, QtCore
import sys
class Model(QtCore.QAbstractTableModel):
def reset(self):
self.beginResetModel()
self.endResetModel()
class Filter(QtCore.QSortFilterProxyModel):
def __init__(self, model, arg):
super().__init__(model)
self.setSourceModel(model)
model.modelReset.connect(lambda: print(f'Reset {arg}'))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
model = Model()
filter0 = Filter(model, '0')
filter1 = Filter(filter0, '1')
filter2 = Filter(filter1, '2')
filter3 = Filter(filter2, '3')
button = QtWidgets.QPushButton('Reset')
button.clicked.connect(model.reset)
button.show()
sys.exit(app.exec_())
返回:
Reset 3
Reset 2
Reset 1
Reset 0
但我想拥有
Reset 0
Reset 1
Reset 2
Reset 3
答案 0 :(得分:1)
您的代码有错误,它不会更改为监视您正在监视sourceModel的当前模型信号,而只会更改为:
import sys
from PyQt5 import QtWidgets, QtCore
class Model(QtCore.QAbstractTableModel):
def reset(self):
self.beginResetModel()
self.endResetModel()
class Filter(QtCore.QSortFilterProxyModel):
def __init__(self, model, arg, parent=None):
super().__init__(parent)
self.setSourceModel(model)
self.modelReset.connect(lambda: print(f'Reset {arg}')) # <---
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
model = Model()
filter0 = Filter(model, '0')
filter1 = Filter(filter0, '1')
filter2 = Filter(filter1, '2')
filter3 = Filter(filter2, '3')
button = QtWidgets.QPushButton('Reset')
button.clicked.connect(model.reset)
button.show()
sys.exit(app.exec_())
输出:
Reset 0
Reset 1
Reset 2
Reset 3