def setData(model, index, var, role=Qt.EditRole): """ Sets the role data for the item at index to var. var is a QVariant (called data in documentation) """ print("[model] setData: %r" % (str(qtype.qindexinfo(index)))) try: if not index.isValid(): return None flags = model.flags(index) if not (flags & Qt.ItemIsEditable or flags & Qt.ItemIsUserCheckable): return None if role == Qt.CheckStateRole: type_ = "QtCheckState" data = var == Qt.Checked elif role != Qt.EditRole: return False else: # Cast var into datatype type_ = model.get_coltype(index.column()) data = qtype.cast_from_qt(var, type_) # Do actual setting of data print(" * new_data = %s(%r)" % (utool.type_str(type_), data)) model.set_data(index, data) # Emit that data was changed and return succcess model.dataChanged.emit(index, index) return True except Exception as ex: var_ = str(var.toString()) # NOQA utool.printex(ex, "ignoring setData", "[model]", key_list=["var_"]) # raise # print(' * ignoring setData: %r' % locals().get('var', None)) return False
def setData(model, qtindex, value, role=Qt.EditRole): """ Sets the role data for the item at qtindex to value. value is a QVariant (called data in documentation) Returns a map with values for all predefined roles in the model for the item at the given index. Reimplement this function if you want to extend the default behavior of this function to include custom roles in the map. """ try: if not qtindex.isValid(): return None flags = model.flags(qtindex) #row = qtindex.row() col = qtindex.column() if not (flags & Qt.ItemIsEditable or flags & Qt.ItemIsUserCheckable): return None if role == Qt.CheckStateRole: type_ = 'QtCheckState' data = (value == Qt.Checked) elif role != Qt.EditRole: return False else: # Cast value into datatype type_ = model.col_type_list[col] data = qtype.cast_from_qt(value, type_) # Do actual setting of data old_data = model._get_data(qtindex) if old_data != data: model._set_data(qtindex, data) # This may not work with PyQt5 # http://stackoverflow.com/questions/22560296/pyqt-list-view-not-responding-to-datachanged-signal # Emit that data was changed and return succcess model.dataChanged.emit(qtindex, qtindex) return True except Exception as ex: #value = str(value.toString()) # NOQA ut.printex(ex, 'ignoring setData', '[model]', tb=True, key_list=['value'], iswarning=True) return False
def setData(model, index, var, role=Qt.EditRole): """ Sets the role data for the item at index to var. var is a QVariant (called data in documentation) """ print('[model] setData: %r' % (str(qtype.qindexinfo(index)))) try: if not index.isValid(): return None flags = model.flags(index) if not (flags & Qt.ItemIsEditable or flags & Qt.ItemIsUserCheckable): return None if role == Qt.CheckStateRole: type_ = 'QtCheckState' data = var == Qt.Checked elif role != Qt.EditRole: return False else: # Cast var into datatype type_ = model.get_coltype(index.column()) data = qtype.cast_from_qt(var, type_) # Do actual setting of data print(' * new_data = %s(%r)' % ( utool.type_str(type_), data, )) model.set_data(index, data) # Emit that data was changed and return succcess model.dataChanged.emit(index, index) return True except Exception as ex: var_ = str(var.toString()) # NOQA utool.printex(ex, 'ignoring setData', '[model]', key_list=['var_']) #raise #print(' * ignoring setData: %r' % locals().get('var', None)) return False