1 回答
TA贡献1873条经验 获得超9个赞
为了实现您的要求,我通过以下方式实现了几个连接的代理:
model-->Table2ListProxyModel-->QSortFilterProxyModel-->List2TableProxyModel
想法是将其转换为列表的结构,因为过滤一行相当于过滤一个项目,相同的代理对其进行排序,然后我们将列表转换为表格。
import math
from PySide import QtCore, QtGui
class Table2ListProxyModel(QtGui.QSortFilterProxyModel):
def columnCount(self, parent=QtCore.QModelIndex()):
return 1
def rowCount(self, parent=QtCore.QModelIndex()):
if parent.isValid():
return 0
return self.sourceModel().rowCount() * self.sourceModel().columnCount()
def mapFromSource(self, sourceIndex):
if (
sourceIndex.isValid()
and sourceIndex.column() == 0
and sourceIndex.row() < self.rowCount()
):
r = sourceIndex.row()
c = sourceIndex.column()
row = c * sourceIndex.model().columnCount() + r
return self.index(row, 0)
return QtCore.QModelIndex()
def mapToSource(self, proxyIndex):
r = proxyIndex.row() / self.sourceModel().columnCount()
c = proxyIndex.row() % self.sourceModel().columnCount()
return self.sourceModel().index(r, c)
def index(self, row, column, parent=QtCore.QModelIndex()):
return self.createIndex(row, column)
class List2TableProxyModel(QtGui.QSortFilterProxyModel):
def __init__(self, columns=1, parent=None):
super(List2TableProxyModel, self).__init__(parent)
self._columns = columns
def columnCount(self, parent=QtCore.QModelIndex()):
r = self.sourceModel().rowCount()
if r < self._columns:
return r
return self._columns
def rowCount(self, parent=QtCore.QModelIndex()):
if parent.isValid():
return 0
row = math.ceil(self.sourceModel().rowCount()*1.0 / self._columns)
return row
def index(self, row, column, parent=QtCore.QModelIndex()):
return self.createIndex(row, column)
def data(self, index, role=QtCore.Qt.DisplayRole):
r = index.row()
c = index.column()
row = r * self.columnCount() + c
if row < self.sourceModel().rowCount():
return super(List2TableProxyModel, self).data(index, role)
def mapFromSource(self, sourceIndex):
r = math.ceil(sourceIndex.row()*1.0 / self.columnCount())
c = sourceIndex.row() % self.columnCount()
return self.index(r, c)
def mapToSource(self, proxyIndex):
if proxyIndex.isValid():
r = proxyIndex.row()
c = proxyIndex.column()
row = r * self.columnCount() + c
return self.sourceModel().index(row, 0)
return QtCore.QModelIndex()
def setSourceModel(self, model):
model.rowsRemoved.connect(self.reset_model)
model.rowsInserted.connect(self.reset_model)
model.dataChanged.connect(self.reset_model)
model.rowsMoved.connect(self.reset_model)
model.layoutChanged.connect(self.reset_model)
model.modelReset.connect(self.reset_model)
super(List2TableProxyModel, self).setSourceModel(model)
@QtCore.Slot()
def reset_model(self):
QtCore.QTimer.singleShot(0, self.invalidate)
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
if role == QtCore.Qt.DisplayRole:
return str(section)
return super(List2TableProxyModel, self).headerData(
section, orientation, role
)
class CustomTableView(QtGui.QTableView):
def __init__(self, parent=None):
super(CustomTableView, self).__init__(parent)
custom_model = QtGui.QStandardItemModel()
datas = (("ad", "cd", "ef"), ("ab", "ce", "eg"), ("aa", "cb", "eh"))
for i, data in enumerate(datas):
for v, text in enumerate(data):
custom_model.setItem(i, v, QtGui.QStandardItem(text))
self.proxy_list = Table2ListProxyModel(self)
self.proxy_list.setSourceModel(custom_model)
self.proxy_sort_filter = QtGui.QSortFilterProxyModel(self)
self.proxy_sort_filter.setSourceModel(self.proxy_list)
self.proxy_table = List2TableProxyModel(
columns=custom_model.columnCount(), parent=self
)
self.proxy_table.setSourceModel(self.proxy_sort_filter)
self.setModel(self.proxy_table)
@QtCore.Slot(str)
def setFilter(self, text):
self.proxy_sort_filter.setFilterWildcard(
"*{}*".format(text) if text else ""
)
self.proxy_sort_filter.sort(0 if text else -1, QtCore.Qt.AscendingOrder)
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.line_edit = QtGui.QLineEdit()
self.tableview = CustomTableView()
lay = QtGui.QVBoxLayout(self)
lay.addWidget(self.line_edit)
lay.addWidget(self.tableview)
self.line_edit.textChanged.connect(self.tableview.setFilter)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
添加回答
举报