eventHooker::eventHooker(TForm *parent) : QObject(parent){ main = parent;}bool eventHooker::eventFilter(QObject *obj, QEvent *event){ if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); if (obj->objectName() == "tvTemplate") { if (keyEvent->key() == Qt::Key_Delete) { main->deleteTemplate(); return true; } } return false; } else { return QObject::eventFilter(obj, event); }}
void keyPressEvent(QKeyEvent *e){ QTreeView::keyPressEvent(e); emit onKeyPressed(e);}
class MyTreeView : public QTreeView{ Q_OBJECT public: MyTreeView(QWidget *parent = 0) : QTreeView(parent) { /** */ } virtual ~MyTreeView() { /** */ } protected: void keyPressEvent(QKeyEvent *event) { event->ignore(); emit onKeyPressed(event); if (!event->isAccepted()) QTreeView::keyPressEvent(event); } public slots: void currentChanged(const QModelIndex ¤t, const QModelIndex &previous) { emit onChanged(current, previous); } signals: void onKeyPressed(QKeyEvent *event); void onChanged(const QModelIndex &index, const QModelIndex &previous);};
connect(new QShortcut(QKeySequence(Qt::Key_Insert), tvTemplate),SIGNAL(activated()),this,SLOT(newTemplate()));
new QShortcut( Qt::Key_Insert, tvTemplate, SLOT( newTemplate() ), 0, Qt::WidgetShortcut );
connect(new QShortcut(Qt::Key_Insert, tvTemplate, 0, 0, Qt::WidgetShortcut),SIGNAL(activated()),this,SLOT(newTemplate()));