Hello,

This is my first post so please be gentle.

I am having trouble getting keyPressEvents in a QGraphicsItem to work. What I have is a QDialog that builds (in the QDialog constructor) a QGraphicsView, creates a QGraphicsScene, initializes two QGraphicsItems (one is a pixmap the other is a sort of "cursor" that is displayed on top of the pixmap at various locations) then places the items in the scene and then sets the scene in the view. I then set the focus to the "cursor" object by setting setFlags( IsFocusable ) and calling Item.setFocus().

I have overridden the QKeyEvent method in my cursor and am looking for the 'N' key, but it doesn't show up.

I appreciate any help you may give as I don't have much hair left...

Here is some c++ code (I'm still learning c++ and probably will be for quite some time)...

Qt Code:
  1. // imap_cal_cursor.h
  2. #include <QGraphicsItem>
  3. #include <QObject>
  4.  
  5. class CImapCalCursor : public QObject, public QGraphicsItem
  6. {
  7. Q_OBJECT
  8.  
  9. public:
  10. CImapCalCursor();
  11. void keyPressEvent( QKeyEvent *event );
  12. }
  13.  
  14.  
  15. //imap_cal_cursor.cpp
  16. #include "imap_cal_cursor.h"
  17.  
  18. #include <QGraphicsScene>
  19. #include <QPainter>
  20. #include <QStyleOption>
  21. #include <QKeyEvent>
  22.  
  23. #include "syslog.h"
  24.  
  25. CImapCalCursor::CImapCalCursor() : QObject(), QGraphicsItem()
  26. {
  27. }
  28.  
  29. void CImapCalCursor::keyPressEvent( QKeyEvent *event )
  30. {
  31. switch (event->key()) {
  32. case Qt::Key_N:
  33. syslog( LOG_DEBUG, "DEBUG: Received Key_N event" );
  34. break;
  35. }
  36. }
  37.  
  38.  
  39. // calibration_dialog.h
  40. #include <QDialog>
  41. #include <QDialogButtonBox>
  42. #include "imap_cal_image.h"
  43. #include "imap_cal_cursor.h"
  44.  
  45. class CCalibration : public QDialog
  46. {
  47. Q_OBJECT
  48.  
  49. public:
  50. QDialogButtonBox *buttonBox;
  51. QGraphicsView *graphix_area;
  52.  
  53. protected:
  54. class CImapCalImage Image;
  55. class CImapCalCursor cur;
  56. QList< QPoint > list;
  57. int current_list_index;
  58. QGraphicsPixmapItem *pixmapItem;
  59.  
  60. public:
  61. CCalibration( QWidget * parent = 0, PEZG_COMMBLK EZGSharedMemPtr = NULL );
  62. };
  63.  
  64.  
  65. //calibration_dialog.cpp
  66. #include <QtGui>
  67. #include <QColor>
  68.  
  69. #include "calibration_dialog.h"
  70. #include "syslog.h"
  71.  
  72. CCalibration::CCalibration(QWidget * parent, PEZG_COMMBLK EZGSharedMemPtr )
  73. : QDialog(parent)
  74. {
  75. PIM_DIGBUFFER SourceImagePtr;
  76. imageHeader *SourceImageHeaderPtr;
  77. unsigned char *SourceImageDataPtr;
  78.  
  79. SourceImagePtr = &EZGSharedMemPtr->sImProcDigBuffer;
  80. SourceImageHeaderPtr = &SourceImagePtr->Header;
  81. SourceImageDataPtr = SourceImagePtr->image;
  82.  
  83. setObjectName(QString::fromUtf8("Calibration_Dialog"));
  84. buttonBox = new QDialogButtonBox( this );
  85. buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
  86. buttonBox->setGeometry(QRect(400, 630, 161, 32));
  87. buttonBox->setOrientation(Qt::Horizontal);
  88. buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok);
  89.  
  90. graphix_area = new QGraphicsView( this );
  91. graphix_area->setObjectName(QString::fromUtf8("graphix_area"));
  92. graphix_area->setGeometry(QRect(20, 40, 951, 571));
  93. graphix_area->setCacheMode(QGraphicsView::CacheBackground);
  94. graphix_area->setRenderHint(QPainter::Antialiasing);
  95.  
  96. Scene = new QGraphicsScene( this );
  97.  
  98. Image = CImapCalImage( SourceImageDataPtr, SourceImageHeaderPtr->imWidth, SourceImageHeaderPtr->imHeight, QImage::Format_Indexed8 );
  99. Image.GenerateColorMap();
  100. pixmapItem = Scene->addPixmap( QPixmap::fromImage( Image ) );
  101.  
  102. list = Image.ScanForPoints();
  103. if( list.isEmpty() || list.size() == 0 ) {
  104. syslog( LOG_ERR, "ERROR: ScanForPoints returned empty list" );
  105. current_list_index = -1;
  106. }
  107. else {
  108. current_list_index = 0;
  109. Scene->addItem( &cur );
  110. cur.setZValue( 10 );
  111. cur.setPos( pixmapItem->mapToParent( list[ current_list_index ] ) );
  112. cur.setFlag( QGraphicsItem::ItemIsFocusable );
  113. }
  114.  
  115. graphix_area->setScene( Scene );
  116. Scene->setFocusItem( &cur );
  117.  
  118. QSize size(988, 684);
  119. size = size.expandedTo(minimumSizeHint());
  120. resize(size);
  121. }
To copy to clipboard, switch view to plain text mode