Results 1 to 4 of 4

Thread: Stop keyboar-scrolling of QGraphicsView / Filter KeyPressEvents

  1. #1
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Stop keyboar-scrolling of QGraphicsView / Filter KeyPressEvents

    The Node Editor I am currently working on is coming along nicely.

    nodeEdit1.jpg

    However there is a hopefully small issue with the QGraphicsView behaving to Cursor Key Presses.
    Right now the Scene is catching the Keyboard Events.
    For example the Cursor Keys Move all selected Nodes around.
    Scrolling is realised with Scroll Hand Drag. However , by default, the Cursor Keys move the Scroll bars around to.

    I tried to shut them out like this:
    Qt Code:
    1. void nodegraphicsview::keyPressEvent(QKeyEvent *event)
    2. {
    3. if(event->key()==Qt::Key_Up || event->key()==Qt::Key_Down || event->key()==Qt::Key_Left || event->key()==Qt::Key_Right)
    4. {
    5. //do nothing
    6. }
    7. else
    8. {
    9. QGraphicsView::keyPressEvent(event);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    Witch works, but now I also have a QLineEdit Widget in my Scene that shows up on top of a Node after DoubleClicking the Title to Rename it.
    The Cursor keys are then Blocked for the Line Edit as well.
    Can I forward the events directly to the LineEdit somehow?
    Any other Ideas how solve this?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Stop keyboar-scrolling of QGraphicsView / Filter KeyPressEvents

    You could try with an event filter and examining the recipient object.
    If it is the line edit allow the event to pass, if it is the view, don't.
    Maybe need to check the application's focusWidget if the recipient parameter of the event filter doesn't work.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Stop keyboar-scrolling of QGraphicsView / Filter KeyPressEvents

    This is way harder than I expected!
    I did a combination of Eventfilter and Focuschecking.

    The Slot looks like this (where renameproxy is the QGraphicsProxyWidget of the LineEdit):
    Qt Code:
    1. void MainWindow::checkfocus(QGraphicsItem *newFocusItem, QGraphicsItem *oldFocusItem, Qt::FocusReason reason)
    2. {
    3. if (newFocusItem==renameproxy)
    4. {
    5. ui->graphicsView->removeEventFilter(filter);
    6. }
    7. else
    8. {
    9. ui->graphicsView->installEventFilter(filter);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    and is connected to the Scene like so:
    Qt Code:
    1. connect(scene,SIGNAL(focusItemChanged(QGraphicsItem*,QGraphicsItem*,Qt::FocusReason)),this,SLOT(checkfocus(QGraphicsItem *, QGraphicsItem *, Qt::FocusReason)));
    To copy to clipboard, switch view to plain text mode 

    It would work to if I just had the LineEdit Widget. However now I cant move the selected Nodes around any more.
    After around 3 h of experimenting and testing it's starting to get me frustrated.
    One Possible solution I found would be to reimplement scrollContentsBy() of the GraphicsView. but this also means that I would have to handle draging the Scene around myself.
    From all that experimenting my Code looks quite messy right now. After fixing this, and a break I probably will try to implement the dragscoll and post the result.

  4. #4
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default <SOLVED> Stop keyboar-scrolling of QGraphicsView / Filter KeyPressEvents

    I finally did it without messing with scrollContentsBy().
    After Subclassing QLineEdit I could simply filter out outgoing Up and Down Keypresses.(Left and Right are handled by QLineEdit anyway)

    Qt Code:
    1. void customLineEdit::keyPressEvent(QKeyEvent *event)
    2. {
    3.  
    4. if (event->key()!=Qt::Key_Up && event->key()!=Qt::Key_Down)
    5. {
    6. QLineEdit::keyPressEvent(event);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    The FocusCheck part had to be modified slightly to:

    Qt Code:
    1. void MainWindow::checkfocus(QGraphicsItem *newFocusItem, QGraphicsItem *oldFocusItem, Qt::FocusReason reason)
    2. {
    3. if (newFocusItem==renameproxy)
    4. {
    5. emit deselectall();
    6. }
    7. else
    8. {
    9. renameproxy->hide();
    10. }
    11.  
    12. if (newFocusItem!=NULL)
    13. {
    14. scene->removeEventFilter(filter);
    15. }
    16. else
    17. {
    18. scene->installEventFilter(filter);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    In addition I had to deselect all Nodes in my Scene to avoid moving them around while navigating inside the LineEdit.
    It is still not perfect, but I am getting there....

    @Admin:
    I want to set the Thread to solved but the Edit button in the first post vanished. How do I do that?
    Why is there no Button or Checkbox to do this?
    Last edited by 0backbone0; 28th August 2015 at 17:49.

Similar Threads

  1. QGraphicsView and Scrolling
    By validator in forum Qt Programming
    Replies: 5
    Last Post: 8th September 2017, 01:27
  2. Stop repaint while scrolling in paintEvent()
    By sagirahmed in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2010, 07:21
  3. Custom scrolling of QGraphicsView
    By hb in forum Qt Programming
    Replies: 0
    Last Post: 12th August 2008, 11:10
  4. problem with LineEdits and keyPressEvents
    By impeteperry in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2007, 23:12
  5. qtextbrowser stop scrolling on append
    By tpf80 in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2007, 21:28

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.