下面的代码使用两个 ProxyModel 过滤 10,000 个项目。有用...
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class MyTableModel(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.items = [i for i in range(10000)]
def rowCount(self, parent):
return len(self.items)
def columnCount(self, parent):
return 1
def data(self, index, role):
if not index.isValid():
return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
row=index.row()
column=index.column()
if row<len(self.items):
return QVariant(self.items[row])
else:
return QVariant()
class Proxy01(QSortFilterProxyModel):
def __init__(self):
super(Proxy01, self).__init__()
def filterAcceptsRow(self, row, parent):
sourceModel=self.sourceModel()
index=sourceModel.index(row, 0, parent)
name=sourceModel.data(index, Qt.DisplayRole).toString()
if name and not int(name)%10:
return True
return False
class Proxy02(QSortFilterProxyModel):
def __init__(self):
super(Proxy02, self).__init__()
self.keyword=None
def setKeyword(self, arg):
if arg: self.keyword=str(arg)
self.reset()
def filterAcceptsRow(self, row, parent):
sourceModel=self.sourceModel().sourceModel()
index=sourceModel.index(row, 0, parent)
name=sourceModel.data(index, Qt.DisplayRole).toString()
if self.keyword and name and not self.keyword.lower() in str(name).lower():
return False
return True
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
self.tablemodel=MyTableModel(self)
self.proxy1=Proxy01()
self.proxy1.setSourceModel(self.tablemodel)
self.proxy2=Proxy02()
self.proxy2.setSourceModel(self.proxy1)
tableviewA=QTableView()
tableviewA.setModel(self.proxy2)
searchEdit=QLineEdit()
searchEdit.textChanged.connect(self.proxy2.setKeyword)
layout = QVBoxLayout(self)
layout.addWidget(tableviewA)
layout.addWidget(searchEdit)
self.setLayout(layout)
def test(self, arg):
print arg
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
这是一个单一的代理方法。与两个代理实现相比,按关键字搜索要慢得多:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class MyTableModel(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.items = [i for i in range(10000)]
def rowCount(self, parent):
return len(self.items)
def columnCount(self, parent):
return 1
def data(self, index, role):
if not index.isValid():
return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
row=index.row()
column=index.column()
if row<len(self.items):
return QVariant(self.items[row])
else:
return QVariant()
class Proxy01(QSortFilterProxyModel):
def __init__(self):
super(Proxy01, self).__init__()
self.keyword=None
def setKeyword(self, arg):
if arg: self.keyword=str(arg)
self.reset()
def filterAcceptsRow(self, row, parent):
sourceModel=self.sourceModel()
index=sourceModel.index(row, 0, parent)
name=sourceModel.data(index, Qt.DisplayRole).toString()
if self.keyword and name and not self.keyword.lower() in str(name).lower():
return False
return True
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
self.tablemodel=MyTableModel(self)
self.proxy1=Proxy01()
self.proxy1.setSourceModel(self.tablemodel)
tableviewA=QTableView()
tableviewA.setModel(self.proxy1)
searchEdit=QLineEdit()
searchEdit.textChanged.connect(self.proxy1.setKeyword)
layout = QVBoxLayout(self)
layout.addWidget(tableviewA)
layout.addWidget(searchEdit)
self.setLayout(layout)
def test(self, arg):
print arg
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())