def data(self, QModelIndex, int_role = None): """ For initial role out, assume http://qt-project.org/doc/qt-4.8/qt.html#ItemDataRole-enum :param QModelIndex: :param int_role: :return: Qt::DisplayRole 0 The key data to be rendered in the form of text. (QString) Qt::DecorationRole 1 The data to be rendered as a decoration in the form of an icon. (QColor, QIcon or QPixmap) Qt::EditRole 2 The data in a form suitable for editing in an editor. (QString) Qt::ToolTipRole 3 The data displayed in the item's tooltip. (QString) Qt::StatusTipRole 4 The data displayed in the status bar. (QString) Qt::WhatsThisRole 5 The data displayed for the item in "What's This?" mode. (QString) Qt::SizeHintRole 13 The size hint for the item that will be supplied to views. (QSize) Roles describing appearance and meta data (with associated types): Constant Value Description Qt::FontRole 6 The font used for items rendered with the default delegate. (QFont) Qt::TextAlignmentRole 7 The alignment of the text for items rendered with the default delegate. (Qt::AlignmentFlag) Qt::BackgroundRole 8 The background brush used for items rendered with the default delegate. (QBrush) Qt::BackgroundColorRole 8 This role is obsolete. Use BackgroundRole instead. Qt::ForegroundRole 9 The foreground brush (text color, typically) used for items rendered with the default delegate. (QBrush) Qt::TextColorRole 9 This role is obsolete. Use ForegroundRole instead. Qt::CheckStateRole 10 This role is used to obtain the checked state of an item. (Qt::CheckState) Qt::InitialSortOrderRole 14 This role is used to obtain the initial sort order of a header view section. (Qt::SortOrder). This role was introduced in Qt 4.8. """ if int_role == 0: if QModelIndex.isValid(): i = QModelIndex.row() j = QModelIndex.column() return QString(str(self.DataFrame.ix[i, j])) else: return QVariant() else: return QVariant()
def _openPacketInfo(self): index = self.selectedIndexes()[0] index2 = QModelIndex(index) index = index2.sibling(index.row(), 0) if not index.isValid(): return self.openPacketInfo(index)
def updateSentence(self, QModelIndex): self.sentenceInCorrectionIndex = QModelIndex self.sentenceInCorrection = self.packedSentences[QModelIndex.row()] sentenceModel = sentenceCorrectionModel(self.sentenceInCorrection.getTokens()) self.ui.sentenceCorrectionView.setModel(sentenceModel) self.ui.sentenceList.scrollTo(QModelIndex) self.ui.sentenceIndex.setText(QString(str(QModelIndex.row())))
def rowCount(self, parent): if parent is None: parent = QModelIndex() if parent.column() > 0: return 0 if not parent.isValid(): return len(self.__root) else: return len(parent.internalPointer())
def _rItem2Index(self, item, parent=None): if parent is None: parent = QModelIndex() if item == self.getItem(parent): return parent if not parent.isValid() or parent.internalPointer().populated: for i in range(self.rowCount(parent)): index = self.index(i, 0, parent) index = self._rItem2Index(item, index) if index.isValid(): return index return QModelIndex()
def setData(self, ix: QtCore.QModelIndex, value: SqlDataType, role: int=QtCore.Qt.EditRole) -> bool: try: pk = self.visible_data[ix.row()][self.query_manager.table.primary_key_index] row = next( i for i, row in enumerate(self.modified_data) if row[self.query_manager.table.primary_key_index] == pk ) self.visible_data[ix.row()][ix.column()] = value self.modified_data[row][ix.column()] = value self.dataChanged.emit(ix, ix) return True except: return False
def index(self, row, column, parent=None): if parent is None: parent = QModelIndex() if not self.hasIndex(row, column, parent): return QModelIndex() if not parent.isValid(): parent_item = self.__root else: parent_item = parent.internalPointer() child_item = parent_item.child(row) return self.createIndex(row, column, child_item)
def flags(self, ix: QtCore.QModelIndex) -> int: if ix.column() in self.query_manager.editable_fields_indices: return ( QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable ) return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def delete_row(self, ix: QtCore.QModelIndex) -> None: row = ix.row() pk = self.visible_data[row][self.query_manager.table.primary_key_index] mod_row = next( i for i, r in enumerate(self.modified_data) if r[self.query_manager.table.primary_key_index] == pk ) del self.visible_data[row] del self.modified_data[mod_row] self.dataChanged.emit(ix, ix)
def data(self, index: QtCore.QModelIndex, role: int=QtCore.Qt.DisplayRole): alignment = { FieldType.bool: QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter, FieldType.date: QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter, FieldType.int: QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter, FieldType.float: QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter, FieldType.str: QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter, } col = index.column() fld = self.query_manager.table.fields[col] val = self.visible_data[index.row()][col] try: if not index.isValid(): return elif role == QtCore.Qt.TextAlignmentRole: return alignment[fld.dtype] elif role == QtCore.Qt.DisplayRole: if col in self.foreign_keys.keys(): return self.foreign_keys[col][val] return fld.format_value(val) except Exception as e: self.error_signal.emit('Error modeling data: {}'.format(e))
def add_row(self, ix: QtCore.QModelIndex) -> None: dummies = { FieldType.bool: True , FieldType.int: 0 , FieldType.float: 0.0 , FieldType.str: '' , FieldType.date: '1900-01-01' } dummy_row = [] # type: List for fld in self.query_manager.table.fields: dummy_row.append(dummies[fld.dtype]) for k, v in self.query_manager.table.foreign_keys.items(): dummy_row[k] = next(fk for fk in self.foreign_keys[k]) dummy_row[self.query_manager.table.primary_key_index] = uuid.uuid4().int self.visible_data.insert(ix.row(), dummy_row) self.modified_data.insert(0, dummy_row) self.dataChanged.emit(ix, ix)
def __update(self): """Update the current description. """ if self.__currentIndex != -1: index = self.model().index(self.__currentIndex, 0) else: index = QModelIndex() if not index.isValid(): description = "" name = "" path = "" svg = NO_PREVIEW_SVG else: description = qtcompat.qunwrap(index.data(Qt.WhatsThisRole)) if description: description = six.text_type(description) else: description = u"No description." description = escape(description) description = description.replace("\n", "<br/>") name = qtcompat.qunwrap(index.data(Qt.DisplayRole)) if name: name = six.text_type(name) else: name = "Untitled" name = escape(name) path = qtcompat.qunwrap(index.data(Qt.StatusTipRole)) path = six.text_type(path) svg = qtcompat.qunwrap(index.data(previewmodel.ThumbnailSVGRole)) svg = six.text_type(svg) desc_text = self.__template.format(description=description, name=name) self.__label.setText(desc_text) self.__path.setText(path) if not svg: svg = NO_PREVIEW_SVG if svg: self.__image.load(QByteArray(svg.encode("utf-8")))
def isFirstMatchChecked(self): """Check if first file in the search results is expanded """ file = self.index(0, 0, QModelIndex()) match = file.child(0, 0) return match.data(Qt.CheckStateRole) == Qt.Checked
def parentFromIndex(self, index): index = QModelIndex(index) # explicitly convert from QPersistentModelIndex if index.isValid(): return index.internalPointer() return self._root
def insertRows(self, row, count, parent=QModelIndex()): """ On models that support this, inserts count rows into the
def rowCount(self, index=QModelIndex()): return len(self._elemvar)
def isChildOfRoot(self, index): return self.parent(index) == QModelIndex()
def columnCount(self, parent=QModelIndex()): """ Model-method, called by the view to determine how many columns are to be displayed at a given time. :param parent: """ return len(self.header)
def removeRow(self, position, parent=QModelIndex()): self.boxRemoved.emit(position) return super(BoxListModel, self).removeRow(position, parent=parent)
def new_annotation(self, note): l = len(self.packets) self.beginInsertRows(QModelIndex(), l, l) self.packets.append("*** " + str(note)) self.endInsertRows()
def new_packet(self, pack): l = len(self.packets) self.first_ts = self.first_ts or pack.ts_sec + pack.ts_usec/1e6 self.beginInsertRows(QModelIndex(), l, l) self.packets.append(pack) self.endInsertRows()
def removeRows(self, first, count, parent = None): last = first + count - 1 self.beginRemoveRows(QModelIndex(), first, last) self.packets = self.packets[:first] + self.packets[last+1:] self.endRemoveRows() return True
def columnCount(self, index=QModelIndex()): return self.df.shape[1]
def rowCount(self, index=QModelIndex()): return self.df.shape[0]
def index(self, row, column=0, parent=QModelIndex()): if self._is_index_valid_for(row, self) and column == 0: return QAbstractListModel.createIndex(self, row, column, parent) else: return QModelIndex()
def insertRows(self, row, rows=1, index=QModelIndex()): self.beginInsertRows(QModelIndex(), row, row + rows - 1) tmp_entity = Entity() self.endInsertRows() return True
def index(self, row, column, parent): node = self.nodeFromIndex(parent) if row < len(node): return self.createIndex(row, column, node.childAtRow(row)) else: return QModelIndex()
def columnCount(self, parent=QModelIndex()): return 2
def columnCount(self, index=QModelIndex()): return 3 + len(self._snaplist) + 1
def rowCount(self, parent=QModelIndex()): return 0 if parent.isValid() else len(self)
def itemFromIndex(self, index): index = QModelIndex(index) # explicitly convert from QPersistentModelIndex if index.isValid(): return index.internalPointer().childAt(index.row()) return self._root
def columnCount(self, parent=QModelIndex()): return 0 if parent.isValid() else 1
def clear(self): """Clear all results """ self.beginRemoveRows(QModelIndex(), 0, len(self.fileResults) - 1) self.fileResults = [] self.endRemoveRows()
def parent(self, index=QModelIndex()): return QModelIndex()
def columnCount(self, parent=QModelIndex()): if parent.isValid(): return 0 return self.column_count
def rowCount(self, parent=QModelIndex()): if parent.isValid(): return 0 return len(self.rows)
def rowCount(self, parent=QModelIndex()): if not parent.isValid(): return len(self._layerStack) return 0
def insert_left_data(self, new_rows, size): modelIndex = QModelIndex() self.beginInsertRows(modelIndex, 0, size - 1) for row in new_rows: self.rows.appendleft(row) self.endInsertRows()
def selectedIndex(self): row = self.selectedRow() if row >= 0: return self.index(self.selectedRow()) else: return QModelIndex()
def hasChildren(self, parent=QModelIndex()): if parent.isValid(): return False return True
def parent(self, index=QModelIndex()): """Reimplemented from `QAbstractTableModel.parent`.""" return QModelIndex()
def columnCount(self, parent=QModelIndex()): """Reimplemented from `QAbstractTableModel.columnCount`.""" return 0 if parent.isValid() else self.__columnCount
def insert(self, i, val): self.beginInsertRows(QModelIndex(), i, i) self._list.insert(i, val) self._other_data.insert(i, _store()) self.endInsertRows()
item1 = QStandardItem() item1.setText("B") pixmap1 = QPixmap(50, 50) pixmap1.fill(Qt.blue) item1.setIcon(QIcon(pixmap1)) item1.setToolTip("indexB") parentItem.appendRow(item1) # 创建新的标准项,这里使用了另一种方法来设置文本、图标和工具提示 item2 = QStandardItem() pixmap2 = QPixmap(50, 50) pixmap2.fill(Qt.green) item2.setData("C", Qt.EditRole) # 等同于 setText("C") item2.setData("indexC", Qt.ToolTipRole) # 等同于 setToolTip("indexB") item2.setData(QIcon(pixmap2), Qt.DecorationRole) #等同于 setIcon(QIcon(pixmap1)) parentItem.appendRow(item2) # 在树视图中显示模型 view.setModel(model) view.show() # 获取item0的索引并输出item0的子项数目,然后输出了item1的显示文本和工具提示 indexA = model.index(0, 0, QModelIndex()) print "indexA row count: ", model.rowCount(indexA) indexB = model.index(0, 0, indexA) print "indexB text: ", model.data(indexB, Qt.EditRole).toString() print "indexB toolTip: ", model.data(indexB, Qt.ToolTipRole).toString() app.exec_()