简体   繁体   中英

Qt on mac OSX, clicking the desktop

I am experimenting with Qt on Mac OSX 10. What I am trying to do: Move cursor to a specific position and left-click on a desktop item (in this case as an example, the "Apple" Symbol in the left upper corner of the desktop. I have tried it with the QT Test Events:

first go was to add a "mouseClick" to a QTestEventList and simulate it:

list.addMouseClick(Qt::LeftButton, Qt::NoModifier, point);
QWidget* widget = QApplication::desktop();
list.simulate(widget);

This did not work out. I also tried:

QPoint point(26,11);
QCursor::setPos(point);
list.addMouseClick(Qt::LeftButton, Qt::NoModifier);
QWidget* widget = QApplication::desktop();
list.simulate(widget);

This also did not work. My last attempt:

list.addMousePress(Qt::LeftButton, Qt::NoModifier);
list.addDelay(1000);
list.addMouseRelease(Qt::LeftButton, Qt::NoModifier);
QWidget* widget = QApplication::desktop();
list.simulate(widget);

-> also no success.

Is it possible that the QTestEventList used on the DesktopWidget from the Application has no effect at all on Mac? Or I am doing something wrong...

Help would be really appreciated ;-) Thanks in advance!

Luc

I have never tried using QTestEventList for simulating mouse events on a Mac. Instead, I used the Mac Quartz Event Services API .

The following code illustrates how to move the mouse to a certain position and how to simulate a click event:

#include <ApplicationServices/ApplicationServices.h>

// Move the mouse to the position x, y on the screen
int x = 26;
int y = 11;
CGEventRef mouseEv = CGEventCreateMouseEvent(
                    NULL, kCGEventMouseMoved,
                    CGPointMake(x, y),
                    kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, mouseEv);
CFRelease(mouseEv);

// Simulate a left mouse click down
CGEventRef mouseEv = CGEventCreateMouseEvent(
                NULL, kCGEventLeftMouseDown,
                getCursorPosition(),
                kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, mouseEv);
CFRelease(mouseEv);

// Simulate a left mouse click up
CGEventRef mouseEv = CGEventCreateMouseEvent(
                NULL, kCGEventLeftMouseUp,
                getCursorPosition(),
                kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, mouseEv);
CFRelease(mouseEv);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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