项目实战:Qt中英文输入软键盘(支持Qt4、Qt5、触摸和键鼠混合输入等)

若该文为原创文章,转载请注明原文出处
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/111831179
长期持续带来更多项目与技术分享,咨询请加QQ:21497936、微信:yangsir198808
红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点击传送门)

开发专栏:项目实战


需求

  1. 全屏软键盘;
  2. 输入英文;
  3. 输入中文;
  4. 支持触摸、键盘和输入混合输入;
  5. 目前有黑色系皮肤;
  6. Qt4和Qt5区分2个版本;


Demo:Qt5

  
  
  
  CSDN(粉丝免积分下载):https://download.csdn.net/download/qq21497936/13945327
  QQ群:1047134658(点击“文件”搜索“softKeyboard”,群内与博文同步更新)


Demo:Qt4

  移植到MCGS嵌入式工控机上
  
  


V1.0.0

  项目实战:Qt中英文输入软键盘(支持Qt4、Qt5、触摸和键鼠混合输入等)_第1张图片
  项目实战:Qt中英文输入软键盘(支持Qt4、Qt5、触摸和键鼠混合输入等)_第2张图片
  项目实战:Qt中英文输入软键盘(支持Qt4、Qt5、触摸和键鼠混合输入等)_第3张图片
  项目实战:Qt中英文输入软键盘(支持Qt4、Qt5、触摸和键鼠混合输入等)_第4张图片
  项目实战:Qt中英文输入软键盘(支持Qt4、Qt5、触摸和键鼠混合输入等)_第5张图片


Demo代码

  可视化的布局,以便于定制修改,所见即所得:
  项目实战:Qt中英文输入软键盘(支持Qt4、Qt5、触摸和键鼠混合输入等)_第6张图片
KeyBoardWidget.h

#ifndef KEYBOARDWIDGET_H
#define KEYBOARDWIDGET_H

#include 
#include 
#include 

namespace Ui {
class KeyBoardWidget;
}

class KeyBoardWidget : public QWidget
{
    Q_OBJECT
private:
    enum INPUT_MODE {
        INPUT_MODE_ZH = 0x01,
        INPUT_MODE_EN,
        INPUT_MODE_SYMB,
    };

public:
    explicit KeyBoardWidget(QWidget *parent = 0);
    ~KeyBoardWidget();

public:
    void clearChineseCache();
    void setKeyPressEvent(QKeyEvent *event);

signals:
    void signal_sendKeyToFocusItem(QString keyText);

protected:
    void updateChineseCharactersButtonState();
    void switchLanguage(int inputMode);
    void searchChineseCharacters(int currentpage);

protected slots:
    void slot_characterButtonsClicked();
    void slot_switchLanguage();
    void slot_backspace();
    void slot_blankspace();
    void slot_chineseCharacterSelected();
    void slot_chineseCharacterPrevious();
    void slot_chineseCharacterNext();
    void slot_enter();
    void slot_upper();
    void slot_symbols();
    void slot_emoji();
    void slot_hideKeyboard();

protected:
    void updateKeyboard();

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void showEvent(QShowEvent *event);
    void hideEvent(QHideEvent *event);

private:
    Ui::KeyBoardWidget *ui;

private:
    QList _listCharacterBtns;
    QList _listChineseCharactersBtns;
    INPUT_MODE _currentMode;
    INPUT_MODE _lastMode;
    bool _upperMode;
    int _symbolPage;

    QFont _font;

private:
    static QString _strKeyboradCharacters;
    static QString _strKeyBoradSymbols[];
};

#endif // KEYBOARDWIDGET_H

KeyBoardWidget.cpp

#include "KeyBoardWidget.h"
#include "ui_KeyBoardWidget.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

...

KeyBoardWidget::KeyBoardWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::KeyBoardWidget),
    _currentMode(INPUT_MODE_EN),
    _upperMode(false),
    _symbolPage(0)
{
    ui->setupUi(this);

    this->setFixedSize(qApp->desktop()->width(), 380);

    //设置主窗体样式
    setWindowFlags(windowFlags()
                   | Qt::Tool
                   | Qt::FramelessWindowHint
                   | Qt::WindowStaysOnTopHint
                   | Qt::WindowDoesNotAcceptFocus);

    //图标字体
    int fontId = QFontDatabase::addApplicationFont(":/font/res/FontAwesome.otf");
    QString fontName = QFontDatabase::applicationFontFamilies(fontId).at(0);
    _font.setFamily(fontName);
    _font.setPixelSize(24);

    // 中文汉字按键存入列表
    _listChineseCharactersBtns.append(ui->pushButton_r1);
    _listChineseCharactersBtns.append(ui->pushButton_r2);
    _listChineseCharactersBtns.append(ui->pushButton_r3);
    _listChineseCharactersBtns.append(ui->pushButton_r4);
    _listChineseCharactersBtns.append(ui->pushButton_r5);
    for(int index = 0; index < _listChineseCharactersBtns.size(); index++)
    {
        connect(_listChineseCharactersBtns.at(index), SIGNAL(clicked(bool)), this, SLOT(slot_chineseCharacterSelected()));
    }
    // 翻页按键
    ui->pushButton_prev->setFont(_font);
    ui->pushButton_prev->setText(QString(QChar(0xf0d9)));
    connect(ui->pushButton_prev, SIGNAL(clicked(bool)), this, SLOT(slot_chineseCharacterPrevious()));
    ui->pushButton_next->setFont(_font);
    ui->pushButton_next->setText(QString(QChar(0xf0da)));
    connect(ui->pushButton_next, SIGNAL(clicked(bool)), this, SLOT(slot_chineseCharacterNext()));
    // 字符按键存入列表
    _listCharacterBtns.append(ui->pushButton_c1);
    _listCharacterBtns.append(ui->pushButton_c2);
    _listCharacterBtns.append(ui->pushButton_c3);
    _listCharacterBtns.append(ui->pushButton_c4);
    _listCharacterBtns.append(ui->pushButton_c5);
    _listCharacterBtns.append(ui->pushButton_c6);
    _listCharacterBtns.append(ui->pushButton_c7);
    _listCharacterBtns.append(ui->pushButton_c8);
    _listCharacterBtns.append(ui->pushButton_c9);
    _listCharacterBtns.append(ui->pushButton_c10);
    _listCharacterBtns.append(ui->pushButton_c11);
    _listCharacterBtns.append(ui->pushButton_c12);
    _listCharacterBtns.append(ui->pushButton_c13);
    _listCharacterBtns.append(ui->pushButton_c14);
    _listCharacterBtns.append(ui->pushButton_c15);
    _listCharacterBtns.append(ui->pushButton_c16);
    _listCharacterBtns.append(ui->pushButton_c17);
    _listCharacterBtns.append(ui->pushButton_c18);
    _listCharacterBtns.append(ui->pushButton_c19);
    _listCharacterBtns.append(ui->pushButton_c20);
    _listCharacterBtns.append(ui->pushButton_c21);
    _listCharacterBtns.append(ui->pushButton_c22);
    _listCharacterBtns.append(ui->pushButton_c23);
    _listCharacterBtns.append(ui->pushButton_c24);
    _listCharacterBtns.append(ui->pushButton_c25);
    _listCharacterBtns.append(ui->pushButton_c26);
    _listCharacterBtns.append(ui->pushButton_c27);
    _listCharacterBtns.append(ui->pushButton_c28);
    _listCharacterBtns.append(ui->pushButton_c29);
    for(int index = 0; index < _strKeyboradCharacters.size(); index++)
    {
        _listCharacterBtns.at(index)->setText(_strKeyboradCharacters.at(index));
        connect(_listCharacterBtns.at(index), SIGNAL(clicked(bool)),
                this, SLOT(slot_characterButtonsClicked()));
    }
    // 功能键:回退
    ui->pushButton_backspace->setText(QChar(0xf060));
    ui->pushButton_backspace->setFont(_font);
    connect(ui->pushButton_backspace, SIGNAL(clicked(bool)), this, SLOT(slot_backspace()));
    // 功能键:大小写
    ui->pushButton_upper->setFont(_font);
    ui->pushButton_upper->setText(QChar(0xf062));
    connect(ui->pushButton_upper, SIGNAL(clicked(bool)), this, SLOT(slot_upper()));
    // 功能键:回车
    ui->pushButton_enter->setFont(_font);
    ui->pushButton_enter->setText("Enter");
    connect(ui->pushButton_enter, SIGNAL(clicked(bool)), this, SLOT(slot_enter()));
    // 功能键:切换符号
    ui->pushButton_symbol->setText(".?123");
    connect(ui->pushButton_symbol, SIGNAL(clicked(bool)), this, SLOT(slot_symbols()));
    // 功能键:语言切换
    ui->pushButton_language->setText("English");
    connect(ui->pushButton_language, SIGNAL(clicked(bool)), this, SLOT(slot_switchLanguage()));
    // 功能键:空格
    ui->pushButton_blankspace->setText("");
    connect(ui->pushButton_blankspace, SIGNAL(clicked(bool)), this, SLOT(slot_blankspace()));
    // 功能键:表情
    ui->pushButton_emoji->setFont(_font);
    ui->pushButton_emoji->setText(QChar(0xf118));
    connect(ui->pushButton_emoji, SIGNAL(clicked(bool)), this, SLOT(slot_emoji()));
    // 功能键:隐藏
    ui->pushButton_hideKeyboard->setFont(_font);
    ui->pushButton_hideKeyboard->setText(QString(QChar(0xf11c)).append(QChar(0xf103)));
    connect(ui->pushButton_hideKeyboard, SIGNAL(clicked(bool)), this, SLOT(slot_hideKeyboard()));

    updateChineseCharactersButtonState();
}

KeyBoardWidget::~KeyBoardWidget()
{
    delete ui;
}
...
void KeyBoardWidget::setKeyPressEvent(QKeyEvent *event)
{
    if(_currentMode == INPUT_MODE_SYMB
            && _symbolPage == 0)
    {
        switch (event->key())
        {
        case Qt::Key_1:
            _listCharacterBtns.at(0)->click();
            break;
        case Qt::Key_2:
            _listCharacterBtns.at(1)->click();
            break;
        case Qt::Key_3:
            _listCharacterBtns.at(2)->click();
            break;
        case Qt::Key_4:
            _listCharacterBtns.at(3)->click();
            break;
        case Qt::Key_5:
            _listCharacterBtns.at(4)->click();
            break;
        case Qt::Key_6:
            _listCharacterBtns.at(5)->click();
            break;
        case Qt::Key_7:
            _listCharacterBtns.at(6)->click();
            break;
        case Qt::Key_8:
            _listCharacterBtns.at(7)->click();
            break;
        case Qt::Key_9:
            _listCharacterBtns.at(8)->click();
            break;
        case Qt::Key_0:
            _listCharacterBtns.at(9)->click();
            break;
        case Qt::Key_Backspace:
            ui->pushButton_backspace->click();
            break;
        case Qt::Key_Return:
            ui->pushButton_enter->click();
            break;
        default:
            break;
        }
    }
    else
    {
        switch (event->key())
        {
        case Qt::Key_Q:
            _listCharacterBtns.at(0)->click();
            break;
        case Qt::Key_W:
            _listCharacterBtns.at(1)->click();
            break;
        case Qt::Key_E:
            _listCharacterBtns.at(2)->click();
            break;
        case Qt::Key_R:
            _listCharacterBtns.at(3)->click();
            break;
        case Qt::Key_T:
            _listCharacterBtns.at(4)->click();
            break;
        case Qt::Key_Y:
            _listCharacterBtns.at(5)->click();
            break;
        case Qt::Key_U:
            _listCharacterBtns.at(6)->click();
            break;
        case Qt::Key_I:
            _listCharacterBtns.at(7)->click();
            break;
        case Qt::Key_O:
            _listCharacterBtns.at(8)->click();
            break;
        case Qt::Key_P:
            _listCharacterBtns.at(9)->click();
            break;
        case Qt::Key_A:
            _listCharacterBtns.at(10)->click();
            break;
        case Qt::Key_S:
            _listCharacterBtns.at(11)->click();
            break;
        case Qt::Key_D:
            _listCharacterBtns.at(12)->click();
            break;
        case Qt::Key_F:
            _listCharacterBtns.at(13)->click();
            break;
        case Qt::Key_G:
            _listCharacterBtns.at(14)->click();
            break;
        case Qt::Key_H:
            _listCharacterBtns.at(15)->click();
            break;
        case Qt::Key_J:
            _listCharacterBtns.at(16)->click();
            break;
        case Qt::Key_K:
            _listCharacterBtns.at(17)->click();
            break;
        case Qt::Key_L:
            _listCharacterBtns.at(18)->click();
            break;
        case Qt::Key_Z:
            _listCharacterBtns.at(19)->click();
            break;
        case Qt::Key_X:
            _listCharacterBtns.at(20)->click();
            break;
        case Qt::Key_C:
            _listCharacterBtns.at(21)->click();
            break;
        case Qt::Key_V:
            _listCharacterBtns.at(22)->click();
            break;
        case Qt::Key_B:
            _listCharacterBtns.at(23)->click();
            break;
        case Qt::Key_N:
            _listCharacterBtns.at(24)->click();
            break;
        case Qt::Key_M:
            _listCharacterBtns.at(25)->click();
            break;
        case Qt::Key_Comma:
            _listCharacterBtns.at(26)->click();
            break;
        case Qt::Key_Period:
            _listCharacterBtns.at(27)->click();
            break;
        case Qt::Key_Question:
            _listCharacterBtns.at(28)->click();
            break;
        case Qt::Key_Backspace:
            ui->pushButton_backspace->click();
            break;
        case Qt::Key_Space:
            ui->pushButton_blankspace->click();
            break;
        case Qt::Key_Return:
            ui->pushButton_enter->click();
            break;
        case Qt::Key_CapsLock:
            ui->pushButton_upper->click();
            break;
        default:
            break;
        }
    }

}


若该文为原创文章,转载请注明原文出处
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/111831179

你可能感兴趣的:(项目实战,Qt开发,Qt虚拟键盘,Qt中英文输入,Qt屏幕键盘,Qt拼音输入法)