1. 鼠标事件:
鼠标事件包含移动、按下、松开、单机、双击等。
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void mouseDoubleClickEvent(QMouseEvent *e);
QMouseEvent 类的e->x()和e->y()方法,可以获取鼠标相对于接受事件的窗体位置(相对位置);e->globalX()和e->globalY()方法可以获取鼠标相对窗口的系统位置。
e->button()方法区分鼠标按键的类型。
void MouseEvent::mousePressEvent(QMouseEvent *e)
{
QString str="("+QString::number(e->x())+","+QString::number(e->y()) +")";
if(e->button()==Qt::LeftButton) {
statusBar()->showMessage(QStringLiteral("左键:")+str);
}
else if(e->button()==Qt::RightButton) {
statusBar()->showMessage(tr("右键:")+str);
}
else if(e->button()==Qt::MidButton) {
statusBar()->showMessage(tr("中键:")+str);
}
}
2. 键盘事件:
void keyPressEvent(QKeyEvent *e);
QKeyEvent 类的e->modifiers()(Qt::KeyboardModifiers modifiers()),判断修饰键是否按下;e->key()方法判断其他键是否按下。
KeyboardModifiers 枚举值:
NoModifier | 没有修饰键按下 |
ShiftModifier | 【Shift】键按下 |
ControlModifier | 【Ctrl】键按下 |
AltModifier | 【Alt】键按下 |
MetaModifier | Meta键按下,(Meta 是Sun系统下的,也和WIN键一样) |
KeypadModifier | 小键盘上的键按下(例如Keypad0小键盘0,Keypad1小键盘1) |
GroupSwitchModifier | Mode Switch键按下 |
KeyboardModifierMask |
event->key()的枚举值Key见文章最后的表格。
3. 事件过滤:
Qt的事件模型中提供的事件过滤器功能使得一个QObject对象可以监视另一个QObject对象中的事件,通过在一个QObject对象中安装事件过滤器,可以在事件到达该对象前捕获事件,从而起到监视对象事件的效果。
EventFilter::EventFilter(QWidget *parent,Qt::WindowFlags f)
: QDialog(parent,f)
{
setWindowTitle(tr("事件过滤"));
label1 = new QLabel;
label1->installEventFilter(this); // 安装事件过滤器,指定窗体(this)为监视事件的对象
}
监视事件的对象通过eventFilter()函数接收事件。如果某个事件需要被过滤掉,即停止正常的事件响应,则在eventFilter()函数中返回true,否则返回false;
QObject的removeEventFilter()可以解除已安装的事件过滤器。
bool EventFilter::eventFilter(QObject *watched, QEvent *event)
{
if(watched==label1) //首先判断当前发生事件的对象
{
if(event->type()==QEvent::MouseButtonPress) //判断发生的事件类型
{
//将事件event转化为鼠标事件
QMouseEvent *mouseEvent=(QMouseEvent *)event;
/* 以下根据鼠标的按键类型分别显示 */
if(mouseEvent->buttons()&Qt::LeftButton) {
stateLabel->setText(tr("左键按下左边图片"));
}
else if(mouseEvent->buttons()&Qt::MidButton) {
stateLabel->setText(tr("中键按下左边图片"));
}
else if(mouseEvent->buttons()&Qt::RightButton) {
stateLabel->setText(tr("右键按下左边图片"));
}
/* 显示缩小的图片 */
QMatrix matrix;
matrix.scale(1.8,1.8);
QImage tmpImg=Image1.transformed(matrix);
label1->setPixmap(QPixmap::fromImage(tmpImg));
}
/* 鼠标释放事件的处理,恢复图片的大小 */
else if (event->type()==QEvent::MouseButtonRelease)
{
stateLabel->setText(tr("鼠标释放左边图片"));
label1->setPixmap(QPixmap::fromImage(Image1));
}
}
//将事件交给上层对话框
return QDialog::eventFilter(watched,event);
}
QKeyEvent的Key枚举值:
Qt::Key_Escape | 0x01000000 | Esc键 |
Qt::Key_Tab | 0x01000001 | Tab键 |
Qt::Key_Backtab | 0x01000002 | Tab补全键 |
Qt::Key_Backspace | 0x01000003 | 退格键 |
Qt::Key_Return | 0x01000004 | Return键 |
Qt::Key_Enter | 0x01000005 | 回车键 |
Qt::Key_Insert | 0x01000006 | Insert键 |
Qt::Key_Delete | 0x01000007 | Delete键 |
Qt::Key_Pause | 0x01000008 | 暂停键 |
Qt::Key_Print | 0x01000009 | 截图键 |
Qt::Key_SysReq | 0x0100000a | PrtSc |
Qt::Key_Clear | 0x0100000b | 清除键 |
Qt::Key_Home | 0x01000010 | Home键 |
Qt::Key_End | 0x01000011 | End键 |
Qt::Key_Left | 0x01000012 | ← |
Qt::Key_Up | 0x01000013 | ↑ |
Qt::Key_Right | 0x01000014 | → |
Qt::Key_Down | 0x01000015 | ↓ |
Qt::Key_PageUp | 0x01000016 | 上页 |
Qt::Key_PageDown | 0x01000017 | 下页 |
Qt::Key_Shift | 0x01000020 | Shift键 |
Qt::Key_Control | 0x01000021 | Ctrl键 |
Qt::Key_Alt | 0x01000023 | Alt键 |
Qt::Key_AltGr | 0x01001103 | 右侧Alt |
Qt::Key_CapsLock | 0x01000024 | 大写键 |
Qt::Key_NumLock | 0x01000025 | NumLock |
Qt::Key_ScrollLock | 0x01000026 | 滚动锁定 |
Qt::Key_F1 | 0x01000030 | F1~F12 |
Qt::Key_F2 | 0x01000031 | |
Qt::Key_F3 | 0x01000032 | |
Qt::Key_F4 | 0x01000033 | |
Qt::Key_F5 | 0x01000034 | |
Qt::Key_F6 | 0x01000035 | |
Qt::Key_F7 | 0x01000036 | |
Qt::Key_F8 | 0x01000037 | |
Qt::Key_F9 | 0x01000038 | |
Qt::Key_F10 | 0x01000039 | |
Qt::Key_F11 | 0x0100003a | |
Qt::Key_F12 | 0x0100003b | |
Qt::Key_Menu | 0x01000055 | 菜单键 |
Qt::Key_Help | 0x01000058 | Help键 |
Qt::Key_Space | 0x20 | 空格键 |
Qt::Key_Exclam | 0x21 | ! |
Qt::Key_QuoteDbl | 0x22 | 引用 |
Qt::Key_NumberSign | 0x23 | # |
Qt::Key_Dollar | 0x24 | $ |
Qt::Key_Percent | 0x25 | % |
Qt::Key_Ampersand | 0x26 | & |
Qt::Key_Apostrophe | 0x27 | 分词符" ' " |
Qt::Key_ParenLeft | 0x28 | ( |
Qt::Key_ParenRight | 0x29 | ) |
Qt::Key_Asterisk | 0x2a | * |
Qt::Key_Plus | 0x2b | + |
Qt::Key_Comma | 0x2c | , |
Qt::Key_Minus | 0x2d | - |
Qt::Key_Period | 0x2e | 。 |
Qt::Key_Slash | 0x2f | / |
Qt::Key_0 | 0x30 | 数字0~9 |
Qt::Key_1 | 0x31 | |
Qt::Key_2 | 0x32 | |
Qt::Key_3 | 0x33 | |
Qt::Key_4 | 0x34 | |
Qt::Key_5 | 0x35 | |
Qt::Key_6 | 0x36 | |
Qt::Key_7 | 0x37 | |
Qt::Key_8 | 0x38 | |
Qt::Key_9 | 0x39 | |
Qt::Key_Colon | 0x3a | : |
Qt::Key_Semicolon | 0x3b | ; |
Qt::Key_Less | 0x3c | < |
Qt::Key_Equal | 0x3d | 等于 |
Qt::Key_Greater | 0x3e | > |
Qt::Key_Question | 0x3f | ? |
Qt::Key_At | 0x40 | @ |
Qt::Key_A | 0x41 | 字母键 |
Qt::Key_B | 0x42 | |
Qt::Key_C | 0x43 | |
Qt::Key_D | 0x44 | |
Qt::Key_E | 0x45 | |
Qt::Key_F | 0x46 | |
Qt::Key_G | 0x47 | |
Qt::Key_H | 0x48 | |
Qt::Key_I | 0x49 | |
Qt::Key_J | 0x4a | |
Qt::Key_K | 0x4b | |
Qt::Key_L | 0x4c | |
Qt::Key_M | 0x4d | |
Qt::Key_N | 0x4e | |
Qt::Key_O | 0x4f | |
Qt::Key_P | 0x50 | |
Qt::Key_Q | 0x51 | |
Qt::Key_R | 0x52 | |
Qt::Key_S | 0x53 | |
Qt::Key_T | 0x54 | |
Qt::Key_U | 0x55 | |
Qt::Key_V | 0x56 | |
Qt::Key_W | 0x57 | |
Qt::Key_X | 0x58 | |
Qt::Key_Y | 0x59 | |
Qt::Key_Z | 0x5a | |
Qt::Key_BracketLeft | 0x5b | [ |
Qt::Key_Backslash | 0x5c | \ |
Qt::Key_BracketRight | 0x5d | ] |
Qt::Key_AsciiCircum | 0x5e | ^ |
Qt::Key_Underscore | 0x5f | _ |
Qt::Key_QuoteLeft | 0x60 | “ |
Qt::Key_BraceLeft | 0x7b | { |
Qt::Key_Bar | 0x7c | | |
Qt::Key_BraceRight | 0x7d | } |
Qt::Key_AsciiTilde | 0x7e | ~ |
Qt::Key_nobreakspace | 0x0a0 | 不换行空格 |
Qt::Key_exclamdown | 0x0a1 | ! |
Qt::Key_cent | 0x0a2 | 美分 |
Qt::Key_sterling | 0x0a3 | 英镑 |
Qt::Key_currency | 0x0a4 | 货币 |
Qt::Key_yen | 0x0a5 | 日元 |