Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. deleteChar() not deleting characters
Forum Updated to NodeBB v4.3 + New Features

deleteChar() not deleting characters

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 596 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.
  • F Offline
    F Offline
    Fuchsiaff
    wrote on last edited by
    #1

    So I am implementing my own terminal emulator in Python using PyQt5. I've come to the point where I'd like to add the press up arrow key to go through commands used last. This is the code I have right now:

     if e.key() == 16777235:
                    try:
    
                        for i in range(len(self.commands[self.tracker])):
                            cursor.deleteChar()
    
                        self.insertPlainText(self.commands[self.tracker])
                        self.tracker += 1
    
                    except IndexError:
                        self.tracker = 0
    
                    return
    

    Every time an user enters a command it gets appended to the commands list and when the up arrow is pressed (key code is 16777235) then I'm inserting the word to the plain text edit. Now when the user clicks arrow twice then the word isn't being deleted that was inserted before. I tried the same code with deletePreviousChar and that worked but it didn't work perfectly - it kept not deleting one character and that was messing up my cursor position in the QPlainTextEdit.

    jsulmJ 1 Reply Last reply
    0
    • F Fuchsiaff

      So I am implementing my own terminal emulator in Python using PyQt5. I've come to the point where I'd like to add the press up arrow key to go through commands used last. This is the code I have right now:

       if e.key() == 16777235:
                      try:
      
                          for i in range(len(self.commands[self.tracker])):
                              cursor.deleteChar()
      
                          self.insertPlainText(self.commands[self.tracker])
                          self.tracker += 1
      
                      except IndexError:
                          self.tracker = 0
      
                      return
      

      Every time an user enters a command it gets appended to the commands list and when the up arrow is pressed (key code is 16777235) then I'm inserting the word to the plain text edit. Now when the user clicks arrow twice then the word isn't being deleted that was inserted before. I tried the same code with deletePreviousChar and that worked but it didn't work perfectly - it kept not deleting one character and that was messing up my cursor position in the QPlainTextEdit.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Fuchsiaff Where is the cursor when you call cursor.deleteChar()?
      Also, you should not use maggic numbers like 16777235 but enum values from http://doc.qt.io/qt-5/qt.html#Key-enum

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • F Offline
        F Offline
        Fuchsiaff
        wrote on last edited by
        #3

        The cursor is right beside the word that was inserted so

        echo hello[cursor is here]
        
        1 Reply Last reply
        0
        • F Offline
          F Offline
          Fuchsiaff
          wrote on last edited by Fuchsiaff
          #4

          I didn't figure out why deleteChar() didn't do its job but I managed to find a workaround:

                      if e.key() == 16777235:
                          try:
                              if self.tracker != 0:
                                  cursor.select(QTextCursor.BlockUnderCursor)
                                  cursor.removeSelectedText()
                                  self.appendPlainText(self.name)
          
                              self.insertPlainText(self.commands[self.tracker])
                              self.tracker += 1
          
                          except IndexError:
                              self.tracker = 0
          
                          return
          

          Basically I am removing the block and adding the necessary stuff back to it along with the last used command.

          Here's a little demo on what I'm working on: https://raw.githubusercontent.com/Fuchsiaff/Content/master/Peek 2019-02-04 21-06.mp4

          1 Reply Last reply
          1

          • Login

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