PDA

View Full Version : QDialog keypress event



bear35645
30th January 2015, 22:40
I am trying to capture the keypress event in a QDialog.

In my constructor, I have this as the last line:
QObject::installEventFilter(this);


Then I have this method:


bool frmFileDialog::eventFilter(QObject *obj, QEvent *event)
{
QKeyEvent *keyEvent;

if(event->type() == QEvent::KeyPress){
keyEvent = static_cast<QKeyEvent *>(event);
}
else{
return QObject::eventFilter(obj, event);
}
if(keyEvent->isAutoRepeat()){
return QObject::eventFilter(obj, event);
}

if(keyEvent->key() == Qt::Key_1){
keyEvent->accept();
this->close();
return true;
}
else{
return QObject::eventFilter(obj, event);
}
}


The issue I am having is if any of regular keys are typed on the keyboard (i.e. the 1 key), the method is not entered at all. If "F1" or the tab key are pressed, it is entered. What do I need to capture any key that is pressed in this Dialog?

Thanks,
Kevin

stampede
31st January 2015, 07:51
What do I need to capture any key that is pressed in this Dialog?
Simply reimplement QDialog::keyPressEvent (http://qt-project.org/doc/qt-4.8/qdialog.html#keyPressEvent).

bear35645
3rd February 2015, 22:08
Actually I figured out the problem, but I am not sure how to fix it. I have a QTableView on the form. It is catching keystrokes first. I disabled the QTableView, then my keyPressEvent works. Can I disable the QTableView processing of keypresses and implement them in my form?

anda_skoa
4th February 2015, 09:13
You can install an event filter on the treeview and only let events pass that you don't need to handle yourself.

Cheers,
_

bear35645
4th February 2015, 22:34
I added a call to the grabKeyboard() method in the constructor, then when the proper key is pressed to close the form, I added releaseKeyBoard(). I realize I may have to handle the navigation in the table view, but that is Ok.

d_stranz
5th February 2015, 23:47
I added a call to the grabKeyboard()

That seems a rather extreme solution when simply adding an event filter to the table view would do it. Any time you grab control of an input device, there is always the chance that some corner case will lead to a condition where your app is locked up and there is no way to get back to the widget that has grabbed the inputs.


I realize I may have to handle the navigation in the table view

Which is exactly what the event filter would let you easily do - as anda_skoa said, you let events pass that you don't need to handle yourself.

Kryzon
6th February 2015, 06:51
Which is exactly what the event filter would let you easily do - you let events pass that you don't need to handle yourself.
Or even let all events pass, if you just need to use the filter as a means to act on some event(s) before the target widget does.