我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用PyQt5.QtCore.Qt.EditRole()。
def setData(self, index, value, role): if index.isValid() and role == Qt.EditRole: row = index.row() col = index.column() if col == 0 or col == 1: if col == 0: self._mp_database.db[row].name = value elif col == 1: self._mp_database.db[row].state = value self.dataChanged.emit(index, index) # P R I V A T E M E T H O D S #===========================================================================
def update_flight_data(self, parameter, value): if parameter in KPFlightDataModel.flight_data_lookup: row = KPFlightDataModel.flight_data_lookup.index(parameter) col = 2 model_index = self.createIndex(row, col) # handle special formatting if parameter == 'vessel_rotation': value = "({:.3f},{:.3f},{:.3f},{:.3f})".format(value[0], value[1], value[2], value[3]) self.setData(model_index, QVariant(value), Qt.EditRole) # O V E R R I D E M E T H O D S #===========================================================================
def setData(self, index, value, role): if index.isValid() and role == Qt.EditRole: row = index.row() col = index.column() self._flight_data[row][col] = value self.dataChanged.emit(index, index) # P R I V A T E M E T H O D S #=========================================================================== # S L O T S #=========================================================================== # H E L P E R F U N C T I O N S #===========================================================================
def test_setData(self): # only if the returned string can be parsed it should be accepted item = PropertyItem(self.props["List"]) edit_str = item.data(role=Qt.EditRole) self.assertIsInstance(edit_str, str) self.assertEqual(edit_str, str(self.props["List"])) new_list = [1, 3, 5] item.setData(new_list, role=PropertyItem.RawDataRole) self.assertEqual(item.data(role=PropertyItem.RawDataRole), new_list) new_str = str(new_list) self.assertEqual(item.data(role=Qt.DisplayRole), new_str) # invalid data should be rejected and exception should be printed to log item.setData(edit_str[:-1], role=Qt.EditRole) self.assertEqual(item.data(role=Qt.DisplayRole), new_str)
def setData(self, index, value, role): """Set the role data for the item at index to value.""" if not index.isValid(): return False node = self.getNode(index) column = index.column() if role == Qt.DisplayRole or role == Qt.EditRole: if column > 0 and not node.childCount(): try: node.setItemData(column, float(value)) except ValueError: return False else: node.setItemData(column, value) elif role == Qt.CheckStateRole: node.setCheckState(value) if value == Qt.Checked: self.nodeCheckStateChanged.emit(index) self.dataChanged.emit(index, index) return True
def createEditor(self, parent, styleOption, index): if index.column() == 1: editor = QDateTimeEdit(parent) editor.setDisplayFormat(self.parent().currentDateFormat) editor.setCalendarPopup(True) return editor editor = QLineEdit(parent) # create a completer with the strings in the column as model allStrings = [] for i in range(1, index.model().rowCount()): strItem = index.model().data(index.sibling(i, index.column()), Qt.EditRole) if strItem not in allStrings: allStrings.append(strItem) autoComplete = QCompleter(allStrings) editor.setCompleter(autoComplete) editor.editingFinished.connect(self.commitAndCloseEditor) return editor
def data(self, role): if role in (Qt.EditRole, Qt.StatusTipRole): return self.formula() if role == Qt.DisplayRole: return self.display() t = str(self.display()) try: number = int(t) except ValueError: number = None if role == Qt.TextColorRole: if number is None: return QColor(Qt.black) elif number < 0: return QColor(Qt.red) return QColor(Qt.blue) if role == Qt.TextAlignmentRole: if t and (t[0].isdigit() or t[0] == '-'): return Qt.AlignRight | Qt.AlignVCenter return super(SpreadSheetItem, self).data(role)
def test_label_tooltip(self): self.cfc.ui.cbProtoView.setCurrentIndex(0) self.cfc.add_protocol_label(0, 16, 2, 0, edit_label_name=False) model = self.cfc.protocol_label_list_model model.setData(model.index(0, 0), "test", Qt.EditRole) table_model = self.cfc.protocol_model for i in range(0, 16): self.assertEqual(table_model.data(table_model.index(2, i), Qt.ToolTipRole), "test", msg=str(i)) for i in range(17, 100): self.assertEqual(table_model.data(table_model.index(2, i), Qt.ToolTipRole), "", msg=str(i)) self.cfc.add_protocol_label(20, 24, 2, 0, edit_label_name=False) checksum_field_type = next(ft for ft in self.cfc.field_types if ft.function == FieldType.Function.CHECKSUM) model.setData(model.index(1, 0), checksum_field_type.caption, Qt.EditRole) for i in range(20, 24): self.assertIn("Expected", table_model.data(table_model.index(2, i), Qt.ToolTipRole)) for i in range(0, 20): self.assertNotIn("Expected", table_model.data(table_model.index(2, i), Qt.ToolTipRole))
def setData(self, index: QModelIndex, value, role=None): if role == Qt.EditRole: i, j = index.row(), index.column() fieldtype = self.field_types[i] try: if j == 0: present_captions = {ft.caption for ft in self.field_types} if value not in present_captions: fieldtype.caption = value elif j == 1: try: fieldtype.function = FieldType.Function[value] except KeyError: return False if j == 2: fieldtype.display_format_index = int(value) except ValueError: return False return True
def setData(self, index: QModelIndex, value, role=None): if role == Qt.EditRole: i, j = index.row(), index.column() rule = self.ruleset[i] try: if j == 0: rule.start = int(value) - 1 elif j == 1: rule.end = int(value) if j == 2: rule.value_type = int(value) if j == 3: rule.operator_description = self.operator_descriptions[int(value)] if j == 4: rule.target_value = value except ValueError: return False return True
def _set_program_state(self, program_num, state): if program_num < len(self._mp_database.db): row = program_num col = 1 model_index = self.createIndex(row, col) self.setData(model_index, QVariant(state), Qt.EditRole) # S L O T S #===========================================================================
def execute_mcu_code(self): idx = self.mcuFilesListView.currentIndex() assert isinstance(idx, QModelIndex) model = self.mcuFilesListView.model() assert isinstance(model, QStringListModel) file_name = model.data(idx, Qt.EditRole) self._connection.run_file(file_name)
def remove_file(self): idx = self.mcuFilesListView.currentIndex() assert isinstance(idx, QModelIndex) model = self.mcuFilesListView.model() assert isinstance(model, QStringListModel) file_name = model.data(idx, Qt.EditRole) try: self._connection.remove_file(file_name) except OperationError: QMessageBox().critical(self, "Operation failed", "Could not remove the file.", QMessageBox.Ok) return self.list_mcu_files()
def read_mcu_file(self, idx): assert isinstance(idx, QModelIndex) model = self.mcuFilesListView.model() assert isinstance(model, QStringListModel) file_name = model.data(idx, Qt.EditRole) if not file_name.endswith(".py"): QMessageBox.information(self, "Unknown file", "Files without .py ending won't open" " in editor, but can still be transferred.") return progress_dlg = FileTransferDialog(FileTransferDialog.DOWNLOAD) progress_dlg.finished.connect(lambda: self.finished_read_mcu_file(file_name, progress_dlg.transfer)) progress_dlg.show() self._connection.read_file(file_name, progress_dlg.transfer)
def transfer_to_pc(self): idx = self.mcuFilesListView.currentIndex() assert isinstance(idx, QModelIndex) model = self.mcuFilesListView.model() assert isinstance(model, QStringListModel) remote_path = model.data(idx, Qt.EditRole) local_path = self.localPathEdit.text() + "/" + remote_path progress_dlg = FileTransferDialog(FileTransferDialog.DOWNLOAD) progress_dlg.finished.connect(lambda: self.finished_transfer_to_pc(local_path, progress_dlg.transfer)) progress_dlg.show() self._connection.read_file(remote_path, progress_dlg.transfer)
def setEditorData(self, editor, index): value = index.model().data(index, Qt.EditRole) editor.setCurrentIndex(editor.findText(value)) editor.showPopup()
def setModelData(self, editor, model, index): value = editor.currentText() model.setData(index, value, Qt.EditRole)
def setEditorData(self, editor, index): data = index.model().itemFromIndex(index) if isinstance(data, QStandardItem): text = index.model().data(index, Qt.EditRole) editor.setPlainText(text) editor.selectAll() else: super(CommentListViewDelegate, self).setEditorData(editor, index)
def setModelData(self, editor, model, index): data = index.model().itemFromIndex(index) if isinstance(data, QStandardItem): text = editor.toPlainText().strip() model.setData(index, text, Qt.EditRole) resizeCommentListViewToContents() else: super(CommentListViewDelegate, self).setModelData(editor, model, index)
def data(self, index, role=None): if role == Qt.DisplayRole: return self.m_data[index.row()][index.column()] elif role == Qt.EditRole: return self.m_data[index.row()][index.column()] elif role == Qt.BackgroundRole: for color, rect in self.m_mapping.items(): if rect.contains(index.column(), index.row()): return QColor(color) return QVariant()
def setData(self, index, value, role=None): if index.isValid() and role == Qt.EditRole: self.m_data[index.row()][index.column()] = int(value) self.dataChanged.emit(index,index) self.signal_update_models.emit() return True return False
def data(self, index, role=Qt.DisplayRole): if index.isValid() and role in (Qt.DisplayRole, Qt.EditRole): row, column = index.row(), index.column() return self._data(row, column) return None
def setData(self, index, value, role=Qt.EditRole): if index.isValid() and role in (Qt.DisplayRole, Qt.EditRole): row, column = index.row(), index.column() oldValue = self._data(row, column) self._setData(row, column, value) if not self._inDrop: self.valueChanged.emit(index, oldValue, value) self.dataChanged.emit(index, index, [role]) return True return super(AbstractListModel, self).setData(index, value, role)
def createEditor(self, parent, option, index): data = index.data(Qt.EditRole) if isinstance(data, bool): checkBox = QCheckBox(parent) checkBox.setChecked(data) checkBox.toggled.connect( lambda: self.setModelData(checkBox, index.model(), index)) return checkBox elif isinstance(data, QColor): # we have our own ways return None return super(ListItemDelegate, self).createEditor(parent, option, index)
def setModelData(self, editor, model, index): data = index.data(Qt.EditRole) if isinstance(data, bool): value = editor.isChecked() model.setData(index, value) else: super().setModelData(editor, model, index)
def data(self, index, role): """Return role specific data for the item referred by the index.""" if not index.isValid(): return if role == Qt.DisplayRole or role == Qt.EditRole: label = self.data[index.row()].label return label
def data(self, index, role): """Return role specific data for the item referred by index.column().""" if not index.isValid(): pass node = self.getNode(index) column = index.column() value = node.getItemData(column) if role == Qt.DisplayRole: try: if column == 1: return '{0:8.3f}'.format(value) else: return '{0:8.2f}'.format(value) except ValueError: return value if role == Qt.EditRole: return str(value) if role == Qt.CheckStateRole: if node.parent == self.rootNode and column == 0: return node.getCheckState() if role == Qt.TextAlignmentRole: if column > 0: return Qt.AlignRight
def setEditorData(self, spinBox, index): value = index.model().data(index, Qt.EditRole) spinBox.setValue(value)
def setModelData(self, spinBox, model, index): spinBox.interpretText() value = spinBox.value() model.setData(index, value, Qt.EditRole)
def updateLineEdit(self, item): if item != self.table.currentItem(): return if item: self.formulaInput.setText(item.data(Qt.EditRole)) else: self.formulaInput.clear()
def returnPressed(self): text = self.formulaInput.text() row = self.table.currentRow() col = self.table.currentColumn() item = self.table.item(row, col) if not item: self.table.setItem(row, col, SpreadSheetItem(text)) else: item.setData(Qt.EditRole, text) self.table.viewport().update()
def setEditorData(self, editor, index): if isinstance(editor, QLineEdit): editor.setText(index.model().data(index, Qt.DisplayRole)) elif isinstance(editor, QSpinBox): value = int(index.model().data(index, Qt.EditRole)) editor.setValue(value)
def data(self, QModelIndex, role=None): """ ??? ???? ???? ? View? Model.data? ?? View? ??? role? ???? ???? ?? ?? ???? role? ?? ??? ?? ?? QVariant? ?? """ item = self._data[QModelIndex.row()] if role == Qt.DisplayRole: # ? ??? return "%s" % item['name'] elif role == Qt.EditRole: # ? ??? return "%s" % item['name'] return QVariant()
def data(self, index, role=Qt.DisplayRole): if role == Qt.DisplayRole: return self.display_list[index.row()] elif role == Qt.DecorationRole: return None elif role == Qt.ToolTipRole: return None elif role == Qt.EditRole: return self.display_list[index.row()]
def setData(self, index, value, role=Qt.EditRole): if role == Qt.EditRole: obj = self.reddit_object_list[index] if value in self.display_list: return False else: self.reddit_object_list[index] = value self.display_list.remove(obj) self.display_list.append(value.name)
def test_edit_rules(self): num_rules = len(self.message_type.ruleset) self.assertEqual(num_rules, self.dialog.ruleset_table_model.rowCount()) self.dialog.ui.rbAssignAutomatically.click() self.assertFalse(self.message_type.assign_manually) self.dialog.ui.rbAssignManually.click() self.assertTrue(self.message_type.assign_manually) self.dialog.ui.rbAssignAutomatically.click() self.assertTrue(self.dialog.ui.btnAddRule.isEnabled()) self.dialog.ui.btnAddRule.click() self.assertEqual(num_rules + 1, len(self.message_type.ruleset)) self.assertEqual(num_rules + 1, self.dialog.ruleset_table_model.rowCount()) model = self.dialog.ruleset_table_model model.setData(model.index(0, 0), 10, role=Qt.EditRole) self.assertEqual(self.message_type.ruleset[0].start, 9) model.setData(model.index(0, 1), 20, role=Qt.EditRole) self.assertEqual(self.message_type.ruleset[0].end, 20) model.setData(model.index(0, 2), 2, role=Qt.EditRole) self.assertEqual(self.message_type.ruleset[0].value_type, 2) model.setData(model.index(0, 3), 2, role=Qt.EditRole) model.setData(model.index(0, 4), "10101", role=Qt.EditRole) self.assertEqual(self.message_type.ruleset[0].target_value, "10101") for i in range(model.rowCount()): for j in range(model.columnCount()): self.assertEqual(model.flags(model.index(i, j)), Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable) self.dialog.ui.btnRemoveRule.click() self.assertEqual(num_rules, len(self.message_type.ruleset)) self.assertEqual(num_rules, self.dialog.ruleset_table_model.rowCount())
def __set_model_data(self, model, row, column, value): model.setData(model.createIndex(row, column), value, role=Qt.EditRole)
def __set_label_name(self, index: int, name: str): list_model = self.form.compare_frame_controller.ui.listViewLabelNames.model() list_model.setData(list_model.createIndex(index, 0), name, role=Qt.EditRole)
def setData(self, index: QModelIndex, value, role=Qt.EditRole): if value == "": return True i = index.row() j = index.column() if i >= len(self.message_type): return False lbl = self.message_type[i] if j == 0: lbl.name = value type_before = type(lbl) self.message_type.change_field_type_of_label(lbl, self.field_types_by_caption.get(value, None)) if type_before != ProtocolLabel or type(self.message_type[i]) != ProtocolLabel: self.special_status_label_changed.emit(self.message_type[i]) elif j == 1: lbl.start = self.message.convert_index(int(value-1), from_view=self.proto_view, to_view=0, decoded=True)[0] elif j == 2: lbl.end = self.message.convert_index(int(value), from_view=self.proto_view, to_view=0, decoded=True)[0] elif j == 3: lbl.color_index = value elif j == 4: if bool(value) != lbl.apply_decoding: lbl.apply_decoding = bool(value) self.apply_decoding_changed.emit(lbl) return True
def setData(self, index: QModelIndex, value, role=None): item = self.getItem(index) if role == Qt.EditRole and len(value) > 0: item.setData(value) return True elif role == Qt.CheckStateRole: item.show = value return True return False