Auto Click Qt5 C ++

I am trying to make an application in which the mouse goes to certain locations on the screen and automatically presses the left button. The problem is that I cannot click outside of the Qt application, so I made a workaround by making the application transparent to mouse clicks and making it fullscreen with this code:

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 click does not work. I also tried replacing the Qt class for handling mouse events and using the window API, since I do not need a cross-platform application, but I am stuck at one point.

+4
source share
1 answer

, , , . .

, , . , API, -, , , , , .

, , , QT5 .

QTest Namespace. , QTEST:: mouseClick.

, , :

#include <QTest>

// class and function declarations removed here

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

Source: https://habr.com/ru/post/1526193/


All Articles