PDA

View Full Version : Selecting Text QPlainTextEdit



smhall316
7th December 2010, 21:09
I am trying to select text programmatically using the QTextCursor in QPlainTextEdit. I am trying to create my selection by using the mouse[Press | Move | Release]Event.


def mousePressEvent(self, event):
pos = event.pos()
cursor = self.cursorForPosition(pos)
self.start_pos = cursor.position()
cursor.setPosition(self.start_pos,QTextCursor.Move Anchor)


def mouseMoveEvent(self, event):
pos = event.pos()
cursor = self.cursorForPosition(pos)
cursor.setPosition(cursor.position(),QTextCursor.K eepAnchor)
print unicode(cursor.selectedText())

When I do this and try to select, it always prints an empty string.

I know by default that selections are already handled but I am trying to understand how to selections are done so that I can do some custom tasks. I have managed to get selections to work by using setPosition() followed by movePosition() but it would not highlight the selection. I determined that the selection was working by printing out the selected text.

My questions are, why doesn't my selection code for the mouse events work and second, when I do create a selection why doesn't it highlight the selection? Any help would be greatly appreciated.

Thanks,
Scott