• Qt 模拟鼠标点击(QApplication::sendEvent(ui->pushbutton, &event0);)


    QPoint pos(0,0);
    QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    QApplication::sendEvent(ui->pushbutton, &event0);

    QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    QApplication::sendEvent(ui->pushbutton, &event1);
    1
    2
    3
    4
    5
    6
    sendEvent 是发送事件函数,第一个参数是接收该事件的元对象,第二个参数是要发送的事件;
    QMouseEvent用来构造一个模拟的鼠标事件,构造函数中定义了事件的类型为按压或者是释放,pos参数是用来指定在接收该事件的元对象的事件发生位置,例子中就是指定了在按钮的(0,0)位置接收该点击事件;
    QPos定义位置。

    对于QLineEdit这种部件,可以通过setFocus()函数先让部件获得角点,再把sendEvent()中的接收对象换成QWidget::focusWidget();
    ---------------------
    作者:蜗牛在听雨
    来源:CSDN
    原文:https://blog.csdn.net/omg_orange/article/details/75803977
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    221. 最大正方形
    9. 回文数
    2. 两数相加
    1. 两数之和
    HDU 1864 最大报销额
    47 java包打成本地maven
    46 数组中的元素进行位置交换
    5 docker安装kibana
    45 vue图片放大预览
    4 docker中安装es
  • 原文地址:https://www.cnblogs.com/findumars/p/10344545.html
Copyright © 2020-2023  润新知