Results 1 to 6 of 6

Thread: Getting keyboard events while in a drag and drop state

  1. #1
    Join Date
    Jun 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Getting keyboard events while in a drag and drop state

    Hi, Does anyone have any idea how to receive keyboard events in the target widget of am in-progress drag and drop operation. As far as I can tell neither the source or target widgets are receiving keyboard events during the drag operation. However, pressing the escape key terminates the drag operation so the keyboard events are definitely being handles somewhere.

    I am new to Qt so if you do answer please don’t assume I am an expert L)

    Thanks

    Gerry

  2. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Getting keyboard events while in a drag and drop state

    Use QEvent::KeyPress , QEvent::KeyRelease event in this...
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  3. #3
    Join Date
    Jun 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Getting keyboard events while in a drag and drop state

    Hi,

    Thanks for your response. I am a bit lost as I am a newbie to Qt, how does one use QEvent::KeyPress like this?

    Gerry

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Getting keyboard events while in a drag and drop state

    It is assumed that you are proficient with C++.
    If so:
    QWidget has event handlers for mouse and keyboard.
    You have to overload them or, install and eventFilter to catch them.
    See the docs for more info and examples:
    http://qt-project.org/doc/qt-4.8/qwi...#keyPressEvent
    http://qt-project.org/doc/qt-4.8/qob...ml#eventFilter
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jun 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Getting keyboard events while in a drag and drop state

    Hi,

    Yes I am fine with C++, but I am new to Qt. I have already tried what you suggest. I am dragging from a QListWidget derived class to a QGraphicsView derived class. In my code I have the following: -

    Qt Code:
    1. class EdaSymbolListWidget : public QListWidget
    2. {
    3. protected:
    4. void startDrag(Qt::DropActions /*supportedActions*/)
    5. {
    6. QListWidgetItem *item = currentItem();
    7. QMimeData *mimeData = new QMimeData;
    8. ba = item->text().toLatin1().data();
    9. mimeData->setData("application/x-my-custom-item", ba);
    10. QDrag *drag = new QDrag(this);
    11. drag->setMimeData(mimeData);
    12.  
    13. drag->exec(Qt::MoveAction);
    14. }
    15.  
    16.  
    17. void keyPressEvent(QKeyEvent *key)
    18. {
    19. qDebug("EdaSymbolListWidget Key Event: %x", key->key());
    20.  
    21. QListWidget::keyPressEvent(key);
    22. }
    23.  
    24. };
    To copy to clipboard, switch view to plain text mode 

    I also have the following override in my QGraphicsView derived class


    Qt Code:
    1. void MyGraphicsView::keyPressEvent(QKeyEvent *key)
    2. {
    3. qDebug("View Key Event: %x", key->key());
    4.  
    5. QGraphicsView::keyPressEvent(key);
    6. }
    To copy to clipboard, switch view to plain text mode 

    When I focus the list or view I get the key presses in the debug window as I would expect. However, once a start a drag operation from the QListWidget pressing keys no longer calls the keyPressEvent in either view, and thats where I am stuck

    Gerry

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Getting keyboard events while in a drag and drop state

    Now I see what you mean.
    This is because the drag operation is a blocking operation (exec()) and the main event loop is waiting for the drag to finish, hence other events are not handled.
    You can ask your QDropEvent on the receiver side for any modifier keys:
    http://qt-project.org/doc/qt-4.8/qdr...boardModifiers
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 0
    Last Post: 9th July 2012, 13:23
  2. Replies: 2
    Last Post: 13th October 2010, 21:51
  3. Handle Drag & Drop Events with QGraphicsItem [SOLVED]
    By guilugi in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2010, 18:05
  4. Replies: 0
    Last Post: 4th May 2010, 10:24
  5. Query keyboard state in Qt 3
    By Pieter from Belgium in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2006, 17:20

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.