[PyQt] Peculiar error/warning message trying to set contents of

John F Sturtz john at sturtz.org
Mon Apr 29 04:27:03 BST 2019


[Windows 10/Python 3.7.1/PyQt 5.11.3]

I am experiencing strange behavior with QPlainTextEdit, and I'm wondering if anyone knows the cause of or a workaround for this.

I have a custom class that derives from QTableView, and an associated QAbstractTableModel.  I have a Delegate set up, which uses a editor widget that derives from QPlainTextEdit.

Under certain circumstances when a cell in the View is edited, after the editor widget is created I want to set the initial contents.  From one of the View methods, I have this:

self.edit(self.currentIndex())

That initiates editing in the current cell, and the Delegate creates an editor widget.  The Delegate saves a reference to the editor widget, which the View picks up as follows:

editor = self.itemDelegate().editor

Then I purport to set the initial value displayed in the (QPlainTextEdit-based) editor:

editor.setPlainText(<intended_value>)

It works, in that the value does get placed into the editor widget inside the cell.  But under some circumstances (depending, seemingly, on what was present in the cell before editing took place), I get a message to the console like this:

QTextCursor::setPosition: Position '5' out of range

The number referred to varies, and seems not to have any obvious correlation to anything, neither the length of the string that was in the cell previously, nor the length of the string I put there with setPlainText().

It seems most of the time that the program continues running just fine, so mostly it doesn't seem to hurt anything.  But very occasionally the program crashes after this happens.

I found one post on StackOverflow (though it's Qt, not PyQt) referencing a similar problem: https://stackoverflow.com/questions/26578319/error-when-im-trying-to-set-text-in-qtextedit

None of the suggested workarounds seemed to work for me (nor the original poster).  The posted answers suggest it is a bug in Qt.

Sorry to post just a few lines, and not actual complete code.  My app is pretty large.  I tried to mock up a smaller set of sample code to demonstrate the problem -- but (annoyingly) the smaller code doesn't generate the message (so perhaps my larger app is at fault ...)

Does anyone have any insight into this behavior?

Thanks!

/John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190428/c1133dc0/attachment.html>


More information about the PyQt mailing list