QT虚拟键盘(QWidget)的实现

*QT虚拟键盘的实现

:**
在QApplication a(argc, argv);之前加上
qputenv(“QT_IM_MODULE”, QByteArray(“qtvirtualkeyboard”));
即可实现简易的虚拟键盘(qt自带的):
main.cpp

#include "mainwindow.h"

#include <QApplication>
#include"lineedit.h"
#include <QtWidgets>
#include"QDebug"
#define W 1024
#define H 768

int main(int argc, char *argv[]) {
    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

效果显示
但是,会遮盖部分内容,挡住输入内容。

重写QLineEdit(防止虚拟键盘遮挡输入框)

lineedit.h
#ifndef LINEEDIT_H
#define LINEEDIT_H
#include"QLineEdit"

class LineEdit :public QLineEdit {
        Q_OBJECT
public:
    LineEdit(QWidget *parent = nullptr);
    LineEdit(const QString&, QWidget *parent = nullptr);

protected:
    bool event(QEvent*) override;

private:
    bool _moved = false;
    int _lastDiff = 0;
};

#endif // LINEEDIT_H

lineedit.cpp

#include "lineedit.h"
#include <QGuiApplication>
#include"QDebug"
LineEdit::LineEdit(QWidget *parent) :QLineEdit(parent) {
    setAttribute(Qt::WA_InputMethodEnabled, true);
    setInputMethodHints(inputMethodHints() | Qt::InputMethodHint::ImhPreferLowercase);
}

LineEdit::LineEdit(const QString& txt, QWidget *parent) : QLineEdit(txt, parent) {
    //必须在创建自定义文本编辑小部件时设置。
    //setAttribute如果on为真,则设置此小部件的属性属性;否则清除属性。void QWidget::setAttribute(Qt::WidgetAttribute attribute, bool on = true)
    setAttribute(Qt::WA_InputMethodEnabled, true);
    //ImhPreferLowercase小写字母优先(但不是必需)。
    //如果设置了Qt::ImhFormattedNumbersOnly标志,输入法可能会改变其可视化组件,以反映只能输入数字。
    setInputMethodHints(inputMethodHints() | Qt::InputMethodHint::ImhPreferLowercase);
}

bool LineEdit::event(QEvent* e) {
    //keyboardRectangle虚拟键盘的几何图形在窗口坐标。 如果不可能知道键盘的几何形状,这可能是一个空矩形。
    const auto keyboard_rect = QGuiApplication::inputMethod()->keyboardRectangle();
    const auto keyboard_visible = QGuiApplication::inputMethod()->isVisible();
    const auto global_y = QWidget::mapToGlobal(rect().topLeft()).y() + height();
    const auto k_global_y = keyboard_rect.topLeft().y();
    const auto diff = k_global_y - global_y;
    const auto need_to_move = diff < 0;

    /* move main widget */
    if (keyboard_visible && !_moved && need_to_move) {
        _moved = true;
        _lastDiff = diff;
        const auto g = parentWidget()->frameGeometry();
        parentWidget()->move(g.x(), g.y() - qAbs(_lastDiff));
    }
    /* roll back */
    if (!keyboard_visible && _moved) {
        _moved = false;
        const auto g = parentWidget()->frameGeometry();
        parentWidget()->move(g.x(), g.y() + qAbs(_lastDiff));
    }
    return QLineEdit::event(e);
}

在这里插入图片描述在这里插入图片描述

在这里插入图片描述
感谢参考文档:https://www.it1352.com/1822481.html

  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
Qt实现虚拟键盘可以通过以下步骤: 1. 创建一个QWidget或QDialog,作为虚拟键盘的窗口。 2. 在窗口添加多个QPushButton作为键盘的按键,可以使用Qt的布局管理器来自动排列按钮位置。 3. 为每个按钮添加响应的槽函数,以便在按下按钮时执行相应的操作,例如模拟键盘输入、控制光标位置等。 4. 可以使用Qt的信号和槽机制来将虚拟键盘与其他控件关联,例如将虚拟键盘与文本框关联,当按下虚拟键盘的按键时,自动向文本框输入相应的文本。 5. 最后,将虚拟键盘窗口显示出来,并根据需要设置其大小、位置、样式等属性。 以下是一个简单的示例代码: ```cpp #include <QtWidgets> class VirtualKeyboard : public QDialog { Q_OBJECT public: VirtualKeyboard(QWidget *parent = nullptr); private slots: void buttonClicked(); private: QPushButton *m_buttons[10]; QPushButton *m_okButton; QPushButton *m_cancelButton; }; VirtualKeyboard::VirtualKeyboard(QWidget *parent) : QDialog(parent) { QVBoxLayout *layout = new QVBoxLayout; QGridLayout *gridLayout = new QGridLayout; for (int i = 0; i < 10; ++i) { m_buttons[i] = new QPushButton(QString::number(i), this); connect(m_buttons[i], SIGNAL(clicked()), this, SLOT(buttonClicked())); gridLayout->addWidget(m_buttons[i], i / 3, i % 3); } m_okButton = new QPushButton(tr("OK"), this); connect(m_okButton, SIGNAL(clicked()), this, SLOT(accept())); m_cancelButton = new QPushButton(tr("Cancel"), this); connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(reject())); layout->addLayout(gridLayout); layout->addWidget(m_okButton); layout->addWidget(m_cancelButton); setLayout(layout); setWindowTitle(tr("Virtual Keyboard")); } void VirtualKeyboard::buttonClicked() { QPushButton *button = qobject_cast<QPushButton *>(sender()); Q_ASSERT(button != nullptr); // do something with the button text, such as simulate keyboard input QString text = button->text(); QKeyEvent keyPress(QEvent::KeyPress, text.toInt() + Qt::Key_0, Qt::NoModifier, text); QCoreApplication::postEvent(QApplication::focusWidget(), &keyPress); // move focus to the next widget QApplication::focusWidget()->focusNextPrevChild(true); } int main(int argc, char *argv[]) { QApplication app(argc, argv); QLineEdit *lineEdit = new QLineEdit; QPushButton *button = new QPushButton(tr("Show Keyboard")); QWidget::connect(button, &QPushButton::clicked, [&]() { VirtualKeyboard keyboard; if (keyboard.exec() == QDialog::Accepted) { lineEdit->setText(lineEdit->text() + " OK"); } else { lineEdit->setText(lineEdit->text() + " Cancel"); } }); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(lineEdit); layout->addWidget(button); QWidget window; window.setLayout(layout); window.show(); return app.exec(); } #include "main.moc" ``` 这个示例程序创建了一个带有10个数字按钮、OK按钮和Cancel按钮的虚拟键盘。当用户点击数字按钮时,程序会向当前焦点部件发送相应的按键事件,以模拟键盘输入。当用户点击OK或Cancel按钮时,程序会返回相应的结果。在主窗口,点击“Show Keyboard”按钮可以显示虚拟键盘,并将其与当前的文本框关联起来。
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值