Dedenting function in QPlainTextEdit causes segfault if last line is involved

a 夏天 提交于 2019-12-13 02:08:11

问题


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() and cursor.endEditBlock() are used, see also: QTextCursor and beginEditBlock

Thanks


回答1:


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!



来源:https://stackoverflow.com/questions/29320202/dedenting-function-in-qplaintextedit-causes-segfault-if-last-line-is-involved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!