Results 1 to 8 of 8

Thread: problem in eventFilter of QGraphicsView()

  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default problem in eventFilter of QGraphicsView()

    hi friends,
    in QGraphicsView i am try to install event filter to an item .. to capture a key ...
    Qt Code:
    1. RecRepView::RecRepView(QWidget *parent)
    2. : QGraphicsView(parent)
    3. {
    4. QRectF bounds(0, -20, 700, 500);
    5. scene = new QGraphicsScene(bounds, this);
    6. setScene(scene);
    7. //this is the QGraphicsItem
    8. logout = new BackItem(QRectF(0, 0, 70, 65));
    9. logout->setPos(20, 405);
    10. logout->setFlag(QGraphicsItem::ItemIsSelectable);
    11. logout->installEventFilter(this);
    12. connect(logout, SIGNAL(closeSignal()), qApp, SLOT(quit()));
    13. scene->addItem(logout);
    14. }
    To copy to clipboard, switch view to plain text mode 

    and in event filter
    Qt Code:
    1. bool RecRepView:: eventFilter(QObject *ob, QEvent *e)
    2. {
    3. printf("is it coming inside ..\n");
    4. if(ob == logout && e->type() == QEvent::KeyPress) {
    5. const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
    6. if(ke->key()==Qt::Key_F1){
    7. some cond .....
    8. }
    9. return true;
    10. }
    11. return QWidget::eventFilter(ob, e);
    12. }
    To copy to clipboard, switch view to plain text mode 

    but even the printf output is not displaying .....
    i dont know where i done the mistake ... please help ....

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: problem in eventFilter of QGraphicsView()

    Install the filter on the scene or view, not on the item.
    Qt Code:
    1. this->installEventFilter(this);
    2. //...
    3. return QGraphicsView::eventFilter(ob, e);
    To copy to clipboard, switch view to plain text mode 

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

    wagmare (19th May 2009)

  4. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in eventFilter of QGraphicsView()

    Quote Originally Posted by Lykurg View Post
    Install the filter on the scene or view, not on the item.
    Qt Code:
    1. this->installEventFilter(this);
    2. //...
    3. return QGraphicsView::eventFilter(ob, e);
    To copy to clipboard, switch view to plain text mode 
    of course i try that one too ....

    my full code
    Qt Code:
    1. RecRepView::RecRepView(QWidget *parent)
    2. : QGraphicsView(parent)
    3. {
    4. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    5. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    6. setFrameShape(QFrame::NoFrame);
    7. setCacheMode(CacheBackground);
    8. setViewportUpdateMode(FullViewportUpdate);
    9. setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    10. setStyleSheet("background:#00002E");
    11. QRectF bounds(0, -20, 700, 500);
    12. scene = new QGraphicsScene(bounds, this);
    13. setScene(scene);
    14. this->installEventFilter(this);
    15. signalMapper = new QSignalMapper();
    16. int i =0;
    17. mapValue[i] = STARTVALUE;
    18. button1 = new GraphButton(QRectF(0,0, 150, 40), 1);
    19. button1->setFlag(QGraphicsItem::ItemIsSelectable);
    20. button1->setPos(475, 330);
    21. scene->addItem(button1);
    22. connect(button1, SIGNAL(activated()), signalMapper, SLOT(map()));
    23. signalMapper->setMapping(button1,mapValue[i]);
    24. i++;
    25.  
    26.  
    27. mapValue[i] = STOPVALUE;
    28. button2 = new GraphButton(QRectF(0,0, 150, 40), 2);
    29. button2->setFlag(QGraphicsItem::ItemIsSelectable);
    30. button2->setPos(475, 400);
    31. scene->addItem(button2);
    32. connect(button2, SIGNAL(activated()), signalMapper, SLOT(map()));
    33. signalMapper->setMapping(button2,mapValue[i]);
    34.  
    35. logout = new BackItem(QRectF(0, 0, 70, 65));
    36. logout->setPos(20, 405);
    37. logout->setFlag(QGraphicsItem::ItemIsSelectable);
    38. // log connect(logout, SIGNAL(closeSignal()), qApp, SLOT(quit()));
    39. scene->addItem(logout);
    40.  
    41. connect(signalMapper, SIGNAL(mapped(int )), this, SIGNAL(activated(int)));
    42.  
    43. connect(this, SIGNAL(activated(int )), this, SLOT(recRepSlot(int )));
    44.  
    45. }
    46.  
    47. void RecRepView ::resizeEvent(QResizeEvent *event)
    48. {
    49. QGraphicsView::resizeEvent(event);
    50. fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
    51. }
    52.  
    53. void RecRepView::drawBackground(QPainter *painter, const QRectF &rect)
    54. {
    55. Q_UNUSED(rect);
    56.  
    57. QRectF rects(450,310, 200, 160);
    58. QPen pen(QColor(211, 210, 212), 0.5);
    59. painter->setBrush(QColor(128, 128, 128));
    60. painter->setPen(pen);
    61. painter->drawRect(rects);
    62.  
    63. }bool RecRepView:: eventFilter(QObject *ob, QEvent *e)
    64. {
    65. if(ob == logout && e->type() == QEvent::KeyPress) {
    66. const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
    67. if(ke->key()==Qt::Key_F1){
    68. printf("is it coming inside ..\n");
    69.  
    70. }
    71. return true;
    72. }
    73. return QWidget::eventFilter(ob, e);
    74. }
    75. void RecRepView::recRepSlot(int value)
    76. {
    77. printf("the value coming inside :%d \n", value);
    78.  
    79. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. GraphButton::GraphButton(const QRectF &rect, int itemNo)
    2. {
    3. }
    4.  
    5. void GraphButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
    6. {
    7. if(event->button() != Qt::LeftButton)
    8. {
    9. QGraphicsRectItem::mousePressEvent(event);
    10. return;
    11. }
    12. emit activated();
    13. }
    To copy to clipboard, switch view to plain text mode 

    please help ....

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in eventFilter of QGraphicsView()

    What is the point of installing an event filter on "this"? Why do you need the event filter in the first place? Can't you use the regular means of handling events?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in eventFilter of QGraphicsView()

    when the user press key <F1> .. i have to close the application or else user can click logout item .. like wise there are seven buttons <F2>, <F3> ,<F4> ... , each having different functions ... so i am planning to catch the event of pressing F1 ,... and try to do the option ....
    why this one is not working ..? shall i use QAction instead for this ..?
    "Behind every great fortune lies a crime" - Balzac

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: problem in eventFilter of QGraphicsView()

    Quote Originally Posted by wagmare View Post
    when the user press key <F1> .. i have to close the application or else user can click logout item .. like wise there are seven buttons <F2>, <F3> ,<F4> ... , each having different functions ... so i am planning to catch the event of pressing F1 ,... and try to do the option ....
    why this one is not working ..? shall i use QAction instead for this ..?
    well the item has to be focused to get the event - I guess. If you whant a global short cut - seems to me you whant one - use a QAction with QAction::setShortcutContext(Qt::ApplicationShortcut).

  8. The following user says thank you to Lykurg for this useful post:

    wagmare (19th May 2009)

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in eventFilter of QGraphicsView()

    At "worst" you can reimplement QGraphicsView::keyPressEvent(). Event filters are for something else.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    wagmare (19th May 2009)

  11. #8
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem in eventFilter of QGraphicsView()

    we have to install event filter on QGraphicsScene() not in QGraphicsView() itself ..
    scene->installEventFilter(this);
    now the above code working perfectly .
    "Behind every great fortune lies a crime" - Balzac

Similar Threads

  1. Replies: 2
    Last Post: 26th February 2009, 10:12
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  4. Smooth pixmap transform in QGraphicsView problem
    By spud in forum Qt Programming
    Replies: 1
    Last Post: 24th October 2007, 16:47
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.