Results 1 to 6 of 6

Thread: QGraphicsTextItem focus problem

  1. #1
    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 QGraphicsTextItem focus problem

    I've been playing with QGraphicsTextItem to display editable property texts of the components(QGraphicsItems). I found this helpful.
    But unfortunately now the shortcut assigned to one of QAction interferes while editing text in textitem.
    That QAction is assigned to delete key whose job is to delete selected components.
    Because of this delete key doesn't work in the edit which triggers the action instead.
    ShortcutContexts were helpless here. I can't even use grabKeyboard() since it is not a widget.

    Please do help me solve this problem.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsTextItem focus problem

    How about enabling/disabling the action on the fly, more or less like this:
    Qt Code:
    1. class MyTextItem : public QGraphicsTextItem // QGraphicsTextItem inherits QObject
    2. {
    3. Q_OBJECT
    4. ...
    5.  
    6. signals:
    7. void editing(bool);
    8.  
    9. protected:
    10. void focusInEvent(QFocusEvent* event) {
    11. QGraphicsTextItem::focusInEvent(event);
    12. emit editing(true);
    13. }
    14.  
    15. void focusOutEvent(QFocusEvent* event) {
    16. QGraphicsTextItem::focusOutEvent(event);
    17. emit editing(false);
    18. }
    19. };
    20.  
    21. connect(textItem, SIGNAL(editing(bool)), deleteAction, SLOT(setDisabled(bool)));
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    Gopala Krishna (26th June 2007)

  4. #3
    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: QGraphicsTextItem focus problem

    Quote Originally Posted by jpn View Post
    How about enabling/disabling the action on the fly, more or less like this:
    Thanks. It should work but there are many more keybindings that interferes, QKeySequence::Undo,QKeySequence::Redo.... I just made a note of them from source of QTextControl. Probably i need to add all action that interferes in a list and enable/disable them appropriately. But what if these shortcuts vary with qt versions - ? I mean if the next version adds/removes a shortcut. Oh!

    I really tried hard by going through the souce code of QShortCutMap, QTextControl,QAction,QApplication .. but still there seems to be no clean hack to do this. QGraphicsView::grabKeyboard() doesn't help either since its child of mainwindow Are there any other tricks - possibly eventFilter , actionEvent ..

    I am using Qt-4.2.3 on x11.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  5. #4
    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: QGraphicsTextItem focus problem

    Quote Originally Posted by Gopala Krishna View Post
    I just made a note of them from source of QTextControl. Probably i need to add all action that interferes in a list and enable/disable them appropriately. But what if these shortcuts vary with qt versions - ? I mean if the next version adds/removes a shortcut.
    For now I had to stick on to this solution. I worte the followingg code based on suggestion of jpn.
    Qt Code:
    1. void QucsMainWindow::setInterferingActionsDisabled(bool state)
    2. {
    3. foreach(QAction *action, interferingActions) {
    4. action->setDisabled(state);
    5. #if QT_VERSION < 0x040300
    6. Qt::ShortcutContext c = state ? Qt::WidgetShortcut : Qt::WindowShortcut;
    7. action->setShortcutContext(c);
    8. #endif
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Yup you do need that preprocessor directive since qt-4.2.* has a bug where the action, though disabled, responds to the shortcuts associated. But it seems to be fixed in qt4.3

    BTW, is modifying shortcut context a heavy operation since i may be ececuting this slot quite often ?

    I still will be happy to modify my code if someone can help me with more cleaner hack
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsTextItem focus problem

    Another way could be to install an event filter on the application object:
    Qt Code:
    1. class MyTextItem : public QGraphicsTextItem // QGraphicsTextItem inherits QObject
    2. {
    3. Q_OBJECT
    4. ...
    5.  
    6. public:
    7. bool eventFilter(QObject* object, QEvent* event)
    8. {
    9. if (event->type() == QEvent::Shortcut ||
    10. event->type() == QEvent::ShortcutOverride)
    11. {
    12. if (!object->inherits("QGraphicsView"))
    13. {
    14. event->accept();
    15. return true;
    16. }
    17. }
    18. return false;
    19. }
    20.  
    21. protected:
    22. void focusInEvent(QFocusEvent* event) {
    23. QGraphicsTextItem::focusInEvent(event);
    24. qApp->installEventFilter(this);
    25. }
    26.  
    27. void focusOutEvent(QFocusEvent* event) {
    28. QGraphicsTextItem::focusOutEvent(event);
    29. qApp->removeEventFilter(this);
    30. }
    31. };
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    Gopala Krishna (26th June 2007)

  8. #6
    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: QGraphicsTextItem focus problem [SOLVED]

    Quote Originally Posted by jpn View Post
    Another way could be to install an event filter on the application object:
    Qt Code:
    1. class MyTextItem : public QGraphicsTextItem // QGraphicsTextItem inherits QObject
    2. {
    3. Q_OBJECT
    4. ...
    5.  
    6. public:
    7. bool eventFilter(QObject* object, QEvent* event)
    8. {
    9. if (event->type() == QEvent::Shortcut ||
    10. event->type() == QEvent::ShortcutOverride)
    11. {
    12. if (!object->inherits("QGraphicsView"))
    13. {
    14. event->accept();
    15. return true;
    16. }
    17. }
    18. return false;
    19. }
    20.  
    21. protected:
    22. void focusInEvent(QFocusEvent* event) {
    23. QGraphicsTextItem::focusInEvent(event);
    24. qApp->installEventFilter(this);
    25. }
    26.  
    27. void focusOutEvent(QFocusEvent* event) {
    28. QGraphicsTextItem::focusOutEvent(event);
    29. qApp->removeEventFilter(this);
    30. }
    31. };
    To copy to clipboard, switch view to plain text mode 
    Hey that works like a charm. Thanks a lot!
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

Similar Threads

  1. QGraphicsTextItem size
    By Angelo Moriconi in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2007, 08:34
  2. Tab/Enter focus problem
    By b1 in forum Qt Programming
    Replies: 4
    Last Post: 23rd October 2006, 23:34
  3. Replies: 3
    Last Post: 26th September 2006, 12:16
  4. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.