Pass return/enter and arrow keys from search box to tree view

This commit is contained in:
Jerzy Kozera 2013-01-20 00:22:33 +00:00
parent 93cf5a8c0a
commit fde2910228
5 changed files with 66 additions and 3 deletions

View File

@ -29,6 +29,7 @@ MainWindow::MainWindow(QWidget *parent) :
}
}
ui->setupUi(this);
ui->lineEdit->setTreeView(ui->treeView);
ui->treeView->setModel(&zealList);
ui->treeView->setColumnHidden(1, true);
connect(ui->treeView, &QTreeView::activated, [&](const QModelIndex& index) {
@ -38,6 +39,7 @@ MainWindow::MainWindow(QWidget *parent) :
ui->treeView->setModel(&zealSearch);
ui->treeView->reset();
ui->treeView->setColumnHidden(1, true);
ui->treeView->setCurrentIndex(zealSearch.index(0, 0, QModelIndex()));
});
connect(ui->lineEdit, &QLineEdit::textChanged, [&](const QString& text) {
if(!text.isEmpty()) {

View File

@ -23,7 +23,7 @@
<widget class="QWidget" name="">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLineEdit" name="lineEdit"/>
<widget class="ZealSearchEdit" name="lineEdit"/>
</item>
<item>
<widget class="QTreeView" name="treeView">
@ -78,6 +78,11 @@
<extends>QWidget</extends>
<header>QtWebKit/QWebView</header>
</customwidget>
<customwidget>
<class>ZealSearchEdit</class>
<extends>QLineEdit</extends>
<header>zealsearchedit.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>

View File

@ -16,13 +16,15 @@ SOURCES += main.cpp\
zeallistmodel.cpp \
zealsearchmodel.cpp \
zealdocsetsregistry.cpp \
zealsearchresult.cpp
zealsearchresult.cpp \
zealsearchedit.cpp
HEADERS += mainwindow.h \
zeallistmodel.h \
zealsearchmodel.h \
zealdocsetsregistry.h \
zealsearchresult.h
zealsearchresult.h \
zealsearchedit.h
FORMS += mainwindow.ui

28
zeal/zealsearchedit.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "zealsearchedit.h"
#include <QCoreApplication>
#include <QKeyEvent>
#include <QDebug>
ZealSearchEdit::ZealSearchEdit(QWidget *parent) :
QLineEdit(parent)
{
}
void ZealSearchEdit::setTreeView(QTreeView *view)
{
treeView = view;
this->installEventFilter(this);
}
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 ||
keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) {
QCoreApplication::instance()->sendEvent(treeView, keyEvent);
return true;
}
}
return QLineEdit::eventFilter(obj, ev);
}

26
zeal/zealsearchedit.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef ZEALSEARCHEDIT_H
#define ZEALSEARCHEDIT_H
#include <QEvent>
#include <QTreeView>
#include <QLineEdit>
class ZealSearchEdit : public QLineEdit
{
Q_OBJECT
public:
explicit ZealSearchEdit(QWidget *parent = 0);
void setTreeView(QTreeView *view);
protected:
bool eventFilter(QObject *obj, QEvent *ev);
signals:
public slots:
private:
QTreeView *treeView;
};
#endif // ZEALSEARCHEDIT_H