QtWidgets 应用程序的虚拟键盘或屏幕键盘?

Virtual keyboard or onscreen keyboard for QtWidgets applications?

我将像这样在基于小部件的应用程序中部署 qtvirtualkeyboard

#include <QtWidgets>

int main(int argc, char *argv[]) {
    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
    QApplication app(argc, argv);
    QMainWindow window;
    QLineEdit input(&window);
    input.move(250, 250);
    window.show();
    return app.exec();
}

但唯一的问题是虚拟键盘输入面板隐藏了底层的小部件并覆盖了它们!

我该如何实现?

是否有针对基于小部件的应用程序的文档或解决方案?

你只需要在 main.cpp

中添加这一行
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));

并将在 Qtwidgets 中使用虚拟键盘))

终于找到答案了!

你只需要调用 QGuiApplication::inputMethod() 来获取应用程序范围的 Qt 输入法,然后调用 QInputMethod::keyboardRectangle() and QInputMethod::isVisible() 来获取输入法属性,然后根据你的小部件位置和键盘坐标进行计算,这里有一个完整的示例供分享:

lineedit.h:

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

lineedit.cpp:

LineEdit::LineEdit(QWidget *parent) :QLineEdit(parent) {
    setAttribute(Qt::WA_InputMethodEnabled, true);
    setInputMethodHints(inputMethodHints() | Qt::InputMethodHint::ImhDigitsOnly);
}

LineEdit::LineEdit(const QString& txt, QWidget *parent) : QLineEdit(txt, parent) {
    setAttribute(Qt::WA_InputMethodEnabled, true);
    setInputMethodHints(inputMethodHints() | Qt::InputMethodHint::ImhDigitsOnly);
}

bool LineEdit::event(QEvent* e) {
    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);
}

main.cpp:

#include <QtWidgets>

#define W 1024
#define H 768

int main(int argc, char *argv[]) {
    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
    QApplication app(argc, argv);

    QMainWindow window(nullptr, Qt::FramelessWindowHint);

    LineEdit lineedit1(&window);
    lineedit1.move(100, 450);

    LineEdit lineedit2(&window);
    lineedit2.move(100, 100);

    window.resize(W, H);
    window.show();
    return app.exec();
}

结果: