Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. QGraphicsTextItem cursor issue when setting new text

QGraphicsTextItem cursor issue when setting new text

Scheduled Pinned Locked Moved Unsolved Language Bindings
python
3 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChainReaction
    wrote on last edited by A Former User
    #1

    Hey.
    I have subclassed QGraphicsTextItem so that I can perform some validation on the input, and I'm having some issues with the cursor.
    Here's a code snippet of how my class looks like:

    class TextItem(QGraphicsTextItem):
    
    	def __init__(self, parent = None):
    		super(TextItem, self).__init__(parent)
    		self.setTextInteractionFlags(Qt.TextEditorInteraction)
    
    	def keyPressEvent(self, event):
    		if event.key() == Qt.Key_Return or event.key() == Qt.Key_Escape:
    			self.clearFocus()
    			return
    
    		cursor = self.textCursor() # this, according to the doc, returns a *copy* of the cursor
    
    		# TODO: perform some validation on event.key()
    		cursor.movePosition(QTextCursor.NextCharacter)
    		self.setHtml('validated text') # this changes the cursor, setting it to the end of the line!
    		self.setTextCursor(cursor) # this has no effect
    

    As you can see from the comments, for some reason the self.setHtml() invocation changes the cursor, which is fine, as long as I can set it back, but that's the problem - self.setTextCursor(cursor) has no effect!
    Can someone please shed some light on what is going on here?

    Thanks.

    [Edit: moved to Language Bindings ~~ @Wieland]

    m.sueM 1 Reply Last reply
    0
    • C ChainReaction

      Hey.
      I have subclassed QGraphicsTextItem so that I can perform some validation on the input, and I'm having some issues with the cursor.
      Here's a code snippet of how my class looks like:

      class TextItem(QGraphicsTextItem):
      
      	def __init__(self, parent = None):
      		super(TextItem, self).__init__(parent)
      		self.setTextInteractionFlags(Qt.TextEditorInteraction)
      
      	def keyPressEvent(self, event):
      		if event.key() == Qt.Key_Return or event.key() == Qt.Key_Escape:
      			self.clearFocus()
      			return
      
      		cursor = self.textCursor() # this, according to the doc, returns a *copy* of the cursor
      
      		# TODO: perform some validation on event.key()
      		cursor.movePosition(QTextCursor.NextCharacter)
      		self.setHtml('validated text') # this changes the cursor, setting it to the end of the line!
      		self.setTextCursor(cursor) # this has no effect
      

      As you can see from the comments, for some reason the self.setHtml() invocation changes the cursor, which is fine, as long as I can set it back, but that's the problem - self.setTextCursor(cursor) has no effect!
      Can someone please shed some light on what is going on here?

      Thanks.

      [Edit: moved to Language Bindings ~~ @Wieland]

      m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      Hi @ChainReaction

      When the cursor is at the end of the line and you move it to the "next character", it stays put at the end. I would try QTextCursor::StartOfLine.

      -Michael.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChainReaction
        wrote on last edited by
        #3

        Hey @m-sue , thanks for your reply.

        1. The problem isn't that cursor.movePosition(QTextCursor.NextCharacter) does not move the cursor, but rather that self.setHtml() sends the cursor to the end regardless of its current position.
        2. This does not explain why self.setTextCursor(cursor) has no effect.
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved