Search code examples
pyqt5notepadqscintilla

How to count lines in qscintilla?


I'm using QScintilla to make my own notepad for fun in pyqt5 python. I was wandering if there is a way to get the number of lines of a QScintilla() widget?


Solution

  • You have to use the lines() method, you can also use the linesChanged signal.

    import sys
    from PyQt5 import QtWidgets, Qsci
    
    
    class Editor(Qsci.QsciScintilla):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.setText("Foo\nBar")
            self.print_lines()
    
            self.linesChanged.connect(self.handle_lines_changed)
    
        def handle_lines_changed(self):
            self.print_lines()
    
        def print_lines(self):
            print("total lines: {}".format(self.lines()))
    
    
    if __name__ == "__main__":
    
        app = QtWidgets.QApplication(sys.argv)
        w = Editor()
        w.resize(640, 480)
        w.show()
        sys.exit(app.exec_())