I'm working on a source code editor that should have smart indent/dedent behaviour. However, my dedenting method seems to be causing a segmentation fault. I'd be very pleased if someone could work out why.
Here's a minimal example:
#!/usr/bin/env python
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4 import QtGui
from 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() and 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 for 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__':
a = QtGui.QApplication([])
w = Window()
w.show()
a.exec_()
To recreate, make a selection starting on the second line and ending on the third line, then press Shift+Tab
to dedent and End
to trigger the 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 it happens in PySide too)
Update:
- It seems this bug only occurs if
cursor.beginEditBlock()
andcursor.endEditBlock()
are used, see also: QTextCursor and beginEditBlock
Thanks
It seems to be a bug in Qt:
https://bugreports.qt.io/browse/QTBUG-30051
Apparently editing multiple blocks from within a QTextCursor.beginEditBlock() causes the last block's layout to break, which in my case was causing a segfault.
A work-around may be to rewrite the dedenting code as a single operation (determine text after dedenting, delete all selected lines, replace with new text)
If someone knows a better workaround, please let me know!