使用qt写一个软键盘

软键盘通过将多个ToolButton放在一个Widget当中,并将按钮提升为自定义类softKey继承于QToolButton类。


步骤:
1、为需要使用到软键盘的控件添加事件过滤器

//添加事件过滤器
ui->account_lineEdit->installEventFilter(this);
ui->password_lineEdit->installEventFilter(this);

//先隐藏软键盘
ui->widget->hide();


2、重写eventFilter事件,判断当前触发对象是否是添加了过滤器的控件,且事件是否是鼠标按钮点击事件。是的话,显示软键盘,并将焦点设置到当前控件上。

bool MainWindow::eventFilter(QObject *obj,QEvent *event)
{
    if(obj == ui->account_lineEdit && event->type() == QEvent::MouseButtonPress)
    {
        ui->widget->show();			//显示软键盘
        ui->account_lineEdit->setFocus();		//设置焦点
    }
    else if(obj == ui->password_lineEdit && event->type() == QEvent::MouseButtonPress)
    {
        ui->widget->show();
        ui->password_lineEdit->setFocus();
    }

    return QMainWindow::eventFilter(obj,event);
}


3、若是当前控件编辑完毕则隐藏软键盘

//账号输入完毕
void MainWindow::on_account_lineEdit_editingFinished()
{
    ui->widget->hide();
}

//密码输入完毕
void MainWindow::on_password_lineEdit_editingFinished()
{
    ui->widget->hide();
}


4、在自定义按钮类softKey的槽函数中判断当前点击的按钮文本,定义一个按键事件,通过postEvent将事件key发送出去

#include "softkey.h"

softKey::softKey(QWidget *parent):QToolButton(parent)
{
    //为按钮设置统一样式
    QFile qssFile(":/qss/qss.qss");         
    if(qssFile.open(QIODevice::ReadOnly))
        qApp->setStyleSheet(qssFile.readAll());

    connect(this,SIGNAL(clicked(bool)),this,SLOT(echoClicked(bool)));
}

void softKey::echoClicked(bool)
{
    if(this->text() == "backspace")
    {
        //定义一个事件,Key_Backspace撤消按钮
        QKeyEvent *key = new QKeyEvent(QEvent::KeyPress,Qt::Key_Backspace,Qt::NoModifier);

        //通过postEvent将事件key发送出去
        QApplication::postEvent(QApplication::focusWidget(),key);
    }
    else if(this->text() == "enter")
    {
        QKeyEvent *key = new QKeyEvent(QEvent::KeyPress,Qt::Key_Enter,Qt::NoModifier);

        QApplication::postEvent(QApplication::focusWidget(),key);
    }
    else if(this->text().isEmpty())
    {
        QKeyEvent *key = new QKeyEvent(QEvent::KeyPress,Qt::Key_Space,Qt::NoModifier);

        QApplication::postEvent(QApplication::focusWidget(),key);
    }
    else if(this->text() == "shift")
    {
        QKeyEvent *key = new QKeyEvent(QEvent::KeyPress,Qt::Key_Shift,Qt::NoModifier);

        QApplication::postEvent(QApplication::focusWidget(),key);
    }
    else if(this->text() == "ctrl")
    {
        QKeyEvent *key = new QKeyEvent(QEvent::KeyPress,Qt::Key_Control,Qt::NoModifier);

        QApplication::postEvent(QApplication::focusWidget(),key);
    }
    else if(this->text() == "tab")
    {
        QKeyEvent *key = new QKeyEvent(QEvent::KeyPress,Qt::Key_Tab,Qt::NoModifier);

        QApplication::postEvent(QApplication::focusWidget(),key);
    }
    else if(this->text() == "alt")
    {
        QKeyEvent *key = new QKeyEvent(QEvent::KeyPress,Qt::Key_Alt,Qt::NoModifier);

        QApplication::postEvent(QApplication::focusWidget(),key);
    }
    else
    {
        QKeyEvent *key = new QKeyEvent(QEvent::KeyPress,'w',Qt::NoModifier,this->text());

        //通过postEvent将事件key的文本内容发送出去,QApplication::focusWidget()当前窗口中,有光标的控件上
        QApplication::postEvent(QApplication::focusWidget(),key);
    }
}


效果图:

(ctrl键、alt键、空格键、shift键功能未实现)

你可能感兴趣的:(qt)