简体   繁体   English

Qt5 C ++自动鼠标点击

[英]Qt5 C++ automated mouse clicking

I am trying to make an application where the mouse goes to certain locations on screen and automatically clicks left button. 我正在尝试创建一个应用程序,鼠标移动到屏幕上的某些位置,并自动单击左键。 The problem is I cant click outside the Qt application so I made a workaround by making the application transparent to mouse clicks and making it full screen using this code: 问题是我无法在Qt应用程序之外单击,所以我通过使应用程序对鼠标单击透明并使用以下代码全屏显示来解决方法:

int x = 800;
int y = 500;

this->setWindowFlags(Qt::WindowStaysOnTopHint|Qt::FramelessWindowHint|Qt::ToolTip);
this->setAttribute(Qt::WA_TranslucentBackground);
this->setAttribute( Qt::WA_TransparentForMouseEvents);

QCursor::setPos(x,y);
qDebug()<<QCursor::pos();
QWidget *d = QApplication::desktop()->screen();
QMouseEvent MouseEvent(QEvent::MouseButtonPress, QCursor::pos(),Qt::LeftButton,Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(d, &MouseEvent);
QApplication::sendEvent(d, &MouseEvent);

The mouse cursor moves to the desired location but the clicking doesn't work. 鼠标光标移动到所需位置,但点击不起作用。 I also tried replacing the Qt class for handling mouse events and use windows API since I don't really need cross-platform application but I am getting stuck at the same point. 我也尝试更换Qt类来处理鼠标事件并使用Windows API,因为我不需要跨平台应用程序,但我在同一点上陷入困​​境。

I know that this is a bit late, but there are ways to do what you want. 我知道这有点晚了,但有办法做你想做的事。 In general creating or simulating user interface events is used only for automated testing. 通常,创建或模拟用户界面事件仅用于自动测试。

If you want the results of some calculation or algorithm there is almost always a better to get it desired result from a piece of software other than automating its UI. 如果你想要一些计算或算法的结果,除了自动化UI之外,几乎总能更好地从一个软件获得所需的结果。 You can see if it has a documented API, you can call the same web services it calls, you can link against the same libraries it does or you can duplicate algorithms it uses. 您可以看到它是否具有文档化的API,您可以调用它调用的相同Web服务,您可以链接它所执行的相同库,也可以复制它使用的算法。

If none of these are acceptable then you should probably look at the QT5 tutorials on automated testing . 如果这些都不可接受,那么您应该查看有关自动化测试QT5教程

Specifically the QTest Namespace . 特别是QTest命名空间 There are two overloads for functions that click the mouse called QTEST::mouseClick . 单击鼠标的函数有两个重载,称为QTEST :: mouseClick

I think this might do what you want: 我想这可能会做你想要的:

#include <QTest>

// class and function declarations removed here

QTest::mouseClick(d, Qt::LeftButton, Qt::NoModifier, QPoint(x,y));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM