0

I've been building a GUI with a QTextEdit object and I want to add a QPushButton that moves the cursor down to a new line.

Edit: Someone suggested this question might answer mine: Moving the cursor in a PyQt5 text edit doesn't work

But it doesn't because after crashing my program the debugger says "QTextCursor object has no attribute 'Left'." In the comments under that answer it says to try "setTextCursor" but again... same issue.

Edit: Updated example

class Window(QWidget): def __init__(self): super().__init__() self.layout = QVBoxLayout() self.w = QTextEdit() self.cursor = self.w.textCursor() self.button = QPushButton('Button') self.button.clicked.connect(self.nextBlock) # Add to layout self.layout.addWidget(self.button) self.layout.addWidget(self.w) self.setLayout(self.layout) def nextBlock(self): self.w.moveCursor(QTextCursor.NextBlock) QTimer.singleShot(0, self.w.setFocus) def main(): app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) if __name__ == '__main__': main() 
9
  • No. It gives no indication how to move to next block. self.cursor.movePosition(self.cursor.NextBLock)? Doesn't work. Commented Feb 16, 2021 at 23:18
  • use from PyQt5.QtGui import QTextCursor self.cursor.moveCursor(QTextCursor.NextBlock). If it doesn't work then you probably have another error so you need to provide an MRE Commented Feb 16, 2021 at 23:30
  • @eyllanesc thanks for the suggestion. I tried this and still didn't work. Updated my question with code example. Commented Feb 17, 2021 at 0:04
  • TYPO: change self.button.clicked.connect(self.moveCursor()) tp self.button.clicked.connect(self.moveCursor) Commented Feb 17, 2021 at 0:05
  • 1
    I have tested and it works correctly, on the other hand it shows the imports in addition to pointing out the new error message, and do not forget to save the changes :-) Commented Feb 17, 2021 at 0:25

0