最近项目做触屏界面,需要使用到数字软键盘。

参考了以下内容:

  1. Qt实战开发-数字软键盘:https://blog.csdn.net/Osean_li/article/details/60882890
  2. syszuxpinyin代码:https://github.com/CivilNet/syszuxpinyin

结合我自己项目的需要,优化了以下几点内容:

  1. 使用layout布局替换原有的坐标布局。
  2. 使用button group按钮组来传递slot,避免写重复了SLOT。
  3. 去掉font的定义,方便使用外部的QSS定义样式。
  4. 去掉了编辑的时候的判断,可以任意输入字符。

先上效果图:

这样基本能满足数字和小数点的输入了,如果需要其他字符的输入,就需要查找其他的软键盘方案了。

代码如下:

softkeylineedit.h

#ifndef SOFTKEYLINEEDIT_H
#define SOFTKEYLINEEDIT_H#include <QLineEdit>
#include <QMouseEvent>
#include "numkeyboard.h"class SoftKeyLineEdit : public QLineEdit
{Q_OBJECT
public:explicit SoftKeyLineEdit(QWidget *parent = 0);protected:void mousePressEvent(QMouseEvent *e);private:NumKeyboard *numkeyboard;signals:public slots:};#endif // SOFTKEYLINEEDIT_H

softkeylineedit.cpp

#include "softkeylineedit.h"SoftKeyLineEdit::SoftKeyLineEdit(QWidget *parent) :QLineEdit(parent)
{numkeyboard = new NumKeyboard(this);
}void SoftKeyLineEdit::mousePressEvent(QMouseEvent *e)
{if(e->button() == Qt::LeftButton){numkeyboard->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);numkeyboard->setStyleSheet("border:2px solid white;");numkeyboard->setText(this->text());  //当前的文本框的内容,this->指向的是SoftKeyLineEdit        numkeyboard->exec();if(numkeyboard->valid){this->setText(numkeyboard->getText());  //此处的setText是继承QLineEdit的,numkeyboard->setTex是自定义的,目的是获取当前的文本}}
}

numkeyboard.h

#ifndef NUMKEYBOARD_H
#define NUMKEYBOARD_H#include <QDialog>
#include <QtDebug>
#include <QKeyEvent>
#include <QEvent>
#include <QLineEdit>
#include <QPushButton>class NumKeyboard : public QDialog
{Q_OBJECTpublic:explicit NumKeyboard(QWidget *parent = 0);~NumKeyboard();bool valid;void setText(QString str);QString getText();QPushButton *num6Button;QPushButton *backspaceButton;QPushButton *num4Button;QPushButton *okButton;QPushButton *leftButton;QPushButton *num1Button;QPushButton *cancelButton;QPushButton *rightButton;QPushButton *num9Button;QPushButton *num8Button;QPushButton *num2Button;QPushButton *num7Button;QPushButton *dotButton;QPushButton *num3Button;QPushButton *num0Button;QPushButton *num5Button;QPushButton *signButton;QLineEdit *lineEdit;protected:void changeEvent(QEvent *e);
//    void keyPressEvent(QKeyEvent *e);bool eventFilter(QObject *obj, QEvent *event);private slots:void buttonClickResponse(int gemfield);private:QString strContent;
};#endif // NUMKEYBOARD_H

numkeyboard.h

#include "numkeyboard.h"
#include <QDebug>
#include <QGridLayout>
#include <QButtonGroup>QString st_letter[11]={"0","1","2","3","4","5","6","7","8","9","."};NumKeyboard::NumKeyboard(QWidget *parent) :QDialog(parent)
{QGridLayout *layout = new QGridLayout();QButtonGroup *button_group = new QButtonGroup(this);okButton = new QPushButton(this);okButton->setText("确定");button_group->addButton(okButton,15);layout->addWidget(okButton,4,3,1,2);backspaceButton = new QPushButton(this);backspaceButton->setText("<<");button_group->addButton(backspaceButton,12);layout->addWidget(backspaceButton,2,3,1,2);num6Button = new QPushButton(this);num6Button->setText("6");button_group->addButton(num6Button,6);layout->addWidget(num6Button,2,2,1,1);num4Button = new QPushButton(this);num4Button->setText("4");button_group->addButton(num4Button,4);layout->addWidget(num4Button,2,0,1,1);leftButton = new QPushButton(this);leftButton->setText("<-");button_group->addButton(leftButton,13);layout->addWidget(leftButton,3,3,1,1);num1Button = new QPushButton(this);num1Button->setText("1");button_group->addButton(num1Button,1);layout->addWidget(num1Button,1,0,1,1);cancelButton = new QPushButton(this);cancelButton->setText("取消");button_group->addButton(cancelButton,11);layout->addWidget(cancelButton,1,3,1,2);rightButton = new QPushButton(this);rightButton->setText("->");button_group->addButton(rightButton,14);layout->addWidget(rightButton,3,4,1,1);num9Button = new QPushButton(this);num9Button->setText("9");button_group->addButton(num9Button,9);layout->addWidget(num9Button,3,2,1,1);num8Button = new QPushButton(this);num8Button->setText("8");button_group->addButton(num8Button,8);layout->addWidget(num8Button,3,1,1,1);num2Button = new QPushButton(this);num2Button->setText("2");button_group->addButton(num2Button,2);layout->addWidget(num2Button,1,1,1,1);num7Button = new QPushButton(this);num7Button->setText("7");button_group->addButton(num7Button,7);layout->addWidget(num7Button,3,0,1,1);dotButton = new QPushButton(this);dotButton->setText(".");button_group->addButton(dotButton,10);layout->addWidget(dotButton,4,2,1,1);num3Button = new QPushButton(this);num3Button->setText("3");button_group->addButton(num3Button,3);layout->addWidget(num3Button,1,2,1,1);num0Button = new QPushButton(this);num0Button->setText("0");button_group->addButton(num0Button,0);layout->addWidget(num0Button,4,0,1,2);num5Button = new QPushButton(this);num5Button->setText("5");button_group->addButton(num5Button,5);layout->addWidget(num5Button,2,1,1,1);lineEdit = new QLineEdit(this);lineEdit->setText(QString());layout->addWidget(lineEdit,0,0,1,5);//连接button_group的点击信号,和本对象的buttonClickResponse函数,传递参数为按钮号connect(button_group,SIGNAL(buttonClicked(int)),SLOT(buttonClickResponse(int)));//为所有按钮控件添加objectName,提供给QSS样式使用QList<QPushButton*> btnList = this->findChildren<QPushButton*>();for(int i = 0; i < btnList.size(); i++){QPushButton* btn = btnList.at(i);btn->setObjectName("keyboard");}setLayout(layout);
}NumKeyboard::~NumKeyboard()
{}void NumKeyboard::changeEvent(QEvent *e)
{QDialog::changeEvent(e);switch (e->type()) {case QEvent::LanguageChange:break;default:break;}
}bool NumKeyboard::eventFilter(QObject *obj, QEvent *event)
{if (event->type() == QEvent::KeyPress){QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);if (obj == lineEdit){if(keyEvent->key() >= 0x20 && keyEvent->key()<= 0x0ff)  //屏蔽所有按键输入return true;elsereturn false;}else{return false;}}else{// standard event processingreturn QObject::eventFilter(obj, event);}
}//***********************按键*****************************//
void NumKeyboard::buttonClickResponse(int gemfield)        //
{if( gemfield >=0 && gemfield <= 10){int idx = lineEdit->cursorPosition();  //光标的位置索引//  字符串的存储有引用计数,当一个 QString 对象被复制为另一个 QString 对象时,它们实际上指向相同的存储空间,仅仅是增加一个引用计数strContent.insert(idx, st_letter[gemfield]);  //插入字符串str在给定的索引位置对这个字符串,并返回一个引用。 //数据存入strContentlineEdit->setText(strContent); //strContent中的内容显示在lineEditlineEdit->setCursorPosition(idx+1); //设置光标的位置 ??不设置好像也可以使用设定光标的位置lineEdit->setFocus();  //存在光标qDebug()<<"strContent"<<strContent;qDebug()<<"idx"<<idx;}else if( gemfield == 13)//<-{int idx = lineEdit->cursorPosition();if(idx == 0){lineEdit->setCursorPosition(idx);lineEdit->setFocus();return;}lineEdit->setCursorPosition(idx-1);lineEdit->setFocus();}else if( gemfield == 14)//->{int idx = lineEdit->cursorPosition();if(idx == strContent.length()) //返回此字符串的字符数{lineEdit->setCursorPosition(idx);lineEdit->setFocus();return;}lineEdit->setCursorPosition(idx+1);lineEdit->setFocus();}else if( gemfield == 12)//<<{int idx = lineEdit->cursorPosition();if(idx == 0){lineEdit->setCursorPosition(idx);lineEdit->setFocus();return;}strContent.remove(idx-1,1);lineEdit->setText(strContent);lineEdit->setCursorPosition(idx-1);lineEdit->setFocus();}else if( gemfield == 11)//ESC{this->close();valid = false;}else if( gemfield == 15)//OK{this->close();valid = true;}
}void NumKeyboard::setText(QString str)      //设置文本内容
{strContent = str;lineEdit->setText(strContent);
}QString NumKeyboard::getText()              //获取内容
{return strContent;
}

将上面四个文件添加到工程中编译。使用方法如下:

SoftKeyLineEdit *pEdit_test = new SoftKeyLineEdit();
pEdit_test->setText("123456");

相关QSS如下:

QPushButton#keyboard{        /* 控件最小宽度 */  width:50px;height:50px;   /* 字体 加粗 大小 字体种类  PS: qss不支持中文设置字体*/  font: bold 30px "Arial Black,Microsoft YaHei";      border-style: outset;border-width: 2px;border-color: white;color: rgb(255, 255, 255);   background-color: rgba(33, 131, 128, 255);
}QLineEdit#keyboard {/* 最小宽度 */min-width:200px; /* 边框样式 */border: 2px solid white;      border-style: inset;    /* 内边框:上下和左右的距离 */padding: 0 8px;       /* 背景色 */background: rgb(33, 131, 128);
}

Qt开发-数字软键盘相关推荐

  1. EMWIN数字软键盘设计

    最近学习stm32的emwin界面,编写了一个数字软键盘的功能,其中遇到了一些问题,也学到了一些经验.我想要实现的功能是在如图所示的界面下,点击文本框弹出软键盘,能够通过软键盘输入0到9和小数点,ta ...

  2. 基于MDKA5D31-EK_T70开发板的QT示例-demo06:软键盘

    By Mcuzone 硬件平台:MDKA5D31-EK_T70开发板  QT版本:4.8.5  简要说明:本示例主要演示QT下调用相应的API制作软键盘及其使用效果. 应用程序运行效果: 初始化后设置 ...

  3. QT 嵌入式 输入法 软键盘

    设计自己的输入法需要继承QWSInputMethod. 需要重新实现:     virtual void updateHandler ( int type );     //type值是枚举变量Upd ...

  4. android 支付数字键盘,微信小程序自定义数字键盘|仿支付宝、微信支付数字软键盘...

    (此图片来源于网络,如有侵权,请联系删除! ) 前几天有开发过一个html5仿支付宝.微信支付数字键盘,在某些特定场景下自定义数字键盘应用还是蛮广泛的,比如 小程序商城系统 需要零钱支付 ,会员卡支付 ...

  5. iOS开发之软键盘使用小技巧

    在iOS开发过程中,有时候需要弹出软键盘进行输入,有时候又需要在某些情况下隐藏软键盘,以提高用户体验.今天有几个关于软键盘的小技巧和大家分享. (1)只弹出数字键盘 有某些需求中,要求用户只能在Tex ...

  6. 案例分享:Qt中英文输入软键盘(支持Qt4、Qt5、触摸和键鼠混合输入等)

    若该文为原创文章,转载请注明原文出处 本文章博客地址:https://blog.csdn.net/qq21497936/article/details/111831179 长期持续带来更多项目与技术分 ...

  7. android开发隐藏软键盘,Android开发之完全隐藏软键盘的方法

    隐藏软键盘一直是我头痛的事情,没有找到一种真正能隐藏的方法.点击EditText的时候总是弹出软键盘.-----杯具 杯具(一): InputMethodManager im =(InputMetho ...

  8. android调用数字软键盘设置(EditText属性)

    在某些时候我们要设置EditText调用数字键盘,省去在中文键盘下去切换成数字键盘,怎么做比较合适呢? 答案很简单:EditText设置android:numeric="integer&qu ...

  9. Android开发设置软键盘收索键

    有时候为了布局美观,不会特意的去放置一个收索按钮,而是调用软件盘上的回车按钮作为收索.要实现把软键盘的回车按钮设置为收索按钮其实很简单,直接在Layout文件中,对EditText控件加上如下属性: ...

最新文章

  1. 小米手机qq不读取相册照片_小米手机越用越卡怎么办?
  2. python numpy.mean() axis参数使用方法【sum(axis=*)是求和,mean(axis=*)是求平均值】
  3. u-boot Makefile完全解读
  4. SpringMVC的请求-获得请求参数-请求参数类型
  5. Win11右键菜单变换Win10风格
  6. python入门学习的第三天
  7. HDOJ1106 排序
  8. 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(2)
  9. spring事物管理--声明式(AspectJ)注解实现 (推荐使用)
  10. HTML5的表单验证属性--pattern
  11. MySQL 批量添加
  12. Centos下面Eclipse打开文件闪退
  13. Chrome开发者工具插件
  14. SMART PLC PID仿真 (SMART PID仿真库使用说明)
  15. 量子纠缠计算机里的灵魂意识,量子纠缠理论告诉我们,灵魂真的存在于你我身体?...
  16. Java实现仿win10计算器
  17. 《Hello!树先生》简介
  18. 操作系统课程项目 OS project —— Pintos from Project 1 to Project 3
  19. 最满意的10款PC 软件分享
  20. 【Android源码面试宝典】MMKV从使用到原理分析(一)

热门文章

  1. python基础注意点(初学python笔记)②
  2. 每日linux——网络命令2
  3. 正点科技公司比赛-----桌面插件闹钟项目总结
  4. 喜报 | 来自Ifenxi与东方证券的双重肯定
  5. 使用腾达(Tenda)路由器部分网址突然打不开,怎么办?
  6. 计算机学硕305分能调剂到哪,考研必看|最全的历年国家线分析+调剂攻略都在这!...
  7. 使用JMH编写基准测试
  8. Hold Fast To Dreams紧紧抓住梦想(双语阅读)
  9. halcon机器视觉识别白底黑字点型二维码qrcode码
  10. 待就业六人组 团队团队展示