'Remove trailing spaces'

def remove_trailing_spaces(self):

    cursor = self.textCursor()
    cursor.beginEditBlock()
    cursor.movePosition(QTextCursor.Start)
    while True:
        cursor.movePosition(QTextCursor.EndOfBlock)
        text = to_text_string(cursor.block().text())
        length = (len(text) - len(text.rstrip()))
        if (length > 0):
            cursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, length)
            cursor.removeSelectedText()
        if cursor.atEnd():
            break
        cursor.movePosition(QTextCursor.NextBlock)
    cursor.endEditBlock()
