Results 1 to 4 of 4

Thread: Disable default tab behaviour for a QGraphicsItem

  1. #1
    Join Date
    Apr 2007
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Disable default tab behaviour for a QGraphicsItem

    I have a QGraphicsItem in a QGraphicsView as part of a MainWindow. I'd like to have a the QGraphicsItem have custom behaviour for a tab key press. However, the MainWindow seems to intercept the tab key and use it to move to the next widget after the QGraphicsView in the window.

    There is a workaround for this to allow QWidgets to have tab key behaviour:

    http://doc.trolltech.com/4.2/eventsandfilters.html

    But this doesn't seem to work as QGraphicsItems don't naturally derive event behaviour from either QObject or QWidget.

    I've also tried calling event->accept() in MyGraphicsWidget::keyPressEvent(), but it doesn't seem to get a chance to even accept it (for example, if ( event->key() == Qt::Key_Tab ) qDebug( "tab" ) produces no output ).

    Is there a way to have custom tabkey behaviour for QGraphicsItems?

    Thanks in advance, Neil.
    Qpsycle -- open-source modular music studio built with Qt

  2. #2
    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: Disable default tab behaviour for a QGraphicsItem

    First of all make sure the graphics view is the current widget (has focus). If you want to catch tab keys you have to reimplement event() - for either the main window (last resort) or the view widget. Then you'll be able to forward the event to the scene or simply make sure it's not handled by the default implementation of event(). If you have trouble doing that, you can always "eat up" the event and either call some method that does what you want directly or post another key event that will then be handled by the scene or one of the items.

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

    nmather (21st April 2007)

  4. #3
    Join Date
    Apr 2007
    Posts
    17
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Disable default tab behaviour for a QGraphicsItem

    Thanks Wysota, I got it to work by overriding QGraphicsView::event()

    In case it helps others, here is what I did:

    Qt Code:
    1. bool MyGraphicsView::event( QEvent *event )
    2. {
    3. switch (event->type()) {
    4. case QEvent::KeyPress: {
    5. QKeyEvent *k = (QKeyEvent *)event;
    6. QGraphicsView::keyPressEvent(k);
    7. if (k->key() == Qt::Key_Backtab
    8. || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier))
    9. || (k->key() == Qt::Key_Tab) ) {
    10. event->accept();
    11. }
    12. return true;
    13. }
    14. default:
    15. return QGraphicsView::event( event );
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Don't know if it's necessarily the best way to do it, but it seems OK for now.
    Qpsycle -- open-source modular music studio built with Qt

  5. #4
    Join Date
    Nov 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Thumbs up Re: Disable default tab behaviour for a QGraphicsItem

    Thanks. I managed to simulate selection using TAB thanks to this post solution.
    Here is my code sample:

    Qt Code:
    1. bool FacadeGraphicsView::event( QEvent *event )
    2. {
    3. switch (event->type())
    4. {
    5. case QEvent::KeyPress:
    6. {
    7. QKeyEvent *k = (QKeyEvent *)event;
    8. QGraphicsView::keyPressEvent(k);
    9. if (k->key() == Qt::Key_Backtab
    10. || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier))
    11. || (k->key() == Qt::Key_Tab) )
    12. {
    13. event->accept();
    14. m_facadeScene()->manageTabSelection();
    15. }
    16. return true;
    17. }
    18. default:
    19. return QGraphicsView::event( event );
    20. }
    21. }
    22.  
    23. CustomScene* FacadeGraphicsView::m_customScene()
    24. {
    25. auto pCustomScene = dynamic_cast<CustomScene*>(scene());
    26. return pCustomScene;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void CustomScene::manageTabSelection()
    2. {
    3. if ((selectedItems().count() == 0) || (selectedItems().count() > 1))
    4. {
    5. foreach( QGraphicsItem *item, items() )
    6. {
    7. auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
    8. if( current)
    9. {
    10. current->setSelected(false);
    11. }
    12. }
    13.  
    14. foreach( QGraphicsItem *item, items() )
    15. {
    16. auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
    17. if( current)
    18. {
    19. current->setSelected(true);
    20. break;
    21. }
    22. }
    23. }
    24. else
    25. {
    26. bool found = false;
    27. // Find a selected item
    28. foreach( QGraphicsItem *item, items() )
    29. {
    30. auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
    31. if( current)
    32. {
    33. if (current->isSelected())
    34. {
    35. current->setSelected(false);
    36. found = true;
    37. }
    38. else
    39. {
    40. if (found)
    41. {
    42. current->setSelected(true);
    43. break;
    44. }
    45. }
    46. }
    47. }
    48.  
    49. // if selected was the last item, ensure to select at least one item
    50. if (selectedItems().count() == 0)
    51. {
    52. foreach( QGraphicsItem *item, items() )
    53. {
    54. auto current = dynamic_cast<QGraphicsPolygonItem*>( item );
    55. if( current)
    56. {
    57. current->setSelected(true);
    58. break;
    59. }
    60. }
    61. }
    62. }
    63. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31

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.