commit a0ceffcd90c7403d9305feb0dfdcd930c5fe4dc2 Author: tianzhendong <1203886034@qq.com> Date: Tue Dec 26 16:37:21 2023 +0800 v1.0.0 diff --git a/NumKeyBoard.pri b/NumKeyBoard.pri new file mode 100644 index 0000000..a784098 --- /dev/null +++ b/NumKeyBoard.pri @@ -0,0 +1,7 @@ +INCLUDEPATH += $$PWD/ + +HEADERS += $$PWD/numkeydia.h + +SOURCES += $$PWD/numkeydia.cpp + +FORMS += $$PWD/numkeydia.ui diff --git a/NumKeyBoard.pro b/NumKeyBoard.pro new file mode 100644 index 0000000..139d56a --- /dev/null +++ b/NumKeyBoard.pro @@ -0,0 +1,25 @@ +QT += core gui widgets + +TEMPLATE = lib + +CONFIG += c++17 + +INCLUDEPATH += $$PWD/ + +DESTDIR = $$PWD/../../Build/Libs +MOC_DIR = $$PWD/Build/moc +OBJECTS_DIR = $$PWD/Build/objs +RCC_DIR = $$PWD/Build/resources +UI_DIR = $$PWD/Build/ui + +HEADERS += $$PWD/numkeydia.h + +SOURCES += $$PWD/numkeydia.cpp + +FORMS += $$PWD/numkeydia.ui + +# Default rules for deployment. +unix { + target.path = /usr/lib +} +!isEmpty(target.path): INSTALLS += target diff --git a/README.md b/README.md new file mode 100644 index 0000000..a8c0f9e --- /dev/null +++ b/README.md @@ -0,0 +1,66 @@ +# 数字软键盘 + +## 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(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(); + 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")); +``` + diff --git a/numkeydia.cpp b/numkeydia.cpp new file mode 100644 index 0000000..16035d6 --- /dev/null +++ b/numkeydia.cpp @@ -0,0 +1,238 @@ +#include "numkeydia.h" +#include "ui_numkeydia.h" +#include +#include +#include + +NumKeyDia::NumKeyDia(QWidget *parent) : + QDialog(parent), + ui(new Ui::NumKeyDia) +{ + ui->setupUi(this); + this->setWindowFlag(Qt::FramelessWindowHint); + ui->lineEdit_input->setCursorPosition(Qt::StrongFocus); + auto _btnInputList = ui->widget_input->findChildren(); + foreach (auto &i, _btnInputList) + { + connect(i, &QPushButton::clicked, this, &NumKeyDia::btn_input_clicked); + i->setFocusPolicy(Qt::NoFocus); + } + ui->toolButton_ico->installEventFilter(this); + ui->label_appInfo->installEventFilter(this); +} + +NumKeyDia::~NumKeyDia() +{ + delete ui; +} + +bool NumKeyDia::eventFilter(QObject *, QEvent *event) +{ + if(event->type() == QEvent::MouseButtonDblClick) + { + if(!m_bMaxFlag) + { + this->showFullScreen(); + m_bMaxFlag = true; + } + else + { + this->showNormal(); + m_bMaxFlag = false; + } + return true; + } + else if(event->type() == QEvent::MouseButtonPress) + { + QMouseEvent *mouseEvent = static_cast(event); + m_point = mouseEvent->globalPos() - frameGeometry().topLeft(); + //鼠标位置减去左上角的左边 + } + else if(event->type() == QEvent::MouseMove) + { + QMouseEvent *mouseEvent = static_cast(event); + move(mouseEvent->globalPos() - m_point); + } + return false; +} + +QLineEdit *NumKeyDia::pLineEdit() const +{ + return m_pLineEdit; +} + +void NumKeyDia::setPLineEdit(QLineEdit *newPLineEdit) +{ + m_pLineEdit = newPLineEdit; + QString _text = m_pLineEdit->text(); +// int _cnt = _text.count(); +// for(int i = 0; i < _cnt; ++i) +// { +// if(_text.at(0) != "1" && _text.at(0) != "2" && _text.at(0) != "3" && _text.at(0) != "4" && +// _text.at(0) != "5" && _text.at(0) != "6" && _text.at(0) != "7" && _text.at(0) != "8" && _text.at(0) != "9") +// { +// _text.remove(0, 1); +// } +// } + ui->lineEdit_input->setFocus(); + ui->lineEdit_input->setText(_text); + newPLineEdit->setEnabled(false); + + m_firstInputFlag = true; + + ui->label_3->setText(m_pLineEdit->property("Name").toString() + ": "); + + //最大值和最小值显示 + m_isNum = m_pLineEdit->property("IsNum").toBool(); + if(!m_isNum) + { + ui->label_min->setText(tr("no limit")); + ui->label_max->setText(tr("no limit")); + return; + } + + m_minValue = m_pLineEdit->property("Min").toDouble(); + m_maxValue = m_pLineEdit->property("Max").toDouble(); + + m_isDouble = m_pLineEdit->property("IsDouble").toBool(); + + if(m_minValue == m_maxValue) + { + ui->label_min->setText(tr("no limit")); + ui->label_max->setText(tr("no limit")); + } + else + { + if(m_isDouble) + { + ui->label_max->setText(QString::number(m_maxValue, 10, 3)); + ui->label_min->setText(QString::number(m_minValue, 10, 3)); + } + else + { + ui->label_max->setText(QString::number(m_maxValue, 10, 0)); + ui->label_min->setText(QString::number(m_minValue, 10, 0)); + } + } +} + +void NumKeyDia::on_btn_ok_clicked() +{ + if(!m_pLineEdit) + return; + + //如果输入为空,那么设为0 + QString _res = ui->lineEdit_input->text(); + if(_res == "") + _res = "0"; + + //不是数字,直接设置后返回 + if(!m_isNum) + { + m_pLineEdit->setText(_res); + } + else + { + bool ok; + double _d = ui->lineEdit_input->text().toDouble(&ok); + + if(!ok) + { + QMessageBox::critical(this, "error", "Input is not a number!\nPlease reInput"); + return; + } + + bool _isNotValid = false; + if(m_minValue < m_maxValue) + { +// _d = (_d < m_minValue) ? m_minValue : _d; +// _d = (_d > m_maxValue) ? m_maxValue : _d; + if(_d < m_minValue || _d > m_maxValue) + _isNotValid = true; + } + if(_isNotValid) + { + QMessageBox::critical(this, "error", "Input is not valid!\nPlease reInput"); + return; + } + + if(m_isDouble) + m_pLineEdit->setText(QString::number(_d, 10, 3)); + else + m_pLineEdit->setText(QString::number(_d, 10, 0)); + } + + closeKeyBoard(); +} + + +void NumKeyDia::on_btn_cancel_clicked() +{ + //取消,关闭数字键盘 + closeKeyBoard(); +} + + +void NumKeyDia::on_btn_clear_clicked() +{ + //清空 + ui->lineEdit_input->clear(); + ui->lineEdit_input->setFocus(); +} + + +void NumKeyDia::on_btn_back_clicked() +{ + m_firstInputFlag = false; + + int _index = ui->lineEdit_input->cursorPosition(); + QString _currentText = ui->lineEdit_input->text(); + + if(_currentText == "") + ui->lineEdit_input->setFocus(); + + //当前光标在最前面,不做任何处理 + if(_index != 0) + { + //删除前一个字符 + _currentText.remove(_index - 1, 1); + ui->lineEdit_input->setText(_currentText); + } + + ui->lineEdit_input->setFocus(); + ui->lineEdit_input->setCursorPosition(_index - 1); +} + +void NumKeyDia::btn_input_clicked() +{ + if(m_firstInputFlag) + { + ui->lineEdit_input->clear(); + m_firstInputFlag = false; + } + auto _btnClicked = qobject_cast(sender()); + QString _inputValue = _btnClicked->text(); + + int _index = ui->lineEdit_input->cursorPosition(); + QString _currentText = ui->lineEdit_input->text(); + + _currentText.insert(_index, _inputValue); + ui->lineEdit_input->setText(_currentText); +} + +void NumKeyDia::closeKeyBoard() +{ + this->close(); + m_pLineEdit->setEnabled(true); +} + +void NumKeyDia::setMaxValue(double newMaxValue) +{ + m_maxValue = newMaxValue; +} + +void NumKeyDia::setMinValue(double newMinValue) +{ + m_minValue = newMinValue; +} + diff --git a/numkeydia.h b/numkeydia.h new file mode 100644 index 0000000..6b68700 --- /dev/null +++ b/numkeydia.h @@ -0,0 +1,61 @@ +#ifndef NUMKEYDIA_H +#define NUMKEYDIA_H + +#include +#include +#include + +namespace Ui +{ +class NumKeyDia; +} + +class Q_DECL_EXPORT NumKeyDia : public QDialog +{ + Q_OBJECT + +public: + explicit NumKeyDia(QWidget *parent = nullptr); + ~NumKeyDia(); + + QLineEdit *pLineEdit() const; + void setPLineEdit(QLineEdit *newPLineEdit); + + void setMinValue(double newMinValue); + + void setMaxValue(double newMaxValue); + +protected: + virtual bool eventFilter(QObject * obj, QEvent *event) override; + +private slots: + void on_btn_ok_clicked(); + + void on_btn_cancel_clicked(); + + void on_btn_clear_clicked(); + + void on_btn_back_clicked(); + + void btn_input_clicked(); + + void closeKeyBoard(); + +private: + Ui::NumKeyDia *ui; + + QLineEdit *m_pLineEdit = nullptr; + + bool m_bMaxFlag = false; + + QPoint m_point; + + bool m_firstInputFlag = true; + + double m_minValue = 0.000; + double m_maxValue = 0.000; + bool m_isNum = false; + bool m_isDouble = false; +}; + +#endif // NUMKEYDIA_H diff --git a/numkeydia.ui b/numkeydia.ui new file mode 100644 index 0000000..a893dec --- /dev/null +++ b/numkeydia.ui @@ -0,0 +1,752 @@ + + + NumKeyDia + + + + 0 + 0 + 532 + 422 + + + + Dialog + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Vertical + + + QSizePolicy::MinimumExpanding + + + + 10 + 0 + + + + + + + + Qt::Horizontal + + + QSizePolicy::MinimumExpanding + + + + 10 + 20 + + + + + + + + + + 0 + + + + + false + + + + 36 + 36 + + + + + 36 + 36 + + + + border-image: url(:/resources/140xfd.ico); + + + + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 13 + 20 + + + + + + + + + Times New Roman + 12 + true + + + + Numeric Keypad + + + + + + + + + + + + Times New Roman + 12 + true + + + + CurrentInput: + + + + + + + + 0 + 40 + + + + + 16777215 + 40 + + + + + + + + + + + + + Times New Roman + 12 + true + + + + Minimum: + + + + + + + + Times New Roman + 12 + true + + + + 0.000 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + Times New Roman + 12 + true + + + + Maximum: + + + + + + + + 0 + 40 + + + + + Times New Roman + 12 + true + + + + 1.000 + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 6 + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + 1 + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + 2 + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + 3 + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + 4 + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + 5 + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + 6 + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + 7 + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + 8 + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + 9 + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + - + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + 0 + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + . + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + + + + << + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + Ok + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + C + + + + + + + + 120 + 60 + + + + + 120 + 60 + + + + + Times New Roman + 12 + true + + + + Cancel + + + + + + + + + + + + + + + Qt::Horizontal + + + QSizePolicy::MinimumExpanding + + + + 10 + 20 + + + + + + + + Qt::Vertical + + + QSizePolicy::MinimumExpanding + + + + 10 + 10 + + + + + + + + +