QT控件大全 三十八 QWidgetKeyboard
来源:互联网 发布:崩坏学园2官方淘宝 编辑:程序博客网 时间:2024/06/08 06:44
效果如图:
核心代码:
#ifndef __WIDGETKEYBOARD_H_#define __WIDGETKEYBOARD_H_#include <QWidget>#include "ui_WidgetKeyboard.h"class QSignalMapper;class WidgetKeyboard : public QWidget, public Ui::WidgetKeyboard{ Q_OBJECT private:QWidget *m_pParent;qreal opacity;bool isCaps;bool isShift;bool isCtrl;bool isAlt;bool isIns;QSignalMapper *signalMapper;QList<QToolButton *> allButtons;QString soundFilePath; private slots:void on_btn_clicked(int btn);void on_btnCaps_toggled(bool checked);void on_btnShiftLeft_toggled(bool checked);void on_btnShiftRight_toggled(bool checked);void on_btnCtrlLeft_toggled(bool checked);void on_btnAltLeft_toggled(bool checked);void on_btnIns_clicked();void on_btnPrint_clicked();void on_sliderOpacity_valueChanged(int);void changeTextShift(bool isShift);void changeTextCaps(bool isCaps);bool checkNotTextKey(int keyId);void on_checkBoxShowNumPad_stateChanged(int state); public:WidgetKeyboard(QWidget *parent = NULL);~WidgetKeyboard();void setOpacity(int);int getOpacity();void setSoundDisabled();void setSoundEnabled(QString soundPath);};#endif /*__WIDGETKEYBOARD_H_*/
#include "WidgetKeyboard.h"#include <QKeyEvent>#include <QSignalMapper>#include <QSound>#include <QMessageBox>#include <QDesktopWidget>#include <QClipboard>#include <QList>WidgetKeyboard::WidgetKeyboard(QWidget *parent) : QWidget(0){ setupUi(this); resize(0,0); this->setWindowFlags(Qt::Tool); m_pParent = parent; isCaps = false; isShift = false; isCtrl = false; isAlt = false;isIns = false; changeTextCaps(false); signalMapper = new QSignalMapper(this); sliderOpacity->setRange(20,100); allButtons = findChildren<QToolButton *>(); for (int i=0;i<allButtons.count();i++) { connect(allButtons.at(i), SIGNAL(clicked()), signalMapper, SLOT(map())); signalMapper->setMapping(allButtons.at(i), i); } connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(on_btn_clicked(int)));}WidgetKeyboard::~WidgetKeyboard(){delete signalMapper;}void WidgetKeyboard::on_btn_clicked(int btn){if(! soundFilePath.isEmpty())QSound::play(soundFilePath);QString strKeyId; strKeyId = allButtons.at(btn)->accessibleName(); bool isOk; int keyId = strKeyId.toInt(&isOk, 16); if (strKeyId.isEmpty() || !isOk) { QMessageBox::information(0,0,"Key Not Found"); return; } //Keys to be handled separatelyif (keyId==Qt::Key_Shift|| keyId==Qt::Key_Control|| keyId==Qt::Key_Alt|| keyId==Qt::Key_Print) return; int involvedKeys = 1; Qt::KeyboardModifiers Modifier = Qt::NoModifier; if (isCtrl) { Modifier = Modifier | Qt::ControlModifier; involvedKeys++; } if (isShift) { Modifier = Modifier | Qt::ShiftModifier; involvedKeys++; } if (isAlt) { Modifier = Modifier | Qt::AltModifier; involvedKeys++; } bool isTextKey = false;if(keyId==Qt::Key_Insert && !isShift)return;QString ch = allButtons.at(btn)->text().trimmed(); if (ch=="&&") ch="&";else if (keyId==Qt::Key_Space) ch = " "; else if (checkNotTextKey(keyId))ch = QString();elseisTextKey = true; if(isIns && isTextKey) {QKeyEvent keyEventIns(QEvent::KeyPress, Qt::Key_Delete, Qt::NoModifier);QApplication::sendEvent(m_pParent->focusWidget(), &keyEventIns);}QKeyEvent keyEvent(QEvent::KeyPress, keyId, Modifier, ch, false, involvedKeys); QApplication::sendEvent(m_pParent->focusWidget(), &keyEvent); btnShiftLeft->setChecked(false);btnShiftRight->setChecked(false); btnCtrlLeft->setChecked(false); btnAltLeft->setChecked(false);}void WidgetKeyboard::on_btnCaps_toggled(bool checked){ changeTextCaps(checked); isCaps = checked;}void WidgetKeyboard::on_btnShiftLeft_toggled(bool checked){ isShift = checked; if (isCaps) { changeTextShift(checked); } else { changeTextShift(checked); changeTextCaps(checked); }}void WidgetKeyboard::on_btnShiftRight_toggled(bool checked){ on_btnShiftLeft_toggled(checked);}void WidgetKeyboard::on_btnCtrlLeft_toggled(bool checked){ isCtrl = checked;}void WidgetKeyboard::on_btnAltLeft_toggled(bool checked){ isAlt = checked;}void WidgetKeyboard::on_btnIns_clicked(){isIns = !isIns;}void WidgetKeyboard::on_btnPrint_clicked(){QPixmap screen = QPixmap::grabWindow(QApplication::desktop()->winId());QClipboard *cb = QApplication::clipboard();cb->setPixmap(screen, QClipboard::Clipboard);}void WidgetKeyboard::on_sliderOpacity_valueChanged(int val){ opacity = 120.0 - val; opacity /= 100.0; setWindowOpacity(opacity); }void WidgetKeyboard::changeTextShift(bool isShift){ changeTextCaps(!isShift); if (isShift) {btnTilt->setText(QChar('~')); btn1->setText(QChar('!')); btn2->setText(QChar('@')); btn3->setText(QChar('#')); btn4->setText(QChar('$')); btn5->setText(QChar('%')); btn6->setText(QChar('^')); btn7->setText("&&"); btn8->setText(QChar('*')); btn9->setText(QChar('(')); btn0->setText(QChar(')')); btnHiphen->setText(QChar('_')); btnAssign->setText(QChar('+')); btnStartSquare->setText(QChar('{')); btnCloseSquare->setText(QChar('}')); btnFwdSlash->setText(QChar('|')); btnSemiColon->setText(QChar(':')); btnSp->setText(QChar('"')); btnComma->setText(QChar('<')); btnPeriod->setText(QChar('>')); btnBcwdSlash->setText(QChar('?'));} else {btnTilt->setText(QChar('`')); btn1->setText(QChar('1')); btn2->setText(QChar('2')); btn3->setText(QChar('3')); btn4->setText(QChar('4')); btn5->setText(QChar('5')); btn6->setText(QChar('6')); btn7->setText(QChar('7')); btn8->setText(QChar('8')); btn9->setText(QChar('9')); btn0->setText(QChar('0')); btnHiphen->setText(QChar('-')); btnAssign->setText(QChar('=')); btnStartSquare->setText(QChar('[')); btnCloseSquare->setText(QChar(']')); btnFwdSlash->setText(QChar('\\')); btnSemiColon->setText(QChar(';')); btnSp->setText(QChar('\'')); btnComma->setText(QChar(',')); btnPeriod->setText(QChar('.')); btnBcwdSlash->setText(QChar('/')); }}void WidgetKeyboard::changeTextCaps(bool isCaps){ if (isCaps) { btnQ->setText(QChar('Q')); btnW->setText(QChar('W')); btnE->setText(QChar('E')); btnR->setText(QChar('R')); btnT->setText(QChar('T')); btnY->setText(QChar('Y')); btnU->setText(QChar('U')); btnI->setText(QChar('I')); btnO->setText(QChar('O')); btnP->setText(QChar('P')); btnA->setText(QChar('A')); btnS->setText(QChar('S')); btnD->setText(QChar('D')); btnF->setText(QChar('F')); btnG->setText(QChar('G')); btnH->setText(QChar('H')); btnJ->setText(QChar('J')); btnK->setText(QChar('K')); btnL->setText(QChar('L')); btnZ->setText(QChar('Z')); btnX->setText(QChar('X')); btnC->setText(QChar('C')); btnV->setText(QChar('V')); btnB->setText(QChar('B')); btnN->setText(QChar('N')); btnM->setText(QChar('M')); }else { btnQ->setText(QChar('q')); btnW->setText(QChar('w')); btnE->setText(QChar('e')); btnR->setText(QChar('r')); btnT->setText(QChar('t')); btnY->setText(QChar('y')); btnU->setText(QChar('u')); btnI->setText(QChar('i')); btnO->setText(QChar('o')); btnP->setText(QChar('p')); btnA->setText(QChar('a')); btnS->setText(QChar('s')); btnD->setText(QChar('d')); btnF->setText(QChar('f')); btnG->setText(QChar('g')); btnH->setText(QChar('h')); btnJ->setText(QChar('j')); btnK->setText(QChar('k')); btnL->setText(QChar('l')); btnZ->setText(QChar('z')); btnX->setText(QChar('x')); btnC->setText(QChar('c')); btnV->setText(QChar('v')); btnB->setText(QChar('b')); btnN->setText(QChar('n')); btnM->setText(QChar('m')); }}bool WidgetKeyboard::checkNotTextKey(int keyId){if (keyId==Qt::Key_Shift || keyId==Qt::Key_Control || keyId==Qt::Key_Tab || keyId==Qt::Key_Escape || keyId==Qt::Key_Return || keyId==Qt::Key_Insert || keyId==Qt::Key_NumLock || keyId==Qt::Key_F1 || keyId==Qt::Key_F2 || keyId==Qt::Key_F3 || keyId==Qt::Key_F4 || keyId==Qt::Key_F5 || keyId==Qt::Key_F6 || keyId==Qt::Key_F7 || keyId==Qt::Key_F8 || keyId==Qt::Key_F9 || keyId==Qt::Key_F10 || keyId==Qt::Key_F11 || keyId==Qt::Key_F12 || keyId==Qt::Key_Print || keyId==Qt::Key_Pause || keyId==Qt::Key_ScrollLock || keyId==Qt::Key_Enter || keyId==Qt::Key_Home || keyId==Qt::Key_End || keyId==Qt::Key_CapsLock || keyId==Qt::Key_Insert || keyId==Qt::Key_Delete || keyId==Qt::Key_PageUp || keyId==Qt::Key_PageDown || keyId==Qt::Key_Down || keyId==Qt::Key_Up || keyId==Qt::Key_Left || keyId==Qt::Key_Right|| keyId==Qt::Key_Alt) {return true;}elsereturn false;}void WidgetKeyboard::setOpacity(int opacity){ sliderOpacity->setValue(opacity);}int WidgetKeyboard::getOpacity(){ return sliderOpacity->value();}void WidgetKeyboard::setSoundDisabled(){soundFilePath = "";}/* Microsoft Windows : The underlying multimedia system is used; only WAVE format sound files are supported.X11: The Network Audio System is used if available, otherwise all operations work silently. NAS supports WAVE and AU files.Mac OS X : NSSound is used. All formats that NSSound supports, including QuickTime formats, are supported by Qt for Mac OS X.Qt for Embedded Linux : A built-in mixing sound server is used, accessing /dev/dsp directly. Only the WAVE format is supported.*/void WidgetKeyboard::setSoundEnabled(QString soundPath){soundFilePath = soundPath;}void WidgetKeyboard::on_checkBoxShowNumPad_stateChanged(int state){QList<QWidget *> children = frameNumPad->findChildren<QWidget *>();if(state==Qt::Checked){frameNumPad->show();/*for each(QWidget *child in children)child->show();*/}else{frameNumPad->hide();/*for each(QWidget *child in children)child->hide();*/}update();}
阅读全文
0 0
- QT控件大全 三十八 QWidgetKeyboard
- Qt编写自定义控件大全
- QT控件大全一AnalogWidgets
- QT控件大全二VirtualKeyboard
- QT控件大全三QLed
- QT控件大全四QMeter
- QT控件大全五QProgressIndicator
- QT控件大全 六 QVUMeter
- QT控件大全 七 QProg
- QT控件大全 八 QNeedleIndicator
- QT控件大全 九 Qnightcharts
- QT控件大全 十 QPower
- QT控件大全 十一 QLock
- QT控件大全 十二 QScale
- QT控件大全 十三 WidgetMarqueeLabel
- QT控件大全 十四 QProgressmeter
- QT控件大全 十五 QPot
- QT控件大全 十五 QOpenChart
- angular 简单的增删查
- qt 使用QCamera 实现简单的摄像头使用
- 自己写的正则
- [上下界有源汇最小费用可行流] BZOJ 3876: [Ahoi2014&Jsoi2014]支线剧情
- 深入理解JQuery中$.get()、$.post()、$.getJSON()、$.ajax()方法
- QT控件大全 三十八 QWidgetKeyboard
- YCSB--HBase性能测试工具的安装和使用
- PostgreSQL HA双机热备份配置
- python 网络 select
- 740. Delete and Earn
- 关于素数的埃氏筛法
- Container:容器,对象生命周期管理的基石——学习总结篇
- springboot 打包部署 共享依赖包(分布式开发集中式部署微服务)
- IntelliJ下gradle location is incorrect问题