pyqt5 goto line Qtextedit

我一直在网络上寻找很多如何在QtextEdit中制作换行选项,但是我没有成功。我可以看到我在答案中寻找的内容 移动光标线位置 QText编辑


但是当我想做同样的事情时,我没有得到相同的结果,我找不到解释,这是我的代码


import sys 

from PyQt5.QtWidgets import QMainWindow, QApplication,QLineEdit,QPushButton,QTextEdit

from PyQt5.QtGui import QTextCharFormat, QBrush, QColor, QTextCursor

from PyQt5.QtCore import QRegExp

class VentanaFindText(QMainWindow):

    def __init__(self):

        super(VentanaFindText, self).__init__()

        self.setWindowTitle("find text - QTextEdit")

        self.resize(475,253)

        self.line_buscar = QLineEdit(self)

        self.line_buscar.setGeometry(20,20,365,23)

        self.btn_buscar = QPushButton("buscar",self)

        self.btn_buscar.setGeometry(388,20,75,25)

        self.text_edit = QTextEdit(self)

        self.text_edit.setGeometry(20, 50, 441, 191)


        self.btn_buscar.clicked.connect(self.gotoLine)


    def gotoLine(self):     

        print("go to line")

        n = int(self.line_buscar.text())

        cursor = QTextCursor(self.text_edit.document().findBlockByLineNumber(n))

        self.text_edit.setTextCursor(cursor)    



if __name__ == '__main__':

    app = QApplication(sys.argv)

    ventana = VentanaFindText()

    ventana.show()

    sys.exit(app.exec_())


慕勒3428872
浏览 137回答 1
1回答

SMILET

问题是,如果行号小于文本中的行数,则返回一个有效的行号,并且在开始时 QTextEdit 为空,因此它将失败。一种可能的解决方案是添加结束行“\n”,直到获得行数。findBlockByLineNumber()QTextBlockimport sysfrom PyQt5.QtWidgets import (&nbsp; &nbsp; QMainWindow,&nbsp; &nbsp; QApplication,&nbsp; &nbsp; QLineEdit,&nbsp; &nbsp; QPushButton,&nbsp; &nbsp; QTextEdit,&nbsp; &nbsp; QGridLayout,&nbsp; &nbsp; QWidget,)from PyQt5.QtGui import QTextCursorclass VentanaFindText(QMainWindow):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; super(VentanaFindText, self).__init__()&nbsp; &nbsp; &nbsp; &nbsp; self.setWindowTitle("find text - QTextEdit")&nbsp; &nbsp; &nbsp; &nbsp; self.resize(475, 253)&nbsp; &nbsp; &nbsp; &nbsp; self.line_buscar = QLineEdit()&nbsp; &nbsp; &nbsp; &nbsp; self.btn_buscar = QPushButton("buscar",)&nbsp; &nbsp; &nbsp; &nbsp; self.text_edit = QTextEdit()&nbsp; &nbsp; &nbsp; &nbsp; central_widget = QWidget()&nbsp; &nbsp; &nbsp; &nbsp; self.setCentralWidget(central_widget)&nbsp; &nbsp; &nbsp; &nbsp; grid_layout = QGridLayout(central_widget)&nbsp; &nbsp; &nbsp; &nbsp; grid_layout.addWidget(self.line_buscar, 0, 0)&nbsp; &nbsp; &nbsp; &nbsp; grid_layout.addWidget(self.btn_buscar, 0, 1)&nbsp; &nbsp; &nbsp; &nbsp; grid_layout.addWidget(self.text_edit, 1, 0, 1, 2)&nbsp; &nbsp; &nbsp; &nbsp; self.btn_buscar.clicked.connect(self.gotoLine)&nbsp; &nbsp; def gotoLine(self):&nbsp; &nbsp; &nbsp; &nbsp; text = self.line_buscar.text()&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n = int(text)&nbsp; &nbsp; &nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Cannot convert '{}' to integer number".format(text))&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if n < 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("The number must be greater than 1")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc = self.text_edit.document()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.text_edit.setFocus()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if n > doc.blockCount():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.text_edit.insertPlainText("\n" * (n - doc.blockCount()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor = QTextCursor(doc.findBlockByLineNumber(n - 1))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.text_edit.setTextCursor(cursor)if __name__ == "__main__":&nbsp; &nbsp; app = QApplication(sys.argv)&nbsp; &nbsp; ventana = VentanaFindText()&nbsp; &nbsp; ventana.show()&nbsp; &nbsp; sys.exit(app.exec_())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python