ユーザーがQWidget
ベースのウィンドウに入力すると、すべての入力キーを処理する必要があったため、次の 2 つのソリューションをQLineEdit
試しました。keyPressEvent()
QWidget
A.
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;
}