bool ZealSearchEdit::eventFilter(QObject *obj, QEvent *ev) { if (obj == this && ev->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev); if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up) { QModelIndex index = treeView->currentIndex(); int nextRow = keyEvent->key() == Qt::Key_Down ? index.row() + 1 : index.row() - 1; QModelIndex sibling = index.sibling(nextRow, 0); if (nextRow >= 0 && nextRow < treeView->model()->rowCount()) { treeView->setCurrentIndex(sibling); return true; } } if (keyEvent->key() == Qt::Key_Return) { emit treeView->activated(treeView->selectionModel()->currentIndex()); return true; } // Autocompletes the prefixes. if (keyEvent->key() == Qt::Key_Tab) { QString currentText = text(); QString completed = currentCompletion(currentText); if (!completed.isEmpty()) { setText(completed); return true; } } } return LineEdit::eventFilter(obj, ev); }
void ZealSearchEdit::showCompletions(const QString &newValue) { int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); int textWidth = fontMetrics().width(newValue); prefixCompleter->setCompletionPrefix(text()); QString completed = currentCompletion(newValue).mid(newValue.size()); QSize labelSize(fontMetrics().width(completed), size().height()); completionLabel->setMinimumSize(labelSize); completionLabel->move(frameWidth + 2 + textWidth, frameWidth); completionLabel->setText(completed); }
bool SearchEdit::event(QEvent *event) { if (event->type() != QEvent::KeyPress) return QLineEdit::event(event); // Tab key cannot be overriden in keyPressEvent() if (reinterpret_cast<QKeyEvent *>(event)->key() != Qt::Key_Tab) return QLineEdit::event(event); const QString completed = currentCompletion(text()); if (!completed.isEmpty()) setText(completed); return true; }