软键盘输入

1、从QInputContext派生自己的InputContext类 ,例如:

    class MyInputPanelContext : public QInputContext
    {
        Q_OBJECT

    public:
        MyInputPanelContext();
        ~MyInputPanelContext();

 

        /* 事件过滤器 */

        bool filterEvent(const QEvent* event);

 

        QString identifierName();
        QString language();

        bool isComposing() const;

        void reset();

 

    private slots:

        /* 槽用于接收从输入面板输入的字符 */
        void sendCharacter(QChar character);

 

    private:
        void updatePosition();

 

    private:

        /* 关联的输入面板 */
        MyInputPanel *inputPanel;
    };

 

 

 

    MyInputPanelContext::MyInputPanelContext()
   {

        /* 创建和输入法关联的面板 */
        inputPanel = new MyInputPanel;

 

        /* 并将characterGenerated和sendCharacter关联,当用户按键后,则继续由sendCharacter向目标Widget */
        connect(inputPanel, SIGNAL(characterGenerated(QChar)), SLOT(sendCharacter(QChar)));
    }

 

    bool MyInputPanelContext::filterEvent(const QEvent* event)
    {
        if (event->type() == QEvent::RequestSoftwareInputPanel)

        {

            /* 当某个Widget请求软键盘输入时,显示软键盘 */
            updatePosition();
            inputPanel->show();
            return true;
        }

        else if (event->type() == QEvent::CloseSoftwareInputPanel)

        {

            /* 当某个Widget请求关闭软键盘输入时,关闭软键盘 */
           inputPanel->hide();
          return true;
        }
        return false;
    }

 

    void MyInputPanelContext::sendCharacter(QChar character)
    {
        QPointer w = focusWidget();

        if (!w)
            return;

 

        /* 当收到按键面板的按键输入后,分别向当前焦点Widget发送KeyPress和KeyRelease事件 */

        QKeyEvent keyPress(QEvent::KeyPress, character.unicode(), Qt::NoModifier, QString(character));
        QApplication::sendEvent(w, &keyPress);

        if (!w)
            return;

        QKeyEvent keyRelease(QEvent::KeyPress, character.unicode(), Qt::NoModifier, QString());
        QApplication::sendEvent(w, &keyRelease);
    }

 

    /* 根据当前焦点Widget的位置,调整输入面板的位置 */

    void MyInputPanelContext::updatePosition()
    {
        QWidget *widget = focusWidget();
        if (!widget)
            return;

        QRect widgetRect = widget->rect();
        QPoint panelPos = QPoint(widgetRect.left(), widgetRect.bottom() + 2);
        panelPos = widget->mapToGlobal(panelPos);
        inputPanel->move(panelPos);
    }

 

 

 

2、输入面板

    图示:

   

 

    /* 每个按钮具有一个buttonValue动态属性,表明和该按钮相关的字符,当按钮点击时,将此字符作为信号参数发出 */

    /* 前面的InputContext关联了此信号,并进一步发送给了目标Widget */

    void MyInputPanel::buttonClicked(QWidget *w)
    {
        QChar chr = qvariant_cast(w->property("buttonValue"));
        emit characterGenerated(chr);
    }

 

 

 

3、使用InputContext

     在应用程序main处,通过setInputContext将InputContext和Application关联,入:

    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);

        MyInputPanelContext *ic = new MyInputPanelContext;

        /* InputContext和Application关联 */

        app.setInputContext(ic);

 

        /* 主窗口 */

        QWidget widget;
        Ui::MainForm form;
        form.setupUi(&widget);
        widget.show();
  

       return app.exec();
}

你可能感兴趣的:(QT)