先上效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/f10316f74c8c46489f5ff37bbb2ca56d.png#pic_center)
#include <QApplication>
#include "./mainpage.h"
#include "./keyboard.h"
int main(int argc, char **args)
{
QApplication *app = new QApplication(argc, args);
MainPage *mainpage = new MainPage();
app->installEventFilter(mainpage);
mainpage->show();
return app->exec();
}
#include "mainpage.h"
#include "ui_mainpage.h"
MainPage::MainPage(QWidget *parent)
: QWidget(parent), ui(new Ui::MainPage)
{
ui->setupUi(this);
ui->spinBox_1->setRange(0, 9999999);
ui->spinBox_2->setRange(0, 9999999);
connect(ui->pushButton_openKeyboard, &QPushButton::clicked, this,
[=]()
{
m_keyboard.show();
});
}
MainPage::~MainPage()
{
delete ui;
}
bool MainPage::eventFilter(QObject *obj, QEvent *e)
{
if (e->type() == QEvent::FocusIn)
{
if (obj == ui->spinBox_1)
{
SELECTED_SPINBOX = ui->spinBox_1;
printf("1\n");
}
else if (obj == ui->spinBox_2)
{
SELECTED_SPINBOX = ui->spinBox_2;
printf("2\n");
}
}
return QObject::eventFilter(obj, e);
}
#ifndef MAINPAGE_H
#define MAINPAGE_H
#include <QWidget>
#include "./keyboard.h"
namespace Ui
{
class MainPage;
}
class MainPage : public QWidget
{
Q_OBJECT
public:
explicit MainPage(QWidget *parent = nullptr);
~MainPage();
bool eventFilter(QObject *obj, QEvent *e) override;
private:
Ui::MainPage *ui;
Keyboard m_keyboard;
};
#endif
#include "keyboard.h"
#include "ui_keyboard.h"
QSpinBox *SELECTED_SPINBOX;
Keyboard::Keyboard(QWidget *parent)
: QWidget(parent), ui(new Ui::Keyboard)
{
ui->setupUi(this);
this->setWindowFlag(Qt::WindowStaysOnTopHint);
connect(ui->pushButton_0, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_1, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_2, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_3, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_4, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_5, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_6, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_7, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_8, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_9, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_point, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_backSpace, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
connect(ui->pushButton_confirm, &QPushButton::clicked, this, &Keyboard::ButtonClickedHandler);
}
Keyboard::~Keyboard()
{
delete ui;
}
void Keyboard::ButtonClickedHandler()
{
QString senderName = ((QPushButton *)sender())->text();
if (SELECTED_SPINBOX == NULL)
return;
if (senderName == "退格")
{
int value = SELECTED_SPINBOX->value();
value /= 10;
SELECTED_SPINBOX->setValue(value);
}
else if (senderName == "确定")
{
this->close();
}
else if (senderName == ".")
{
}
else
{
int value = SELECTED_SPINBOX->value();
value *= 10;
value += senderName.toInt();
SELECTED_SPINBOX->setValue(value);
}
}
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include <QWidget>
#include <QSpinBox>
extern QSpinBox* SELECTED_SPINBOX;
namespace Ui {
class Keyboard;
}
class Keyboard : public QWidget
{
Q_OBJECT
public:
explicit Keyboard(QWidget *parent = nullptr);
~Keyboard();
void ButtonClickedHandler();
private:
Ui::Keyboard *ui;
};
#endif