Qt 事件处理 快捷键(重写eventFilter的函数,使用Qt::ControlModifier判断)

CTRL+Enter发送信息的实现

在现在的即时聊天程序中,一般都设置有快捷键来实现一些常用的功能,类似QQ可以用CTRL+Enter来实现信息的发送。

在QT4中,所有的事件都继承与QEvent这个类,以下是用QEvent来实现快捷键的功能。

首先所有QT类的基类QObject有一个成员函数installEventFilter,这个函数是用来将一个事件处理器和该QObject绑定起来,所以就有了我下面的想法。

首先在chat类定义一个eventFilter,该函数是一个虚函数,可以由子类进行更改。所以声明eventFilter如下:

virtual bool eventFilter(QObject *obj, QEvent *e);

看了下QT文档对于这个函数的声明的解释,大概意思就是如果你要过滤某个事件就返回false,如果要使用某个事件就返回true。

我想在这个函数中加入对Enter键和Ctrl+Enter组合键的判断,具体查看QKeyEvent类的文档

QKeyEvent类中有函数key和modifier,key函数返回的是发生时间的按键值,modifier返回的而是修饰键,QT所支持的修饰键如下:

Constant Value Description

Qt::NoModifier 0x00000000 No modifier key is pressed.

Qt::ShiftModifier 0x02000000 A Shift key on the keyboard is pressed.

Qt::ControlModifier 0x04000000 A Ctrl key on the keyboard is pressed.

Qt::AltModifier 0x08000000 An Alt key on the keyboard is pressed.

Qt::MetaModifier 0x10000000 A Meta key on the keyboard is pressed.

Qt::KeypadModifier 0x20000000 A keypad button is pressed.

Qt::GroupSwitchModifier 0x40000000 X11 only. A Mode_switch key on the keyboard is pressed.

所以可以重写eventFilter函数来实现快捷键的功能,可以根据QKeyEvent的key和modifire来分别是Enter还是Ctrl+enter被按下。

重写eventFilter的函数如下:

 

[cpp] view plain copy
 
  1. bool Window::eventFilter(QObject *obj, QEvent *e)  
  2. {  
  3.      Q_ASSERT(obj == ui.inputMsgEdit);  
  4.      if (e->type() == QEvent::KeyPress)  
  5.      {  
  6.          QKeyEvent *event = static_cast(e);  
  7.          if (event->key() == Qt::Key_Return && (event->modifiers() & Qt::ControlModifier))  
  8.          {  
  9.               sendMessage();  
  10.               return true;  
  11.           }  
  12.       }  
  13.       return false;  
  14. }  

 

然后把这个过滤器用installEventFilter函数安装在聊天窗口的输入框上(QTextEdit),这样就实现快捷键的功能了。

三键组合Shift + Ctrl + A的实现

只要在窗体中相应keyPressEvent事件函数即可。

 

[cpp] view plain copy
 
  1. void Window::keyPressEvent(QKeyEvent *e)  
  2. {  
  3.       if (e->modifiers() == (Qt::ShiftModifier | Qt::ControlModifier) && e->key() == Qt::Key_A)  
  4.       {  
  5.           //pressed   
  6.       }  
  7. }  

 

键盘按住Ctrl键 + 鼠标左键的实现

在窗体中相应mousePressEvent事件函数在其中检测Ctrl键是否按住即可。

 

[cpp] view plain copy
 
  1. void Window::mousePressEvent(QMouseEvent *e)  
  2. {  
  3.     // 获取鼠标在点击窗体上的坐标   
  4.     QPoint pos = e->pos();  
  5.     if (e->button() == LeftButton)  
  6.     {  
  7.         if ( QApplication::keyboardModifiers () == Qt::ControlModifier)  
  8.         {  
  9.            //pressed   
  10.         }  
  11.      }  
  12. }  

 

一个例子:

 

 

[cpp] view plain copy
 
  1. #include "mainwindow.h"    
  2. #include <QKeyEvent>    
  3. #include <QMessageBox>    
  4. #include <QDebug>    
  5. #include <QShortcut>    
  6. #include <QPushButton>    
  7. #include <QHBoxLayout>    
  8. #include <QAction>    
  9.     
  10. MainWindow::MainWindow(QWidget *parent) :    
  11.     QMainWindow(parent)    
  12. {        
  13.     // 设置窗口布局    
  14.     QWidget *mainWidget = new QWidget(this);    
  15.     
  16.     QPushButton *button1 = new QPushButton("MessageBoxA", this);    
  17.     QPushButton *button2 = new QPushButton("MessageBoxB", this);    
  18.     
  19.     QHBoxLayout *layout = new QHBoxLayout(this);    
  20.     
  21.     layout->addWidget(button1);    
  22.     layout->addWidget(button2);    
  23.     
  24.     mainWidget->setLayout(layout);    
  25.     
  26.     this->setCentralWidget(mainWidget);    
  27.     this->resize(200, 100);    
  28.     this->setWindowTitle("Test");    
  29.     
  30.     // 创建QAction并设置快捷键    
  31.     QAction *actionA = new QAction(this);    
  32.     actionA->setShortcut(QKeySequence(tr("Ctrl+A")));    
  33.     // actionA->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_A));    
  34.     
  35.     QAction *actionB = new QAction(this);    
  36.     actionB->setShortcut(QKeySequence(tr("Ctrl+B")));    
  37.     // actionA->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_B));    
  38.     
  39.     // 给button添加快捷键    
  40.     // this->addAction(actionA);// 没用    
  41.     
  42.     button1->addAction(actionA);// 必须将快捷键添加到按钮上,同时使keyPressEvent失效,无法捕捉到Ctrl+A    
  43.     button2->addAction(actionB);    
  44.     
  45.     // 设置快捷键相应    
  46.     connect(actionA, &QAction::triggered, this, &MainWindow::button1_click);    
  47.     connect(actionB, &QAction::triggered, this, &MainWindow::button2_click);    
  48.     // 设置按钮点击事件相应    
  49.     connect(button1, &QPushButton::clicked, this, &MainWindow::button1_click);    
  50.     connect(button2, &QPushButton::clicked, this, &MainWindow::button2_click);    
  51.     
  52.     // 占用快捷键,使keyPressEvent函数失效    
  53.     QShortcut *shortCut1 = new QShortcut(QKeySequence(tr("Delete")), this);    
  54.     QShortcut *shortCut2 = new QShortcut(QKeySequence(tr("Return")), this);    
  55.     // QShortcut *shortCut2 = new QShortcut(tr("Return"), this);    
  56. }    
  57.     
  58. MainWindow::~MainWindow()    
  59. {    
  60.     
  61. }    
  62. // 如果快捷键被设置,keyPressevent就无法处理相应的按钮事件    
  63. void MainWindow::keyPressEvent(QKeyEvent *event)    
  64. {    
  65.     switch (event->key())    
  66.     {    
  67.         case Qt::Key_0:    
  68.         {    
  69.             qDebug() << Qt::Key_0;    
  70.             QMessageBox::about(NULL, "0", "0");    
  71.             break;    
  72.         }    
  73.         case Qt::Key_Return:// 被占用,失效    
  74.         {    
  75.             qDebug() << Qt::Key_Return;    
  76.             QMessageBox::about(NULL, "return", "return");    
  77.             break;    
  78.         }    
  79.         case Qt::Key_Delete:// 被占用,失效    
  80.         {    
  81.             qDebug() << Qt::Key_Delete;    
  82.             QMessageBox::about(NULL, "Delete", "Delete");    
  83.             break;    
  84.         }    
  85.         case QKeySequence::Delete:// 捕获不到,不能有这种方式处理    
  86.         {    
  87.             qDebug() << QKeySequence::Delete;    
  88.             QMessageBox::about(NULL, "QKeySequence::Delete", "QKeySequence::Delete");    
  89.             break;    
  90.         }    
  91.         case Qt::Key_F1:    
  92.         {    
  93.             qDebug() << Qt::Key_F1;    
  94.             QMessageBox::about(NULL, "F1", "F1");    
  95.             break;    
  96.         }    
  97.         case Qt::Key_O:// 处理组合快捷按钮    
  98.         {    
  99.             if (event->modifiers() & Qt::ControlModifier)    
  100.             {    
  101.                 QMessageBox::about(NULL, "Ctr+O", "Ctr+O");    
  102.             }    
  103.             break;    
  104.         }    
  105.         case Qt::Key_A:// 被窗口button占用,失效,不能处理    
  106.         {    
  107.             if (event->modifiers() & Qt::ControlModifier)    
  108.             {    
  109.                 QMessageBox::about(NULL, "Ctr+A", "Ctr+A");    
  110.             }    
  111.             break;    
  112.         }    
  113.         default:    
  114.         {    
  115.             qDebug() << event->key();    
  116.         }    
  117.     }    
  118. //    if (event->key() == Qt::Key_O)    
  119. //    {    
  120. //        if (event->modifiers() & Qt::ControlModifier)    
  121. //        {    
  122. //            QMessageBox::about(NULL, "Ctr+O", "Ctr+O");    
  123. //        }    
  124. //     }    
  125. }    
  126.     
  127. void MainWindow::keyReleaseEvent(QKeyEvent *event)    
  128. {    
  129.     
  130. }    
  131.     
  132. void MainWindow::button1_click()    
  133. {    
  134.     QMessageBox::about(NULL, "MessageBoxA", "MessageBoxA");    
  135. }    
  136.     
  137. void MainWindow::button2_click()    
  138. {    
  139.     QMessageBox::about(NULL, "MessageBoxB", "MessageBoxB");    
  140. }    

 

http://blog.csdn.net/caoshangpa/article/details/77933937 

 

原文链接:http://blog.csdn.net/yang382197207/article/details/45363967

原文链接:http://blog.csdn.net/xj626852095/article/details/12039093

 

posted @ 2017-06-15 20:42  findumars  Views(3883)  Comments(0Edit  收藏  举报