Results 1 to 7 of 7

Thread: QLineEdit and keyPressEvent(QKeyEvent *)

  1. #1
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default QLineEdit and keyPressEvent(QKeyEvent *)

    I've subclassed QLineEdit in order to obtain field that will be used for acquiring accelerators from user (sth like class derived from QListViewItem in QtDesigner). Unfortunately despite the fact I've reimplemented keyPressEvent(QKeyEvent *) I still cannot catch Ctrl+A and Ctrl+U. I browsed source code of QLineEdit but I still have no idea.

    Qt Code:
    1. #ifndef LINEACCEL_H
    2. #define LINEACCEL_H
    3.  
    4. #include <qkeysequence.h>
    5. #include <qlineedit.h>
    6.  
    7. class LineAccel : public QLineEdit
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. LineAccel(const QKeySequence &, QWidget * parent = 0, const char * name = 0);
    13. ~LineAccel();
    14.  
    15. void setAccel(const QKeySequence &);
    16. QKeySequence accel();
    17.  
    18. protected:
    19. QPopupMenu * createPopupMenu();
    20. void keyPressEvent(QKeyEvent *);
    21.  
    22. private:
    23. int translateModifiers(int);
    24.  
    25. private:
    26. };
    27.  
    28. #endif
    29. #include "lineaccel.h"
    30.  
    31. LineAccel::LineAccel(const QKeySequence & accel, QWidget * parent, const char * name) : QLineEdit(parent, name)
    32. {
    33. setReadOnly(true);
    34.  
    35. setAccel(accel);
    36. }
    37.  
    38. LineAccel::~LineAccel()
    39. {
    40. }
    41.  
    42. void LineAccel::setAccel(const QKeySequence & a)
    43. {
    44. ks = a;
    45.  
    46. setText(ks);
    47. }
    48.  
    49. QKeySequence LineAccel::accel()
    50. {
    51. return ks;
    52. }
    53.  
    54. QPopupMenu * LineAccel::createPopupMenu()
    55. {
    56. return 0;
    57. }
    58.  
    59. void LineAccel::keyPressEvent(QKeyEvent * e)
    60. {
    61. int nextKey = e->key();
    62.  
    63. if (nextKey == QObject::Key_Control ||
    64. nextKey == QObject::Key_Shift ||
    65. nextKey == QObject::Key_Meta ||
    66. nextKey == QObject::Key_Alt)
    67. return;
    68.  
    69. int modifier = translateModifiers(e->state());
    70.  
    71. nextKey |= modifier;
    72.  
    73. setAccel(QKeySequence(nextKey));
    74. }
    75.  
    76. int LineAccel::translateModifiers(int state)
    77. {
    78. int result = 0;
    79.  
    80. if (state & QObject::ShiftButton)
    81. result |= QObject::SHIFT;
    82.  
    83. if (state & QObject::ControlButton)
    84. result |= QObject::CTRL;
    85.  
    86. if (state & QObject::MetaButton)
    87. result |= QObject::META;
    88.  
    89. if (state & QObject::AltButton)
    90. result |= QObject::ALT;
    91.  
    92. return result;
    93. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QLineEdit and keyPressEvent(QKeyEvent *)

    Thing I know is that Ctrl+A is never passed to keyPressEvent. I've installed my event filter but it also didn't help.

  3. #3
    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: QLineEdit and keyPressEvent(QKeyEvent *)

    I'd suggest "stealing" Designer's code.

  4. #4
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QLineEdit and keyPressEvent(QKeyEvent *)

    Designer uses QListViewItem. I switched to KListView because there is no hope to find out what's wrong with QLineEdit. Something is filtering Ctrl+A before sequence reaches recently installed event handler. Some magic behind ;[

  5. #5
    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: QLineEdit and keyPressEvent(QKeyEvent *)

    QObject::event() probably

    And Ctrl+A is probably a shortcut to "Select all", so you might need to disable the shortcut.

  6. #6
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QLineEdit and keyPressEvent(QKeyEvent *)

    But how disable shortcut?

    event() - no.

    I think event is first passed through event filter chain, then to event() and finally to specific event function:

    event
    |
    event filter (n) -> event filter (n - 1) -> ... -> event filter (0) -> event() -> keyPressEvent(), where event filter (n) is latest installed event filter

    I cannot catch this sequence at event filter (n) level so it's obvious I won't catch it at event() level.

  7. #7
    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: QLineEdit and keyPressEvent(QKeyEvent *)

    Quote Originally Posted by fear View Post
    But how disable shortcut?
    Hard to say without looking at the code. I'd say it's an action associated with the context menu of the widget.

    I cannot catch this sequence at event filter (n)
    What did you filter it with?

Similar Threads

  1. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13
  2. how to get last character of QLineEdit
    By yagabey in forum Qt Programming
    Replies: 1
    Last Post: 5th January 2008, 16:38
  3. QLineEdit and focusInEvent
    By fuzzywuzzy01 in forum Qt Programming
    Replies: 5
    Last Post: 16th August 2007, 23:05
  4. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 01:25
  5. a box around QLineEdit?
    By GreyGeek in forum Qt Tools
    Replies: 13
    Last Post: 8th February 2006, 15:40

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.