簡體   English   中英

pyqt5 轉到行 Qtextedit

[英]pyqt5 goto line Qtextedit

我一直在網上尋找很多如何在 QtextEdit 中制作換行符選項,但是我沒有成功。 我可以在答案Move Cursor Line Position QTextEdit 中看到我在尋找什么

但是當我想做同樣的事情時,我沒有得到相同的結果,也找不到解釋,這是我的代碼

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

問題在於,如果行號小於文本中的行數,則findBlockByLineNumber()返回一個有效的QTextBlock ,並且一開始 QTextEdit 為空,因此它將失敗。 一種可能的解決方案是添加結束行“\\n”,直到獲得行數。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM