Qt 5中的屏幕键盘 [英] Onscreen Keyboard in Qt 5
问题描述
我想为桌面应用程序创建一个屏幕键盘.该应用程序将在Qt 5中构建.我有几个问题,请澄清.
I want to create a onscreen keyboard for a desktop application. The application will be built in Qt 5. I have couple of questions, please clarify them.
-
在Qt5中替换
QInputContext
的是什么? (因为我通过实现QInputContext
阅读了有关屏幕键盘的内容,但Qt 5不支持此功能.)
What is the replacement of
QInputContext
in Qt5? (Because I read somewhere about onscreen keybord by implementingQInputContext
but this is not supported by Qt 5.)
在哪里可以找到QPlateformInputContext
& QInputPanel
(在互联网搜索中,我找到了这两个作为QInputContext
的替代品,但不确定,也无法找到它们)?
Where can I find QPlateformInputContext
& QInputPanel
(on an internet search I found these two as alternatives of QInputContext
but not sure about that and also I was unable to find them)?
我的要求:
- 键盘将不使用QML或任何外部库(已构建其他键盘)
- 键盘将使用Qt Gui(繁体)
推荐答案
我花了相当长的时间来找出如何在QT5中完成此工作而不需要qml和过多的工作.以为我愿意分享:
I took me quite a while to find out how to do this in QT5 without qml and too much work. So thought I'd share:
#include <QCoreApplication>
#include <QGuiApplication>
#include <QKeyEvent>
void MainWindow::on_pushButton_clicked()
{
Qt::Key key = Qt::Key_1;;
QKeyEvent pressEvent = QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, QKeySequence(key).toString());
QKeyEvent releaseEvent = QKeyEvent(QEvent::KeyRelease, key, Qt::NoModifier);
QCoreApplication::sendEvent(QGuiApplication::focusObject(), &pressEvent);
QCoreApplication::sendEvent(QGuiApplication::focusObject(), &releaseEvent);
}
这里的提示是,通过单击按钮(如果您要手动制作键盘),可以向当前具有焦点的对象(例如文本框)启动sendevent.您当然可以对文本框进行硬编码,但是只有在您只有一个输入要使用键盘的情况下,这才起作用.
The clue here is that by clicking buttons (if you would manually make your keyboard), launches a sendevent to the current object thas has focus (for example a textbox). You could of course hardcode a textbox, but that only works if you have only a single input to use your keyboard for.
您需要确保的最后一件事是将键盘按钮的focusPolicy设置为NoFocus,以防止在按下键盘时焦点发生转移.
The last thing you have to make sure, is to set the focusPolicy of your keyboard buttons to NoFocus, to prevent focus from shifting when the keyboard is pressed.
贷方转到 https://www .wisol.ch/w/articles/2015-07-26-virtual-keyboard-qt/
希望这对某人有帮助.
这篇关于Qt 5中的屏幕键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!