PDA

View Full Version : Getting keyboard events while in a drag and drop state



gerrysweeney
9th July 2012, 13:24
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

sonulohani
9th July 2012, 13:48
Use QEvent::KeyPress , QEvent::KeyRelease event in this...

gerrysweeney
9th July 2012, 15:24
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

high_flyer
9th July 2012, 15:28
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/qwidget.html#keyPressEvent
http://qt-project.org/doc/qt-4.8/qobject.html#eventFilter

gerrysweeney
10th July 2012, 11:07
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: -



class EdaSymbolListWidget : public QListWidget
{
protected:
void startDrag(Qt::DropActions /*supportedActions*/)
{
QListWidgetItem *item = currentItem();
QMimeData *mimeData = new QMimeData;
QByteArray ba;
ba = item->text().toLatin1().data();
mimeData->setData("application/x-my-custom-item", ba);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);

drag->exec(Qt::MoveAction);
}


void keyPressEvent(QKeyEvent *key)
{
qDebug("EdaSymbolListWidget Key Event: %x", key->key());

QListWidget::keyPressEvent(key);
}

};


I also have the following override in my QGraphicsView derived class




void MyGraphicsView::keyPressEvent(QKeyEvent *key)
{
qDebug("View Key Event: %x", key->key());

QGraphicsView::keyPressEvent(key);
}


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

high_flyer
10th July 2012, 11:19
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/qdropevent.html#keyboardModifiers