專案實戰:Qt中英文輸入軟鍵盤(支援Qt4、Qt5、觸控和鍵鼠混合輸入等)

語言: CN / TW / HK

若該文為原創文章,轉載請註明原文出處 本文章部落格地址:https://blog.csdn.net/qq21497936/article/details/111831179 長期持續帶來更多專案與技術分享,諮詢請加QQ:21497936、微信:yangsir198808
紅胖子(紅模仿)的博文大全:開發技術集合(包含Qt實用技術、樹莓派、三維、OpenCV、OpenGL、ffmpeg、OSG、微控制器、軟硬結合等等)持續更新中…(點選傳送門)

開發專欄:專案實戰

<br>

需求

  1. 全屏軟鍵盤;   2. 輸入英文;   3. 輸入中文;   4. 支援觸控、鍵盤和輸入混合輸入;   5. 目前有黑色系面板;   6. Qt4和Qt5區分2個版本;

<br>

Demo:Qt5

  在這裡插入圖片描述   在這裡插入圖片描述   在這裡插入圖片描述   CSDN(粉絲免積分下載):https://download.csdn.net/download/qq21497936/13945327   QQ群:1047134658(點選“檔案”搜尋“softKeyboard”,群內與博文同步更新)

<br>

Demo:Qt4

  移植到MCGS嵌入式工控機上   在這裡插入圖片描述   在這裡插入圖片描述

<br>

V1.0.0

  在這裡插入圖片描述   在這裡插入圖片描述   在這裡插入圖片描述   在這裡插入圖片描述   在這裡插入圖片描述

<br>

Demo程式碼

  視覺化的佈局,以便於定製修改,所見即所得:   在這裡插入圖片描述 KeyBoardWidget.h

#ifndef KEYBOARDWIDGET_H
#define KEYBOARDWIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>

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<QPushButton*> _listCharacterBtns;
    QList<QPushButton*> _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 <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFontDatabase>
#include <QFile>
#include <QApplication>
#include <QMouseEvent>
#include <QDebug>
#include <QDesktopWidget>

...

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;
        }
    }

}

<br>

若該文為原創文章,轉載請註明原文出處 本文章部落格地址:https://blog.csdn.net/qq21497936/article/details/111831179

「其他文章」