首页 > 解决方案 > PyQt,文本光标

问题描述

我一直在用 QTextEdit 对象构建一个 GUI,我想添加一个 QPushButton 将光标向下移动到新行。

编辑:有人建议这个问题可能会回答我的问题:Moving the cursor in a PyQt5 text edit doesn't work

但这不是因为在我的程序崩溃后调试器说“QTextCursor 对象没有属性'Left'。” 在该答案下的评论中,它说尝试“setTextCursor”,但又一次......同样的问题。

编辑:更新示例

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()

标签: pythonpyqt5

解决方案


推荐阅读