title: Change text selection in PyQt QTextEdit
body: <p>I'm trying to select different text in a QTextEdit object.</p>
  
  <pre><code>def get_text_cursor(self):
      return self.TextEdit.textCursor()
  
  def get_text_selection(self):
      cursor = self.get_text_cursor()
      return cursor.selectionStart(), cursor.selectionEnd()
  
  def set_text_selection(self, start, end):
      cursor = self.get_text_cursor()
      cursor.setPosition(start, end)
      self.TextEdit.setTextCursor(cursor)
  </code></pre>
  
  <p>This code does NOT work (<code>get_text_selection</code> does work) I have tried other things as well and they don't work either.</p>
  
  <p>This question was already asked (but not really answered) here
  <a href="https://stackoverflow.com/questions/15191565/select-text-of-textedit-object-with-qtextcursor-qtextedit">Select text of textEdit object with QTextCursor, QTextEdit</a></p>
  
  <p><strong>Working code, thanks to ekhumoro</strong></p>
  
  <pre><code># text cursor functions
  def get_text_cursor(self):
      return self.TextEdit.textCursor()
  
  def set_text_cursor_pos(self, value):
      tc = self.get_text_cursor()
      tc.setPosition(value, QtGui.QTextCursor.KeepAnchor)
      self.TextEdit.setTextCursor(tc)
  
  def get_text_cursor_pos(self):
      return self.get_text_cursor().position()
  
  def get_text_selection(self):
      cursor = self.get_text_cursor()
      return cursor.selectionStart(), cursor.selectionEnd()
  
  def set_text_selection(self, start, end):
      cursor = self.get_text_cursor()
      cursor.setPosition(start)
      cursor.setPosition(end, QtGui.QTextCursor.KeepAnchor)
      self.TextEdit.setTextCursor(cursor)
  </code></pre>
  
  <p>You can see this code in action at <a href="https://github.com/cloudformdesign/SearchTheSky" rel="nofollow noreferrer">https://github.com/cloudformdesign/SearchTheSky</a></p>