QT键盘键值捕获

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1、QT4.6widget工程项目2、在窗体上上放置一个label,命名为lbl_keyPad.3、widget.h中public:Widget(QWidget*parent=0);~Widget();添加voidkeyPressEvent(QKeyEvent*);4、widget.cpp中头文件添加#includeQKeyEvent内容添加//**********************keyboard键盘键值捕获*************************voidWidget::keyPressEvent(QKeyEvent*k){switch(k-key()){caseQt::Key_0:ui-lbl_keyPad-setText('0'Pressed!);break;caseQt::Key_1:ui-lbl_keyPad-setText('1'Pressed!);break;caseQt::Key_2:ui-lbl_keyPad-setText('2'Pressed!);break;caseQt::Key_3:ui-lbl_keyPad-setText('3'Pressed!);break;caseQt::Key_4:ui-lbl_keyPad-setText('4'Pressed!);break;caseQt::Key_5:ui-lbl_keyPad-setText('5'Pressed!);break;caseQt::Key_6:ui-lbl_keyPad-setText('6'Pressed!);break;caseQt::Key_7:ui-lbl_keyPad-setText('7'Pressed!);break;caseQt::Key_8:ui-lbl_keyPad-setText('8'Pressed!);break;caseQt::Key_9:ui-lbl_keyPad-setText('9'Pressed!);break;caseQt::Key_F1:ui-lbl_keyPad-setText('F1'Pressed!);break;caseQt::Key_F2:ui-lbl_keyPad-setText('F2'Pressed!);break;caseQt::Key_F3:ui-lbl_keyPad-setText('F3'Pressed!);break;caseQt::Key_F4:ui-lbl_keyPad-setText('F4'Pressed!);break;caseQt::Key_F5:ui-lbl_keyPad-setText('F5'Pressed!);break;caseQt::Key_F6:ui-lbl_keyPad-setText('F6'Pressed!);break;caseQt::Key_Asterisk:ui-lbl_keyPad-setText('*'Pressed!);break;caseQt::Key_Slash:ui-lbl_keyPad-setText('/'Pressed!);break;default:ui-lbl_keyPad-setText(OtherKeyPressed!);break;}}运行后,按下对应的键,窗体效果如下:

1 / 2
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功