模拟键盘事件 (实测)

官方文档:Qt5

QWidget *QWidget::focusWidget() const

Returns the last child of this widget that setFocus had been called on. For top level widgets this is the widget that will get focus in case this window gets activated

This is not the same as QApplication::focusWidget(), which returns the focus widget in the currently active window.

QWidget之键盘焦点

在Qt中,键盘事件和QWidget的focus密不可分:一般来说,一个拥有焦点(focus)的QWidget或者grabKeyboard()的QWidget才可以接受键盘事件。

https://blog.csdn.net/ken2232/article/details/129273291

Qt中focusWidget的使用

Qt中focusWidget中有两种实现方法,一种是在QApplication中,一种是在QWidget中,这两个函数的用法不相同。

https://blog.csdn.net/w_weixiaotao/article/details/110187172

Qt界面focus焦点设置的一些体会

【QT】新弹窗默认无焦点

最终发现我的登录框出现的时候,焦点是有的,但它在窗体上,不在输入框里。

1

ui->accountLineEdit->setFocus();

其实只要一行代码就解决了问题,刚才的那行可以写在构造函数里。

问题二:
我希望我的Tab顺序是在账号框和密码框来回跳,又该怎么做呢?

其实这个问题跟我们的focusePolicy有关系的。

1

setTabOrder(ui->accountLineEdit, ui->passwordLineEdit);

https://www.codenong.com/cs105847810/

=========

有效代码:Qt5  , 复合按键,好像无效?

#include <QKeyEvent>
#include <QEvent>

void MainWindow::on_actiondelect_triggered()
{

    QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Delete,Qt::NoModifier);
    QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Delete,Qt::NoModifier);
    //
    在同一个Ui页面内,同时有几个需要删除的不同部件;
    如果不设置 focus,则需要一个一个地分别设置,麻烦。
    //
    //获取当前的焦点widget
    QWidget* focus = QWidget::focusWidget();
    QApplication::sendEvent(focus, &keyPress);//发送事件模拟键盘的delete键
    QApplication::sendEvent(focus, &keyRelease);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值