我为信息亭应用程序制作了一个自定义的虚拟键盘小部件,现在是时候让我产生虚假的键盘事件并将其提供给所选的QLineEdit了。
我执行以下操作:
// target is the QWidget to receive the events
// k is the Qt::Key (keycode) I want to send (Testing with an 'A')
Qt::Key k=Qt::Key_A;
if(0!=target){
//According to docs this will be freed once posted
QKeyEvent * press=new QKeyEvent(QKeyEvent::KeyPress, (int )k,0);
QKeyEvent * release=new QKeyEvent(QKeyEvent::KeyRelease, (int )k,0);
//Give the target focus just to be sure it is available for input
target->setFocus();
//Post the events (queue up and let the target consume them when the eventloop gets around to the target)
QCoreApplication::postEvent ( target, press) ;
QCoreApplication::postEvent ( target, release) ;
}
我看到目标窗口小部件获得了焦点,但是没有像我期望的那样在输入字段中键入字母。我究竟做错了什么?哪些假设是错误的?
PS:我知道可以通过使用现有的虚拟键盘或至少使用平台界面来解决此问题,如本文所述。在我们的方法中,我们决定将滑水板内置到应用程序中,以获得对UX和键盘设计的完全控制。
谢谢!
由于没有人加紧,我将尝试提供一些解决方案。
事实证明,Qt5附带了一个名为testlib的测试工具库。它具有各种各样的优点,可简化Qt应用程序的单元测试的创建,管理和运行。在这些工具中,有一组用于发送伪造事件的功能,例如伪造文本,单击鼠标等。它非常全面,涵盖了许多用例。由于Qt开发人员在内部用于测试Qt本身,因此它也是经过生产验证的代码。
我只是从那里复制了我需要的东西。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句