[Qt-interest] Match new line character

Israel Brewster israel at frontierflying.com
Fri Mar 27 00:10:22 CET 2009




On Mar 26, 2009, at 12:48 PM, Kaleb Pederson wrote:

> On Thu, Mar 26, 2009 at 11:57 AM, Israel Brewster
> <israel at frontierflying.com> wrote:
>> I have a QTextEdit widget in my app where I need to watch what the
>> user types, and if they type certain characters I need to respond in
>> various ways. What I have done so far is connected a slot to the
>> textChanged() signal, such that whenever they type a character I use
>> the QTextCursor to select the last character (whatever they just
>> typed, obviously) and compare it to the set of "action" characters.
>> This works fine for normal characters (if there is a better way, I'd
>> love to hear it) but it doesn't appear to work if they type a new  
>> line
>> (return or enter, I don't care which). I tried comparing to "\n", but
>> the comparison always fails. In the debugger, the selected string
>> looks like "\e2\80\a9" in this case. I was thinking maybe that was
>> just random junk, except that I always get that same string. How  
>> can I
>> match on a new line? Thanks.
>
> QTextDocument, which is accessible through the QTextEdit, has a
> contentsChange signal that is emitted and provides sufficient
> information to perform edits, even of pasted content. It's early
> enough in the chain that the change will not have been drawn, making
> the removal of characters appear as if it was never accepted.
>
> You can use the arguments of the contentsChange signal to figure out
> what characters, including newlines, may have been typed.  I've done
> almost exactly this before.

Thanks for the response. Unfortunately, using that signal leaves me  
with the same problem: I can't seem to figure out how to match a  
return or enter. I can use the position argument to get the character  
using the characterAt function, which gives me a QChar, but that QChar  
doesn't appear to match to anything- same issue I was having with  
using the selection method to figure out the character. Specifically,  
in this case, characterAt(int) gives me a QChar which shows up in the  
debugger with a value of 8233. A Qt::Key_Return has a value of 4,  
Qt::Key_Enter 5, and '\n' 10. Obviously none of these match the 8233   
characterAt gives me. What am I missing here?

As far as the procedure goes, after some poking around, I decided to  
simply overload the keyPressedEvent(). This seems to work nicely for  
acting on the key presses, and the keyEvent does actually match up to  
enter and return. I still need to be able to match them other places,  
however, so the problem remains.
-----------------------------------------------
Israel Brewster
Computer Support Technician II
Frontier Flying Service Inc.
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------


>
>
> HTH,
>
> --Kaleb




More information about the Qt-interest-old mailing list