qt - PyQt4: QTextEdit start in nth line -
i have popup contains qtextedit, has lot of text in it, lot of lines. want scroll line in qtextedit on show(). line want @ top.
code snippet:
editor = qtgui.qtextedit() # fill editor text # set scroll nth line editor.show()
how can achieve that?
update
i've managed show nth line @ bottom:
cursor = qtgui.qtextcursor(editor.document().findblockbylinenumber(n)) editor.movecursor(qtgui.qtextcursor.end) editor.settextcursor(cursor)
for example n=25 get:
_______________________ . . . . 25th line _______________________
but need @ top...
you have it. trick move current cursor bottom first, , reset cursor target line. view automatically scroll make cursor visible:
editor.movecursor(qtgui.qtextcursor.end) cursor = qtgui.qtextcursor(editor.document().findblockbylinenumber(n)) editor.settextcursor(cursor)
by extension, position cursor @ bottom, move current cursor start first:
editor.movecursor(qtgui.qtextcursor.start) ...
here's demo script:
from pyqt4 import qtcore, qtgui class window(qtgui.qwidget): def __init__(self): super(window, self).__init__() self.edit = qtgui.qtextedit(self) self.edit.setplaintext( '\n'.join('%04d - blah blah blah' % in range(200))) self.button = qtgui.qpushbutton('go line', self) self.button.clicked.connect(self.handlebutton) self.spin = qtgui.qspinbox(self) self.spin.setrange(0, 199) self.spin.setvalue(50) self.check = qtgui.qcheckbox('scroll top') self.check.setchecked(true) layout = qtgui.qgridlayout(self) layout.addwidget(self.edit, 0, 0, 1, 3) layout.addwidget(self.button, 1, 0) layout.addwidget(self.spin, 1, 1) layout.addwidget(self.check, 1, 2) qtcore.qtimer.singleshot(0, lambda: self.scrolltoline(50)) def scrolltoline(self, line=0): if self.check.ischecked(): self.edit.movecursor(qtgui.qtextcursor.end) else: self.edit.movecursor(qtgui.qtextcursor.start) cursor = qtgui.qtextcursor( self.edit.document().findblockbylinenumber(line)) self.edit.settextcursor(cursor) def handlebutton(self): self.scrolltoline(self.spin.value()) self.edit.setfocus() if __name__ == '__main__': import sys app = qtgui.qapplication(sys.argv) window = window() window.setgeometry(500, 100, 400, 300) window.show() sys.exit(app.exec_())
Comments
Post a Comment