为了账号安全,请及时绑定邮箱和手机立即绑定

pyqt5 goto line Qtextedit

pyqt5 goto line Qtextedit

慕勒3428872 2022-08-11 16:53:24
我一直在网络上寻找很多如何在QtextEdit中制作换行选项,但是我没有成功。我可以看到我在答案中寻找的内容 移动光标线位置 QText编辑但是当我想做同样的事情时,我没有得到相同的结果,我找不到解释,这是我的代码import sys from PyQt5.QtWidgets import QMainWindow, QApplication,QLineEdit,QPushButton,QTextEditfrom PyQt5.QtGui import QTextCharFormat, QBrush, QColor, QTextCursorfrom PyQt5.QtCore import QRegExpclass 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_())
查看完整描述

1 回答

?
SMILET

TA贡献1796条经验 获得超4个赞

问题是,如果行号小于文本中的行数,则返回一个有效的行号,并且在开始时 QTextEdit 为空,因此它将失败。一种可能的解决方案是添加结束行“\n”,直到获得行数。findBlockByLineNumber()QTextBlock


import sys

from PyQt5.QtWidgets import (

    QMainWindow,

    QApplication,

    QLineEdit,

    QPushButton,

    QTextEdit,

    QGridLayout,

    QWidget,

)

from PyQt5.QtGui import QTextCursor



class VentanaFindText(QMainWindow):

    def __init__(self):

        super(VentanaFindText, self).__init__()

        self.setWindowTitle("find text - QTextEdit")

        self.resize(475, 253)

        self.line_buscar = QLineEdit()

        self.btn_buscar = QPushButton("buscar",)

        self.text_edit = QTextEdit()


        central_widget = QWidget()

        self.setCentralWidget(central_widget)

        grid_layout = QGridLayout(central_widget)

        grid_layout.addWidget(self.line_buscar, 0, 0)

        grid_layout.addWidget(self.btn_buscar, 0, 1)

        grid_layout.addWidget(self.text_edit, 1, 0, 1, 2)


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


    def gotoLine(self):

        text = self.line_buscar.text()

        try:

            n = int(text)

        except ValueError:

            print("Cannot convert '{}' to integer number".format(text))

        else:

            if n < 1:

                print("The number must be greater than 1")

                return

            doc = self.text_edit.document()

            self.text_edit.setFocus()

            if n > doc.blockCount():

                self.text_edit.insertPlainText("\n" * (n - doc.blockCount()))

            cursor = QTextCursor(doc.findBlockByLineNumber(n - 1))

            self.text_edit.setTextCursor(cursor)



if __name__ == "__main__":

    app = QApplication(sys.argv)

    ventana = VentanaFindText()

    ventana.show()

    sys.exit(app.exec_())


查看完整回答
反对 回复 2022-08-11
  • 1 回答
  • 0 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信