上次本来想着直接在页面中拖几个键做键盘用,简单又好用,后来发现复用性太低,而且只能对其中一个lineEdit进行数字输入,太死板,还是得用软件盘,一点输入框就出来的那种。还是根据嵌入式QT软键盘那个程序改了一下,只留下了数字键。
1.写一个My_lineEdit的类,继承QLineEdit,重写鼠标点击事件,点击输入框可以发送一个send_show信号,以便于主界面接收显示软键盘。程序如下:
#ifndef MY_LINEEDIT_H
#define MY_LINEEDIT_H
#include <QWidget>
#include <QLineEdit>
class My_lineEdit : public QLineEdit
{
Q_OBJECT
public:
My_lineEdit(QWidget *parent = 0);
protected:
void mousePressEvent(QMouseEvent *event);
signals:
void send_show(QString data);
};
#endif // MY_LINEEDIT_H
#include "my_lineedit.h"
My_lineEdit::My_lineEdit(QWidget *parent) :
QLineEdit(parent)
{
this->setText("521");
}
void My_lineEdit::mousePressEvent(QMouseEvent *event)
{
emit send_show(this->text());
QLineEdit::mousePressEvent(event);
}
2.写一个包含界面ui文件的键盘类,在键盘上按顺序拖入需要的键,和输入框lineEdit,注意一定要按顺序从左到右,从上到下,这样才能保证按键序列相对应,拖完按键可以点击Tab键看看焦点是不是按顺序移动。这是为键盘响应按钮点击做准备。
#ifndef MYKEYDIALOG_H
#define MYKEYDIALOG_H
#include <QDialog>
#include "ui_mykeydialog.h"
#include <QKeyEvent>
class MyKeyDialog : public QDialog,public Ui::MyKeyDialog
{
Q_OBJECT
public:
MyKeyDialog();
~MyKeyDialog();
void affirmString();
void deleteString();
void initGb();
public slots:
void buttonClickResponse(int gemfield);
signals:
void sendMessage(QString gemfield);
private:
Ui::MyKeyDialog *ui;
QKeyEvent *event;
QButtonGroup *button_group;
QVector<QPushButton*> button_vector;
};
#endif // MYKEYDIALOG_H
#include "mykeydialog.h"
QString number[12]={"0","1","2","3","4","5","6","7","8","9","."};
MyKeyDialog::MyKeyDialog() :
QDialog(0,Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint),button_group(new QButtonGroup(this))
{
setupUi(this);
initGb ();
}
MyKeyDialog::~MyKeyDialog()
{
//delete ui;
}
void MyKeyDialog::initGb()
{
QPushButton *pushButton=new QPushButton(this); //创建一个按钮对象
pushButton->hide(); //按钮隐藏起来
pushButton=pushButton_ok;
for(int i=1;i<14;i++)
{
pushButton->setAutoFillBackground(true);
pushButton->setFocusPolicy(Qt::NoFocus);
button_vector.push_back(pushButton); //按钮集合,push_back()往按钮集合中添加一个按钮
button_group->addButton(pushButton,i); //添加一个按钮到button_group中,并且给按钮编号
pushButton=qobject_cast<QPushButton *>(pushButton->nextInFocusChain()); //焦点指向下一个button
}
connect(button_group,SIGNAL(buttonClicked(int)),SLOT(buttonClickResponse(int)));
}
void MyKeyDialog::buttonClickResponse(int gemfield)
{
if(gemfield==1)
{
affirmString ();
return;
}
else if(gemfield==13)
{
deleteString ();
return;
}
else
{
event=new QKeyEvent(QEvent::KeyPress, 0, Qt::NoModifier,number[gemfield-2]);
}
lineEdit_window->setFocus();
QApplication::sendEvent(focusWidget(),event);
}
void MyKeyDialog::affirmString()
{
emit sendMessage(lineEdit_window->text()); //发送信号给界面
this->hide();
}
void MyKeyDialog::deleteString ()
{
event=new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier); //新建一个键盘事件
lineEdit_window->setFocus();
QApplication::sendEvent(focusWidget(),event);
}
3.添加一个Qt Designer Form Class ,在ui界面上拖入一个lineEdit部件(用程序在界面中也添加了一个lineedit部件),然后提升为My_lineEdit类,在头文件中包含mylineedit.h文件,原本的程序仅对一个部件设置了软件盘事件,添加lineEdit后输入数据总是在第一个lineedit那里,我稍作修改,添加了一个判断sender()的命令,来判断软件盘的调用者。
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <my_lineedit.h>
#include "mykeydialog.h"
#include <QKeyEvent>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
public slots:
void show_numberkeyboard_ui(QString data); //显示键盘
void confirmString(QString gemfield); //接收键盘发过来的数据
private:
Ui::Widget *ui;
QLineEdit *m_edit;
MyKeyDialog *numberkeyboard;
QKeyEvent *event;
QString lineEdit_object;//判断哪个lineedit调用了软件盘
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_edit = new My_lineEdit(this);
m_edit->setObjectName("lineEdit0");
m_edit->setGeometry(10,10,200,20);
numberkeyboard = new MyKeyDialog();
//将所有lineedit信号都和软件盘界面显示关联
connect(m_edit,SIGNAL(send_show(QString)),this,SLOT(show_numberkeyboard_ui(QString)));
connect(ui->lineEdit1,SIGNAL(send_show(QString)),this,SLOT(show_numberkeyboard_ui(QString)));
connect(numberkeyboard,SIGNAL(sendMessage(QString)),this,SLOT(confirmString(QString)));
}
Widget::~Widget()
{
delete ui;
}
void Widget::show_numberkeyboard_ui(QString data){
lineEdit_object = sender()->objectName();//获得调用软件盘的部件的名称
numberkeyboard->lineEdit_window->setText(data);
//numberkeyboard->resize(400,300);
//numberkeyboard->move(200,170);
numberkeyboard->show();
}
void Widget::confirmString(QString gemfield){
//判断应该将信息写入哪一个输入框
if(lineEdit_object==ui->lineEdit1->objectName())
ui->lineEdit1->setText(gemfield);
else
m_edit->setText(gemfield);
}