Results 1 to 5 of 5

Thread: How can I replace every keypress character with my custom key in lineedit?

  1. #1
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default How can I replace every keypress character with my custom key in lineedit?

    Hi,

    I've my custom key maps table (A=c, B=f, C=d, ..... etc.)

    I type and show the texts in lineedit normally,
    but after checked a checkbox, I want to replace every letter immediately with my custom map letter.

    eg. in unchecked mode I type ABCDE then I checked and keep type ABCDE, the letters in lineedit is as follow:
    ABCDcfdj
    I want to see and replace letter in lineedit's every key pressed.

    In VB, this work is very easy. In Qt C++, how can I do it?

    my sample vb code are:

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If ckMyMap.Value = 1 Then KeyAscii = KeyMap(KeyAscii)
    End Sub

    Public Function KeyMap(key As Integer) As Integer
    'My Custom Key Map
    If key = 65 Then
    KeyMap = 99
    ElseIf key = 66 Then
    KeyMap = 102
    ElseIf key = 67 Then
    KeyMap = 100
    ElseIf key = 68 Then
    KeyMap = 106
    ElseIf key ......
    ...............
    ...............
    Else
    KeyMap = key
    End If
    End Function

    In Qt C++, how can I do it?

    Thanks.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How can I replace every keypress character with my custom key in lineedit?

    Subclass QLineEdit and override eventFilter() something like this:
    Qt Code:
    1. bool TranslatingEdit::eventFilter( QObject * watched, QEvent * event )
    2. {
    3. TranslatingEdit *edit = qobject_cast<TranslatingEdit *>(watched);
    4. if ( edit && event->type() == QEvent::KeyPress ) {
    5. QKeyEvent *k = static_cast<QKeyEvent *>( event );
    6. // your mapping logic here this is an example for a single key
    7. if ( k->key() == Qt::Key_Space) {
    8. QEvent *evtSpace = new QKeyEvent(QEvent::KeyPress, Qt::Key_Underscore, Qt::NoModifier, "_");
    9. qApp->postEvent(edit, evtSpace );
    10. return true; // this stops the original event here
    11. }
    12. }
    13. return QLineEdit::event(event);
    14. }
    To copy to clipboard, switch view to plain text mode 
    Then install this filter on the widget itself in its constructor:
    Qt Code:
    1. installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

    The filter code can be in any QObject , for example a parent form class, if that is more convenient.

    Edit: Actually, in hindsight, this might lead to an infinite loop if you map a key to another key that is also subject to mapping.
    Last edited by ChrisW67; 31st October 2015 at 20:11.

  3. The following user says thank you to ChrisW67 for this useful post:

    binary001 (2nd November 2015)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How can I replace every keypress character with my custom key in lineedit?

    You could try this (untested code):
    Qt Code:
    1. // a new member variable (init to false in constructor)
    2. bool m_translating;
    3.  
    4. // A getter
    5. bool TranslatingEdit::isTranslating() const { return m_translating; }
    6.  
    7. // a setter/slot you can connect from your checkbox clicked(bool) signal
    8. void TranslatingEdit::setTranslating(bool flag) { m_translating = flag; }
    9.  
    10. // and a protected override
    11. void TranslatingEdit::keyPressEvent( QKeyEvent * event )
    12. {
    13. if (m_translating) {
    14. // your mapping logic here this is an example for a single key
    15. if ( event->key() == Qt::Key_Space) {
    16. QKeyEvent newEvent(QEvent::KeyPress, Qt::Key_Underscore, Qt::NoModifier, "_");
    17. QLineEdit::keyPressEvent(&newEvent);
    18. event->setAccepted(newEvent.isAccepted());
    19. }
    20. }
    21. else {
    22. return QLineEdit::keyPressEvent(newEvent);
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to ChrisW67 for this useful post:

    binary001 (2nd November 2015)

  6. #4
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: How can I replace every keypress character with my custom key in lineedit?

    Thanks,

    It's working!

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How can I replace every keypress character with my custom key in lineedit?

    The second option? Actually, I can see that in Line 22 in my second post does not need the "return" (possible compiler warning) and should call the base implementation with "event" not "newEvent" (would not compile).

Similar Threads

  1. Custom QApplication and keypress
    By bibhukalyana in forum Qt Programming
    Replies: 4
    Last Post: 5th July 2014, 15:50
  2. Keypress never fired on lineedit qcombobox
    By stef13013 in forum Qt Programming
    Replies: 0
    Last Post: 6th August 2012, 16:46
  3. Custom LineEdit - need help!
    By ArlexBee-871RBO in forum Qt Programming
    Replies: 8
    Last Post: 15th September 2010, 17:10
  4. Character by Character (Unicode?) File Reading
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2009, 15:28
  5. How to read QStringList character by character
    By iamjayanth in forum Qt Programming
    Replies: 4
    Last Post: 3rd April 2009, 11:25

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
  •  
Qt is a trademark of The Qt Company.