我在源模型中附加了一个有孩子的行(称为Something
),我可以展开项目以查看其子项,但是当应用过滤器时,过滤的项目似乎不可扩展。< / p>
self.sf_proxy_model = QSortFilterProxyModel()
self.treeview.setModel(self.sf_proxy_model)
treeview_model = QStandardItemModel()
...defining source model for TreeView...
self.sf_proxy_model.setSourceModel(treeview_model)
过滤器实施:
def filter_regex_changed(self):
syntax = QRegExp.PatternSyntax(QRegExp.RegExp)
regexp = '^{}'.format(self.lineedit_quickfilter.text())
regexp = QRegExp(regexp, Qt.CaseInsensitive, syntax)
self.sf_proxy_model.setFilterRegExp(regexp)
这是创建行的函数:
def create_pin_group(self, group):
model = self.treeview.model()
model_source = self.treeview.model().sourceModel()
# Get child names from a list widget
filtered_pins = self.listview.model()
filtered_pins_text = []
for row in range(filtered_pins.rowCount()):
item = filtered_pins.index(row, 0)
filtered_pins_text.append(item.data(Qt.DisplayRole))
# Create a new group (row) and populate with filtered pins
row_name = QStandardItem(group['name'])
row_color = QStandardItem('')
row_color.setData(group['color'], Qt.UserRole)
row_color.setBackground(QColor(group['color']))
row_color.setEditable(False)
row_group = QStandardItem(group['name'])
row_group.setEditable(False)
row_type = QStandardItem(group['type'])
row_type.setTextAlignment(Qt.AlignCenter)
row_coupling_net = QStandardItem('')
row_coupling_net.setEditable(False)
row_data = [None] * model_source.columnCount()
row_data[self.column_name] = row_name
row_data[self.column_color] = row_color
row_data[self.column_group] = row_group
row_data[self.column_type] = row_type
row_data[self.column_coupling_net] = row_coupling_net
for pin in filtered_pins_text:
child_data = [None] * model_source.columnCount()
child_data[self.column_name] = QStandardItem(pin)
child_data[self.column_color] = row_color.clone()
child_data[self.column_group] = QStandardItem(group['name'])
child_data[self.column_type] = row_type.clone()
child_data[self.column_coupling_net] = QStandardItem('')
row_name.appendRow(child_data)
model_source.appendRow(row_data)
# Delete pins from source model
for pin in filtered_pins_text:
item = model_source.findItems(pin, Qt.MatchRecursive, self.column_name)[0]
parent = item.parent()
if parent.row() == -1: # Single pins
model_source.takeRow(item.row())
else:
if parent.rowCount() == 1: # One child remaining, delete entire last row
model_source.takeRow(parent.row())
else:
parent.takeRow(item.row())
model.sort(self.column_group, Qt.AscendingOrder)
model_source.sort(self.column_group, Qt.AscendingOrder)
我在这里缺少什么?