当用户在QWidget
基于窗口中键入时,我想要QLineEdit
处理所有输入键,所以我尝试了以下两种解决keyPressEvent()
方案QWidget
:
一种。
void Window::keyPressEvent (QKeyEvent *e)
{
switch (e->key())
{
// handle other short cuts
default:
QApplication::sendEvent (lineEdit , e);
break;
}
}
好吧,这有时会使整个界面崩溃,尤其是当我resize window
.
B.
void Window::keyPressEvent (QKeyEvent *e)
{
switch (e->key())
{
// handle other short cuts
default:
if ( ! lineEdit.hasFocus () )
{
lineEdit.setFocus ();
lineEdit.setText (e->key());
// i wanted to push the first key input to that QLineEdit , but how ?
// or i'll miss it
}
break;
}
}
此外,我一直在考虑给予lineEdit
焦点,但我不能这样做,因为其他事件需要由主 UI 处理。
更新
当我过滤关键输入时它不会崩溃,但为什么呢?
default:
if ( e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete ||
(e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z )
)
QApplication::sendEvent(filter , e);
break;
}