<html theme="themeKey1rand100" iconset="color"><head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head><body style="color: rgb(0, 0, 0);" text="#000000">
Hello PyQt forum. I posted several questions here a couple years ago,
and several people were wonderfully helpful. I've been away awhile, but
am fiddling with another PyQt project, and I seem to be stumped by
this. I feel like I must be stupidly overlooking something.<br>
<br>
I have a <span style="font-family: monospace;">QPlainTextEdit</span>
widget. I've connected the <span style="font-family: monospace;">textChanged</span>
signal to a slot. Now I simply want this: For the slot to be able to
know what the widget looked like -- both text contents <span
style="font-style: italic;">and</span> cursor position -- before the
change occurred.<br>
<br>
First pass idea is to save the state of the widget text and cursor
position at the end of the <span style="font-family: monospace;">textChanged</span>
slot, so the next time around, I
have a record of what they previously were. That works as long as the
cursor changes position only because of changing text.<br>
<br>
But of course, you
can move the cursor without changing text as well. So then I can also
connect the <span style="font-family: monospace;">cursorPositionChanged</span>
signal to a slot,
keep track of when the cursor moves, and maintain the previous cursor
location that way as well.<br>
<br>
The problem is that when text changes, the cursor may also move. (Not
necessarily, but probably). In which case both slots get called, and <span
style="font-family: monospace;"><span style="font-family: sans-serif;">by
the time the second (</span></span><span style="font-family:
monospace;">textChanged</span><span style="font-family: monospace;"><span
style="font-family: sans-serif;">) executes, the information has
already been overwritten.</span></span><span style="font-family:
monospace;"></span><span style="font-family: monospace;"></span><br>
<br>
Sample code is attached. For example, try this:<br>
<ul>
<li>Start up the app. Widget gets created, pre-populated with text
<span style="font-family: monospace;">'abc'</span>.</li>
<li>Move the cursor two positions to the right (between the <span
style="font-family: monospace;">'b'</span> and
<span style="font-family: monospace;">'c'</span>). The <span
style="font-family: monospace;">cursorPositionChanged</span> slot gets
called for both moves, so
that at the end, it is aware that the previous cursor position was 1,
and the current position is 2.</li>
<li>Now insert a character (either type it or paste it; the result
is the same either way).</li>
</ul>
<p>The <span style="font-family: monospace;">cursorPositionChanged</span>
slot gets called again, and is briefly aware that the previous cursor
location was 2 and it is now 3. But then it updates the previous
position to 3 before exiting (as it must, to be useful). When the <span
style="font-family: monospace;">text</span><span style="font-family:
monospace;">Changed</span> slot gets called, it no longer has any way to
know that the cursor position was 2 prior to the change.<br>
</p>
I guess what I need is for the <span style="font-family: monospace;">cursorPositionChanged</span>
slot to somehow know the difference between when the cursor moved
because of changing text and when it just moved.<br>
<br>
After much head-scratching, I haven't come up with anything. But surely
there must be a way around this? (I wish there were a '<span
style="font-family: monospace;">textIsAb</span><span style="font-family:
monospace;">outToChange</span>' signal ...). Is it possible to save
the widget state information just after every keyboard and mouse event,
but before the <span style="font-family: monospace;">textChanged</span>
slot gets called?<br>
<br>
Any thoughts appreciated.<br>
<br>
Thanks!<br>
<br>
/John<br>
</body></html>