C++ Qt 5 中的屏幕键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18979015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Onscreen Keyboard in Qt 5
提问by Jai
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.
我想为桌面应用程序创建屏幕键盘。该应用程序将在 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.)Where can I find
QPlateformInputContext
&QInputPanel
(on an internet search I found these two as alternatives ofQInputContext
but not sure about that and also I was unable to find them)?
QInputContext
Qt5中的替代品是什么?(因为我通过实现阅读了有关屏幕键盘的某处,QInputContext
但这不受 Qt 5 支持。)我在哪里可以找到
QPlateformInputContext
&QInputPanel
(在互联网搜索中,我发现这两个是替代品,QInputContext
但不确定,而且我也找不到它们)?
My requirements:
我的要求:
- Keyboard will not use QML or any external library (already build other keyboards)
- Keyboard will use Qt Gui (traditional)
- 键盘不会使用 QML 或任何外部库(已构建其他键盘)
- 键盘将使用 Qt Gui(传统)
采纳答案by Arnout
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:
我花了很长时间才找到如何在没有 qml 和太多工作的情况下在 QT5 中做到这一点。所以想我会分享:
#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);
}
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.
这里的线索是通过单击按钮(如果您要手动制作键盘),向具有焦点的当前对象(例如文本框)启动 sendevent。您当然可以对文本框进行硬编码,但这仅在您只有一个输入可以使用键盘时才有效。
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.
您必须确保的最后一件事是将键盘按钮的 focusPolicy 设置为 NoFocus,以防止按下键盘时焦点移动。
Credits go to https://www.wisol.ch/w/articles/2015-07-26-virtual-keyboard-qt/
学分转到https://www.wisol.ch/w/articles/2015-07-26-virtual-keyboard-qt/
Hope this helps someone.
希望这可以帮助某人。
回答by user1055604
I understand there are two challenges you would have:
我知道您将面临两个挑战:
- Getting notified as to when to show/hide the on-screen keyboard, based on the focus being on text widgets
- How to post key-press event to the text widgets
- 根据焦点在文本小部件上,获得有关何时显示/隐藏屏幕键盘的通知
- 如何将按键事件发布到文本小部件
ANSWER
回答
- As for the former, you could use
QObject::InstallEventFilter()
on widgets that you want to provide the keyboard service to. You can then look for themouseReleaseEvent
along the lines of the Qt code in the link. - This can be achieved by using
QCoreApplication::postEvent()
- 至于前者,您可以
QObject::InstallEventFilter()
在要为其提供键盘服务的小部件上使用。然后,您可以mouseReleaseEvent
在链接中沿着 Qt 代码的行查找。 - 这可以通过使用来实现
QCoreApplication::postEvent()
As for QPlatformInputContext
, get the example of a Qt Virtual Keyboardhere.
至于QPlatformInputContext
,请在此处获取Qt 虚拟键盘的示例。
回答by Fatih ?zlü
A good example is given in here http://tolszak-dev.blogspot.com.tr/2013/04/qplatforminputcontext-and-virtual.htmluses Qt Quick for on screen keyboard. You can check it.
这里给出了一个很好的例子http://tolszak-dev.blogspot.com.tr/2013/04/qplatforminputcontext-and-virtual.html使用 Qt Quick 作为屏幕键盘。你可以检查一下。
回答by michaelmoo
Qt now provides a virtual keyboard framework in Qt 5.5.
Qt 现在在 Qt 5.5 中提供了一个虚拟键盘框架。
http://doc.qt.io/QtVirtualKeyboard/
http://doc.qt.io/QtVirtualKeyboard/
I have not tried it, so I can't say how easy it is to use. It looks like it's QML-based.
我没有尝试过,所以我不能说它是多么容易使用。看起来它是基于 QML 的。
(It says it's for Linux and boot2qt, but it can also be built for Windows according to the building page (http://doc.qt.io/QtVirtualKeyboard/build.html))
(它说它适用于 Linux 和 boot2qt,但也可以根据构建页面(http://doc.qt.io/QtVirtualKeyboard/build.html)为 Windows 构建)
回答by phyatt
I just got this working in my awesome Qt app. Here is how I did it.
我刚刚在我很棒的 Qt 应用程序中使用了它。这是我如何做到的。
For Android and iOS:
对于 Android 和 iOS:
QObject::connect(lineEdit, SIGNAL(returnPressed()), qApp->inputMethod(), SLOT(hide()));
For iOS:
对于 iOS:
Subclass QLineEdit and add the following:
子类 QLineEdit 并添加以下内容:
void focusOutEvent(QFocusEvent * fe)
{
QLineEdit::focusOutEvent(fe);
#ifdef Q_OS_IOS
if(fe->reason() == Qt::OtherFocusReason)
{
// Done was pressed!
emit returnPressed();
}
#endif
}
Btw, the QInputMethod docs don't say much about how to access it from c++. You have to get an instance from QGuiApplication, like I did above.
顺便说一句,QInputMethod 文档并没有说明如何从 C++ 访问它。你必须从 QGuiApplication 获取一个实例,就像我上面做的那样。
Hope that helps.
希望有帮助。