数字键盘
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.6 KiB

10 months ago
# 数字软键盘
## qmake作为pri使用
- 将文件夹复制到主工程Libs目录下
- 主工程pro文件中加入:
```qmake
include($$PWD/Libs/SoftNumKeyBoard/NumKeyBoard.pri)
```
- 主工程中重写eventFilter函数,实现点击lineedit时,调出小键盘
```cpp
protected:
virtual bool eventFilter(QObject * obj, QEvent *event) override;
```
```cpp
bool SettingWidget::eventFilter(QObject *obj, QEvent * event)
{
auto _obj = static_cast<QLineEdit *>(obj);
if(m_inputList.contains(_obj))
{
if(event->type() == QEvent::FocusIn)
{
if(!_keyBoard)
{
_keyBoard = new NumKeyDia(this);
}
//传入对应的QLineEdit
_keyBoard->setPLineEdit(_obj);
_keyBoard->exec();
//QLineEdit取消聚焦,可以不用
_obj->clearFocus();
return true;
}
}
return QObject::eventFilter(obj, event);
}
```
- 对应的控件安装事件过滤器
```cpp
m_inputList = this->findChildren<QLineEdit *>();
foreach (auto &i, m_inputList)
{
i->installEventFilter(this);
i->setFocusPolicy(Qt::ClickFocus);
}
```
- 控件设置属性
```cpp
//设置输入输出的最大值、最小值、名字和是否是数字
ui->lineEdit_energyA->setProperty("IsNum", true);
//double时,默认保留三位小数
ui->lineEdit_energyA->setProperty("IsDouble", true);
//输入的数不能超过最大值和最小值
ui->lineEdit_energyA->setProperty("Max", 1.000);
ui->lineEdit_energyA->setProperty("Min", 0.000);
ui->lineEdit_energyA->setProperty("Name", tr("PressureA"));
```