Results 1 to 6 of 6

Thread: QPlainTextEdit and input filtering

  1. #1
    Join Date
    Oct 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QPlainTextEdit and input filtering

    Hi there,

    I want to make a QPlainTextEdit component restricting the input to only the characters '0' and '1'. Besides that, all the "control keys" for cut/copy/paste, tab for focus change etc. shall still work. So far, I haven't been successful and thus, asking for your help.

    My expectation was, that QPlainTextEdit has one method where all the text going inside is handled. As far as I can tell, that is not the case.

    Here is what I have done so far:

    ----
    Idea #1: Use some signal/slot to intercept text which shall not go inside the edit. The obvious is the textChanged() signal but this is called if the text is already placed in the editor (too late).
    Is there a signal which is called before the text is placed in the edit?

    -----
    Idea #2: Install event filter and discard all input except '0' and '1'
    Qt Code:
    1. MyQPlainTextEdit edit;
    2. ..
    3. MyQPlainTextEdit::MyQPlainTextEdit(QWidget *parent) : QPlainTextEdit(parent) {
    4. ...
    5. installEventFilter(this);
    6. }
    7.  
    8. bool MyQPlainTextEdit::eventFilter(QObject* obj, QEvent* event) {
    9. if (event->type() == QEvent::KeyPress) {
    10. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
    11.  
    12. if ((keyEvent->text() != "") && (keyEvent->text() != "0") && (keyEvent->text() != "1")) {
    13. return true;
    14. }
    15. }
    16.  
    17. // standard event processing
    18. return QObject::eventFilter(obj, event);
    19. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that I cannot reliable distinguish between actual characters shown in the widget and keystrokes potentially doing some control things. For example: I can enter a '0' or '1' (shows up in the text) or I can enter a Ctrl-'0' or Ctrl-'1' (works also with AltGr and so on).
    I feel that intercepting plain key event is a bit too low level for my purpose...
    ---

    Idea #3
    Using a custom QTextCursor and overriding insertText(). As it turns out, these methods are only called if I insert text into the editor programmatically. User input is not seen by these functions.

    -----

    What am I doing wrong? What do you suggest? Thanks for your help and any hits you can give me!

    Regards,
    Paul

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPlainTextEdit and input filtering

    Intercept key press event and discard all input besides "0" and "1" if and only if there are no keyboard modifiers active (or if just Shift modifier is active).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPlainTextEdit and input filtering

    Hi wysota!

    Thanks for your reply. Unfortunately, I am unable to turn your suggestion into working code. I tried:

    Qt Code:
    1. bool MyQPlaintTextEditor::eventFilter(QObject* obj, QEvent* event) {
    2. if (event->type() == QEvent::KeyPress) {
    3. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
    4.  
    5. if ((keyEvent->modifiers() == 0) && (keyEvent->text() != "0") && (keyEvent->text() != "1")) {
    6. return true;
    7. }
    8. }
    9.  
    10. // standard event processing
    11. return QObject::eventFilter(obj, event);
    12. }
    To copy to clipboard, switch view to plain text mode 

    I still can e.g. press AltGr-0 and get a "}" -> AltGr doesn't seem to be catched by modifiers(). Besides that, the code above doesn't allow me to navigate with cursor keys or use backspace. Do I really have to check all those keycodes and let them though? This seems odd to me. But as I said already: I think that playing with the key codes it too low level for my purpose...

    Any other ideas? Thanks a lot!

    Regards,
    Paul

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPlainTextEdit and input filtering

    Quote Originally Posted by paul.schwann View Post
    I still can e.g. press AltGr-0 and get a "}" -> AltGr doesn't seem to be catched by modifiers()
    It should be.

    Do I really have to check all those keycodes and let them though? This seems odd to me.
    What would not seem odd to you?
    I think that playing with the key codes it too low level for my purpose...
    What is the purpose? What if I paste some text which is not composed from 0 and 1? Is it valid or not? If not, then you have to check the contents of the text edit each time it changes using the QTextDocument::contentsChange() signal and simply correct the changes you don't want. Note that you may wreck undo/redo with this though.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPlainTextEdit and input filtering

    I don't like using eventFilter unless I can help it. I prefer to subclass the widget and override QPlainTextEdit::keyPressEvent, and then use QApplication::keyboardModifiers to detect which control keys are pressed.

  6. #6
    Join Date
    Oct 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPlainTextEdit and input filtering

    Quote Originally Posted by wysota View Post
    It should be.


    What would not seem odd to you?

    What is the purpose? What if I paste some text which is not composed from 0 and 1? Is it valid or not? If not, then you have to check the contents of the text edit each time it changes using the QTextDocument::contentsChange() signal and simply correct the changes you don't want. Note that you may wreck undo/redo with this though.
    Hi Wysota,

    I am a bit lost here...

    It seems odd to me that I have to fiddle with tons of conditions although all I want is to avoid some text appear in the editor field. Why do I have to take care about all the control keys (which I don't even know)?

    Regarding the copy/paste problem: I expect to be able to hook into the copy/paste functions of the QPlainTextEdit and either filter all non-0/1 characters out of the text being pasted or at least reject the pase action if other than 0/1 is in the text.

    Let me ask another question: What function is called (and thus, I can override) if the QPlainTextEdit is supposed to insert/append additional text (or set new text)? Is there a single function responsible for this?
    This function might also be part of the QTextDocument instead of the QPlainTextEdit.
    -- I wasn't able to locate such a function (it may exist or not).

    Thanks & Regards,
    Paul

Similar Threads

  1. Maximum input length for QTextEdit or QPlainTextEdit ??
    By b_ginner in forum Qt Programming
    Replies: 2
    Last Post: 22nd August 2009, 20:57
  2. Replies: 1
    Last Post: 14th September 2008, 23:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.