<div id="__MailbirdStyleContent" style="font-size: 12pt;font-family: Candara;color: #000000"><span style="color: #366092">[Windows 10/Python 3.7.1/PyQt 5.11.3]</span><div class="mb_sig"></div><div><span style="color: #366092"><br></span></div><div><span style="color: #366092">I am experiencing strange behavior with <font face="Courier New">QPlainTextEdit</font>, and I'm wondering if anyone knows the cause of or a workaround for this.</span></div><div><span style="color: #366092"><br></span></div><div><span style="color: #366092">I have a custom class that derives from <font face="Courier New">QTableView</font>, and an associated <font face="Courier New">QAbstractTableModel</font>.  I have a Delegate set up, which uses a editor widget that derives from <font face="Courier New">QPlainTextEdit</font>.</span></div><div><span style="color: #366092"><br></span></div><div><span style="color: #366092">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:</span></div><div><span style="color: #366092"><br></span></div><div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><span style="font-size: 12pt;line-height: 1.5"><span style="color: #366092;font-family: Courier New">self.edit(self.currentIndex())</span></span></div></blockquote></div><div><span style="font-size: 12pt;font-family: Candara"><span style="color: #366092"><b></b> <br></span></span></div><div><span style="font-size: 12pt;font-family: Candara"><span style="color: #366092">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:</span></span></div><div><span style="font-size: 12pt;font-family: Candara"><span style="color: #366092"><br></span></span></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><span style="font-size: 12pt"><span style="color: #366092;font-family: Courier New">editor = self.itemDelegate().editor</span></span></div></blockquote><div><span style="font-size: 12pt;font-family: Candara"><span style="color: #366092"><br></span></span></div><div><span style="font-size: 12pt"><span style="color: #366092"><b></b>Then I purport to set the initial value displayed in the (<font face="Courier New">QPlainTextEdit</font>-based) editor:</span></span></div><div><span style="font-size: 12pt;font-family: Candara"><span style="color: #366092"><br></span></span></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><span style="font-size: 12pt"><span style="color: #366092;font-family: Courier New">editor.setPlainText(<i><intended_value></i>)</span></span></div></blockquote><div><span style="font-size: 12pt;font-family: Candara"><span style="color: #366092"><br></span></span></div><div><span style="font-size: 12pt;font-family: Candara"><span style="color: #366092"><b></b>It <i>works, </i>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:</span></span></div><div><span style="font-size: 12pt;font-family: Candara"><span style="color: #366092"><br></span></span></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><span style="font-family: Courier New;color: #c00000"><b>QTextCursor::setPosition: Position '5' out of range</b></span></div></blockquote><div><span style="color: #366092"><br></span></div><div><span style="color: #366092">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 <font face="Courier New">setPlainText()</font>.</span></div><div><span style="color: #366092"><br></span></div><div><span style="color: #366092">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.</span></div><div><span style="color: #366092"><br></span></div><div><span style="color: #366092">I found one post on StackOverflow (though it's Qt, not PyQt) referencing a similar problem: <font face="Courier New">https://stackoverflow.com/questions/26578319/error-when-im-trying-to-set-text-in-qtextedit</font></span></div><div><span style="color: #366092"><br></span></div><div><span style="color: #366092">None of the suggested workarounds seemed to work for me (nor the original poster).  The posted answers suggest it is a bug in Qt.</span></div><div><br></div><div><span style="color: rgb(54, 96, 146);">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 <i>is</i> at fault ...)</span></div><div><br></div><div><span style="color: #366092">Does anyone have any insight into this behavior?</span></div><div><span style="color: #366092"><br></span></div><div><span style="color: #366092">Thanks!</span></div><div><span style="color: #366092"><br></span></div><div><span style="color: #366092">/John</span><br></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><span style="font-size: 12pt;font-family: Candara;color: #000000"></span></div></blockquote><div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div></div></blockquote></div></div>