PDA

View Full Version : keyPressEvent public or should it be protected



taraj
17th June 2020, 02:54
Hi,

Is there any issues with making the slot keyPressEvent() public?

I have a QWidget (called say A) that has a button on it which opens a QWidget window (called B) which gains the focus and I want certain keys entered on the keyboard to be ignored by 'B' and passed through to 'A' to be able to use. The only way I can get this to work is the move the header definition of keyPressEvent() from protected to public... is this a bad thing to do or is there a better way around it...


void B::keyPressEvent(QKeyEvent *k)
{
if((B->hasFocus()) && !(k->key() >= Qt::Key_F1 && k->key() <= Qt::Key_F20))
{
QApplication::sendEvent(B->focusWidget(), k);
}
else
{
A->keyPressEvent(k); //This doesn't work if it is protected slot in A.h needs to be public slot
}
}

Thank you for any help.

Ginsengelf
17th June 2020, 08:16
Hi, you could send the event through the event system using QCoreApplication::postEvent() or QCoreApplication::sendEvent(). That way the keyPressEvent() can stay protected.

Ginsengelf

taraj
17th June 2020, 11:50
Thank you sendEvent worked