- mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 #include <QTextEdit> 6 #include <QEvent> 7 #include <QtEvents> 8 9 namespace Ui { 10 class MainWindow; 11 } 12 13 class MainWindow : public QMainWindow 14 { 15 Q_OBJECT 16 17 public: 18 explicit MainWindow(QWidget *parent = 0); 19 ~MainWindow(); 20 bool eventFilter(QObject *,QEvent *); 21 22 private: 23 Ui::MainWindow *ui; 24 QTextEdit *pedit; 25 }; 26 27 #endif // MAINWINDOW_H
- mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 #include <QDebug> 4 5 MainWindow::MainWindow(QWidget *parent) : 6 QMainWindow(parent), 7 ui(new Ui::MainWindow) 8 { 9 ui->setupUi(this); 10 pedit=new QTextEdit(this); 11 //设置中心控件 12 setCentralWidget(pedit); 13 //设置对象的过滤 14 pedit->installEventFilter(this); 15 } 16 17 MainWindow::~MainWindow() 18 { 19 delete ui; 20 } 21 22 //进行对象的过滤 23 bool MainWindow::eventFilter(QObject *o, QEvent *e) 24 { 25 //处理对象是pedit传来的消息,处理完了不希望其他人处理 26 // if(o==pedit) 27 // { 28 // QKeyEvent *pe = static_cast<QKeyEvent *>(e); 29 // if(e->type() == QEvent::KeyPress) 30 // { 31 // qDebug()<<pe->key(); 32 // } 33 // return false; 34 // } 35 36 //处理快捷键 37 if(e->type()==QEvent::KeyPress) 38 { 39 QKeyEvent *pe = static_cast<QKeyEvent *>(e); 40 if(pe->key() == Qt::Key_K && (Qt::ControlModifier & pe->modifiers())) 41 { 42 qDebug() << "control + K"; 43 } 44 } 45 46 return QMainWindow::eventFilter(o,e); 47 }
转载于:https://www.cnblogs.com/xiaochi/p/8747652.html