python - Dedenting function in QPlainTextEdit causes segfault if last line is involved -
i'm working on source code editor should have smart indent/dedent behaviour. however, dedenting method seems causing segmentation fault. i'd pleased if work out why.
here's minimal example:
#!/usr/bin/env python import sip sip.setapi('qstring', 2) sip.setapi('qvariant', 2) pyqt4 import qtgui pyqt4.qtcore import qt class editor(qtgui.qplaintextedit): def keypressevent(self, event): key = event.key() if key == qt.key_backtab: cursor = self.textcursor() start, end = cursor.selectionstart(), cursor.selectionend() cursor.begineditblock() b = self.document().findblock(start) while b.isvalid() , b.position() <= end: t = b.text() p1 = b.position() p2 = p1 + min(4, len(t) - len(t.lstrip())) cursor.setposition(p1) cursor.setposition(p2, qtgui.qtextcursor.keepanchor) cursor.removeselectedtext() b = b.next() cursor.endeditblock() else: super(editor, self).keypressevent(event) class window(qtgui.qmainwindow): """ new gui editing ``.mmt`` files. """ def __init__(self, filename=none): super(window, self).__init__() self.e = editor() self.e.setplaintext('line 1\n line 2\n line 3') self.setcentralwidget(self.e) self.e.setfocus() if __name__ == '__main__': = qtgui.qapplication([]) w = window() w.show() a.exec_()
to recreate, make selection starting on second line , ending on third line, press shift+tab
dedent , end
trigger segfault.
platform:
- running on fedora (linux)
- python 2.7.8 (default, nov 10 2014, 08:19:18) [gcc 4.9.2 20141101 (red hat 4.9.2-1)]
- pyqt 4.8.6 (but happens in pyside too)
update:
- it seems bug occurs if
cursor.begineditblock()
,cursor.endeditblock()
used, see also: qtextcursor , begineditblock
thanks
it seems bug in qt:
https://bugreports.qt.io/browse/qtbug-30051
apparently editing multiple blocks within qtextcursor.begineditblock() causes last block's layout break, in case causing segfault.
a work-around may rewrite dedenting code single operation (determine text after dedenting, delete selected lines, replace new text)
if knows better workaround, please let me know!
Comments
Post a Comment