Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

deleteChar() not deleting characters



  • 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.


  • Qt Champions 2019

    @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



  • The cursor is right beside the word that was inserted so

    echo hello[cursor is here]
    


  • 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


Log in to reply