Results 1 to 4 of 4

Thread: QGraphicsView and item focus

  1. #1
    Join Date
    Jun 2007
    Posts
    56
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default QGraphicsView and item focus

    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 

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView and item focus

    Quote Originally Posted by Micawber View Post
    Hello,

    This is my first post so please be gentle.
    Welcome to the qtcentre!
    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.
    Well i think , the cursor item is sensing key events properly but syslog's output should be invisible for some reason. I wrote a small example and this works fine in windows. I just used message box to make sure i get feedback when a key is pressed. Here is code
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class KeyReceiver : public QGraphicsEllipseItem
    4. {
    5. public:
    6. KeyReceiver(const QRectF& rect) : QGraphicsEllipseItem(rect)
    7. {
    8. setFlags(ItemIsMovable | ItemIsFocusable | QGraphicsItem::ItemIsSelectable);
    9. }
    10.  
    11. void keyPressEvent ( QKeyEvent * event )
    12. {
    13. if(event->key() == Qt::Key_N)
    14. QMessageBox::warning(0,"dg","HF");
    15. QGraphicsEllipseItem::keyPressEvent(event);
    16. }
    17. };
    18.  
    19. int main(int argc,char *argv[])
    20. {
    21. QApplication app(argc,argv);
    22. QGraphicsScene scene(0,0,1000,800);
    23. KeyReceiver *r = new KeyReceiver(QRectF(10,10,100,80));
    24. scene.addItem(r);
    25. QGraphicsView view(&scene);
    26. view.show();
    27. r->setFocus();
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    Join Date
    Jun 2007
    Posts
    56
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QGraphicsView and item focus

    I tried calling QMessageBox in the keyPressEvent method but still no dice.

    I thought that perhaps since the dialog was still under construction and the items not yet visible, the calls to setFocus were being ignored. So I added a SetFocus method to my extended QDialog class and called it from my main application after I called show(). Still no dice.

    Obviously I am not understanding some key concept either in Qt or in C++.

  4. #4
    Join Date
    Jun 2007
    Posts
    56
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Lightbulb Re: QGraphicsView and item focus

    OK some more information...

    If I change my cursor class such that it is only a descendant of QGraphicsItem (remove the QObject ancestry and the timer stuff) it works. Any clue as to why having QObject ancestry would cause it to break???

    I need the timers and to emit signals.

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  2. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.