/************************************************ eventFilter for ui->commandEd ************************************************/ bool Dialog::editKeyPressEvent(QKeyEvent *event) { switch (event->key()) { case Qt::Key_N: if (event->modifiers().testFlag(Qt::ControlModifier)) { QKeyEvent ev(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier); editKeyPressEvent(&ev); return true; } return false; case Qt::Key_P: if (event->modifiers().testFlag(Qt::ControlModifier)) { QKeyEvent ev(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier); editKeyPressEvent(&ev); return true; } return false; case Qt::Key_Up: case Qt::Key_PageUp: if (ui->commandEd->text().isEmpty() && ui->commandList->isVisible() && ui->commandList->currentIndex().row() == 0 ) { setFilter("", false); return true; } qApp->sendEvent(ui->commandList, event); return true; case Qt::Key_Down: case Qt::Key_PageDown: if (ui->commandEd->text().isEmpty() && ui->commandList->isHidden() ) { setFilter("", true); return true; } qApp->sendEvent(ui->commandList, event); return true; case Qt::Key_Tab: const CommandProviderItem *command = mCommandItemModel->command(ui->commandList->currentIndex()); if (command) ui->commandEd->setText(command->title()); return true; } return QDialog::eventFilter(ui->commandList, event); }
bool Dialog::eventFilter(QObject *object, QEvent *event) { if (event->type() == QEventKeyPress) // QEvent::KeyPress { QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event); if (object == ui->commandEd) return editKeyPressEvent(keyEvent); if (object == ui->commandList) return listKeyPressEvent(keyEvent); } return QDialog::eventFilter(object, event); }