(self, index)
Delete item by index
Delete item by index
Delete item by index
def __delitem__(self, index):
        """Delete item by index
        """
        def _removeBlock(blockIndex):
            block = self._doc.findBlockByNumber(blockIndex)
            if block.next().isValid():  # not the last
                cursor = QTextCursor(block)
                cursor.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor)
            elif block.previous().isValid():  # the last, not the first
                cursor = QTextCursor(block.previous())
                cursor.movePosition(QTextCursor.EndOfBlock)
                cursor.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor)
                cursor.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
            else:  # only one block
                cursor = QTextCursor(block)
                cursor.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
            cursor.removeSelectedText()

        if isinstance(index, int):
            index = self._checkAndConvertIndex(index)
            _removeBlock(index)
        elif isinstance(index, slice):
            # List of indexes is reversed for make sure
            # not processed indexes are not shifted during document modification
            start, stop, step = index.indices(self._doc.blockCount())
            if step > 0:
                start, stop, step = stop - 1, start - 1, step * -1

            for blockIndex in range(start, stop, step):
                _removeBlock(blockIndex)
def __delitem__ ( self , index ) : def _removeBlock ( blockIndex ) : block = self . _doc . findBlockByNumber ( blockIndex ) if block . next ( ) . isValid ( ) : # not the last cursor = QTextCursor ( block ) cursor . movePosition ( QTextCursor . NextBlock , QTextCursor . KeepAnchor ) elif block . previous ( ) . isValid ( ) : # the last, not the first cursor = QTextCursor ( block . previous ( ) ) cursor . movePosition ( QTextCursor . EndOfBlock ) cursor . movePosition ( QTextCursor . NextBlock , QTextCursor . KeepAnchor ) cursor . movePosition ( QTextCursor . EndOfBlock , QTextCursor . KeepAnchor ) else : # only one block cursor = QTextCursor ( block ) cursor . movePosition ( QTextCursor . EndOfBlock , QTextCursor . KeepAnchor ) cursor . removeSelectedText ( ) if isinstance ( index , int ) : index = self . _checkAndConvertIndex ( index ) _removeBlock ( index ) elif isinstance ( index , slice ) : # List of indexes is reversed for make sure # not processed indexes are not shifted during document modification start , stop , step = index . indices ( self . _doc . blockCount ( ) ) if step > 0 : start , stop , step = stop - 1 , start - 1 , step * - 1 for blockIndex in range ( start , stop , step ) : _removeBlock ( blockIndex )