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