[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: Re: Obtain cursor location information in QPlainTextEdit widget
From: John F Sturtz <john () sturtz ! org>
Date: 2021-03-23 17:23:01
Message-ID: a4f9e227-6c32-dca5-9bba-c1974aebde72 () sturtz ! org
[Download RAW message or body]
Hi Maurizio. Thanks again (you were one of the ones who frequently
helped me two years ago).
I hit on a solution that is essentially that. My own statement:
Is it possible to save the widget state information just after every
keyboard and mouse event, but before the textChangedslot gets called?
at the end of my post got me thinking that way. As far as I can see,
the only way the cursor can move (if not programmatically, by the code
itself) is either by user keyboard input or mouse input. So putting
code in to snag the cursor position on each keystroke and mouse button
release seems to do it. This picks up the cursor position before it moves.
It seemed like a bit of a sledgehammer solution. But it seems to work.
Thanks!
/John
> John F Sturtz <mailto:john@sturtz.org>
> Sunday, March 21, 2021 10:41 PM
> 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.
>
> I have a QPlainTextEdit widget. I've connected the textChanged 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 and cursor position
> -- before the change occurred.
>
> First pass idea is to save the state of the widget text and cursor
> position at the end of the textChanged 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.
>
> But of course, you can move the cursor without changing text as well.
> So then I can also connect the cursorPositionChanged signal to a slot,
> keep track of when the cursor moves, and maintain the previous cursor
> location that way as well.
>
> The problem is that when text changes, the cursor may also move. (Not
> necessarily, but probably). In which case both slots get called, and
> by the time the second (textChanged) executes, the information has
> already been overwritten.
>
> Sample code is attached. For example, try this:
>
> * Start up the app. Widget gets created, pre-populated with text 'abc'.
> * Move the cursor two positions to the right (between the 'b' and
> 'c'). The cursorPositionChanged 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.
> * Now insert a character (either type it or paste it; the result is
> the same either way).
>
> The cursorPositionChanged 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 textChanged slot gets called, it no longer has any
> way to know that the cursor position was 2 prior to the change.
>
> I guess what I need is for the cursorPositionChanged slot to somehow
> know the difference between when the cursor moved because of changing
> text and when it just moved.
>
> 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
> 'textIsAboutToChange' signal ...). Is it possible to save the widget
> state information just after every keyboard and mouse event, but
> before the textChanged slot gets called?
>
> Any thoughts appreciated.
>
> Thanks!
>
> /John
[Attachment #3 (text/html)]
<html theme="themeKey1rand100" iconset="color"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body text="#000000">Hi Maurizio. Thanks again (you were one of
the ones who frequently helped me two years ago).<br>
<br>
I hit on a solution that is essentially that. My own statement:<br>
<br>
<span style="color: rgb(0, 106, 169);"><span style="font-style: italic;">Is
it possible to save
the widget state information just after every keyboard and mouse event,
but before the </span></span><span style="color: rgb(0, 106, 169);"><span
style="font-style: italic;"><span style="font-family: monospace;">textChanged</span></span></span><span
style="color: rgb(0, 106, 169);"><span style="font-style: italic;">
slot gets called?</span></span><br>
<br>
at the end of my post got me thinking that way. As far as I can see,
the only way the cursor can move (if not programmatically, by the code
itself) is either by user keyboard input or mouse input. So putting
code in to snag the cursor position on each keystroke and mouse button
release seems to do it. This picks up the cursor position before it
moves.<br>
<br>
It seemed like a bit of a sledgehammer solution. But it seems to work.<br>
<br>
Thanks!<br>
<br>
/John<br>
<span>
</span><br>
<blockquote type="cite"
cite="mid:3791d3fb-1b3f-2154-516f-0effa197108c@sturtz.org"
style="border: 0px none ! important;">
<div xmlns="http://www.w3.org/1999/xhtml" class="__pbConvHr"
style="margin:30px 25px 10px 25px;"><div
style="width:100%;border-top:2px solid
rgba(146,154,163,0.7);padding-top:10px;"> <div
style="display:inline-block;white-space:nowrap;vertical-align:middle;width:49%;">
<a style="color:#485664
!important;padding-right:6px;font-weight:500;text-decoration:none
!important;" href="mailto:john@sturtz.org" moz-do-not-send="true">John F
Sturtz</a></div> <div
style="display:inline-block;white-space:nowrap;vertical-align:middle;width:48%;text-align:
right;"> <font color="#909AA4"><span style="padding-left:6px">Sunday,
March 21, 2021 10:41 PM</span></font></div> </div></div>
<div xmlns="http://www.w3.org/1999/xhtml" class="__pbConvBody"
__pbrmquotes="true"
style="color:#909AA4;margin-left:24px;margin-right:24px;">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
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>
</div>
</blockquote>
<br>
</body></html>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic