April 14, 2012

B3nNy B3nNy
Lab Rat
4 posts

KeyPressEvent while dragging

 

Hi i have problem with implemented keyPressEvent() function in my custom QGLWidget,
my widget is not responding to this event while dragging.

I even set focus and grab keyboard on dragEnterEvent()

Can anybody pls help? I am pretty desperate.

THX in advance

7 replies

April 14, 2012

RaubTieR RaubTieR
Lab Rat
42 posts

Can I ask why do you need drag there? Because if that is for mouse controls, then there are better options to implement it without any conflict with keyboard…

April 14, 2012

B3nNy B3nNy
Lab Rat
4 posts

I am using drag and drop, because i have list of items in table widget and i am dragging them from this widget to QGLWidget.

April 14, 2012

RaubTieR RaubTieR
Lab Rat
42 posts

Are those items come from another window/separate widget, or table widget is a child for QGLWidget?
If QGLWidget loses focus, then window messages, like keyboard ones will go to someone else, and then you drop on QGLWidget and it gains focus again. That may happen.

April 14, 2012

B3nNy B3nNy
Lab Rat
4 posts

They are in the same window, but as separate widgets. And I also call setFocus(), and grabKeyboard() on dragEnterEvent(). This gives QGLWidget focus, but keyborad is not responding.

I solve this problem with reprograming this functionality with enter/leaveEvent and mousePress,Release and move events.

Thanks a lot for your time anyway:)

April 15, 2012

Sam Sam
Ant Farmer
677 posts

If you want to drag and drop items from tableWidget to QGLWidget u need to override the following methods .

1. Table Widget:

  1. protected:
  2.   void dragEnterEvent(QDragEnterEvent *event);
  3.   void dragMoveEvent(QDragMoveEvent *event);
  4.   void dropEvent(QDropEvent *event);
  5.   void startDrag(Qt::DropActions supportedActions);

2. QGLWidget

  1. protected:
  2.     void dragEnterEvent(QDragEnterEvent *event);
  3.     void dragMoveEvent(QDragMoveEvent *event);
  4.     void dropEvent(QDropEvent *event);
  5.     void mousePressEvent(QMouseEvent *event);
  6.     void paintEvent(QPaintEvent *event);

The following examples in the documentation gives an idea about how the drag and drop can be implemented:

1. Draggable Icons Example.
2. Drag and Drop Puzzle Example.

I have also done something similar to what you require but with a listView and a QWidget. If u need any further help feel free to ask :)

April 15, 2012

B3nNy B3nNy
Lab Rat
4 posts

I have no problem with implementing it that way. But yesterday I ┬┤ve found this in Qt Doc on QDrag->exec()
On Windows, the Qt event loop is blocked during the operation.

So i think, i wouldn’t be albe to use keyboard input during the drag and drop operation.

April 15, 2012

Sam Sam
Ant Farmer
677 posts

Yes it seems that the keyPressEvent() function is blocked when dragging an item from the list/table.
But, if you press “escape(Esc)” on the keyboard the dragging of item is cancelled ,still keyPressEvent is not fired.

 
  ‹‹ Search in my Model efficient way?      integrating Qt with visual c++ 2008 ››

You must log in to post a reply. Not a member yet? Register here!