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.
source
share