def index(self, row, column, parent_index=None): if parent_index and parent_index.isValid(): parent_item = parent_index.internalPointer() else: parent_item = self.root_item children_list = get_obj_value(parent_item, 'children') if children_list and len(children_list) > row: child_item = children_list[row] if child_item: set_obj_value(child_item, '_parent', parent_item) return self.createIndex(row, column, child_item) return QModelIndex()
def _slot_set_select(self, column, state): current_model = self.model() source_model = utils.real_model(current_model) source_model.beginResetModel() attr = '{}_checked'.format(source_model.header_list[column].get('key')) for row in range(current_model.rowCount()): real_index = utils.real_index(current_model.index(row, column)) data_obj = real_index.internalPointer() if state is None: old_state = utils.get_obj_value(data_obj, attr) utils.set_obj_value(data_obj, attr, Qt.Unchecked if old_state == Qt.Checked else Qt.Checked) else: utils.set_obj_value(data_obj, attr, state) source_model.endResetModel() source_model.emit(SIGNAL('dataChanged(QModelIndex, QModelIndex)'), None, None)
def setData(self, index, value, role=Qt.EditRole): if index.isValid() and role in [Qt.CheckStateRole, Qt.EditRole]: attr_dict = self.header_list[index.column()] key = attr_dict.get('key') data_obj = index.internalPointer() if role == Qt.CheckStateRole and attr_dict.get('checkable', False): key += '_checked' # 更新自己 set_obj_value(data_obj, key, value) self.emit(SIGNAL('dataChanged(QModelIndex, QModelIndex)'), index, index) # 更新它的children for row, sub_obj in enumerate( get_obj_value(data_obj, 'children', [])): set_obj_value(sub_obj, key, value) sub_index = index.child(row, index.column()) self.emit(SIGNAL('dataChanged(QModelIndex, QModelIndex)'), sub_index, sub_index) # 更新它的parent parent_index = index.parent() if parent_index.isValid(): parent_obj = parent_index.internalPointer() new_parent_value = value old_parent_value = get_obj_value(parent_obj, key) for sibling_obj in get_obj_value( get_obj_value(data_obj, '_parent'), 'children', []): if value != get_obj_value(sibling_obj, key): new_parent_value = 1 break if new_parent_value != old_parent_value: set_obj_value(parent_obj, key, new_parent_value) self.emit( SIGNAL('dataChanged(QModelIndex, QModelIndex)'), parent_index, parent_index) else: set_obj_value(data_obj, key, value) # 采用 self.dataChanged.emit方式在houdini16里面会报错 # TypeError: dataChanged(QModelIndex,QModelIndex,QVector<int>) only accepts 3 arguments, 3 given! # 所以临时使用旧式信号的发射方式 self.emit(SIGNAL('dataChanged(QModelIndex, QModelIndex)'), index, index) # self.dataChanged.emit(index, index) return True else: return False
def test_set_obj_value(self, obj, attr, value): """Test set_obj_value with dict/object as arg.""" utils.set_obj_value(obj, attr, value) assert utils.get_obj_value(obj, attr) == value