Important: Please read the Qt Code of Conduct -

mimicikng mouse interaction

  • Hi,
    I want to control my Qt Application with a Leap Motion Device. The Application GUI is some basic menu with buttons, sliders, checkboxes and comboboxes. So far I use a signal to tell my application when the hand position changes so I can control the cursor with my hand. Now I want to click on widgets in the menu. My current approach is to subclass each widget and invoke a "mouse click" when the cursor hovers the widget for some time. My problem is to invoke the "click" for each widget. For buttons it was pretty easy by using the click() slot. For sliders and comboboxes I fiddled around some time but could not manage to get the "mouse-like" behavior (e.g. dragging the slider with the hand).

    So the question for me is: How can I fake the mouse interaction (moving, clicking, dragging, scrolling, etc.) through code in Qt? I think there must be a more general approach than subclassing each widget.
    What just came to my mind is using QTest for that but it feels a bit hackish to use testing mechanics in application code. Do you have any other ideas?

    Btw I am using Qt 5.6 with mscv2015.

  • Lifetime Qt Champion

    Hi and welcome
    I agree that
    might be slight hack-ish. But would be easy :)

    Other option would be to install an event filter on the widgets and record
    all events and values and see if you can play it back using QMouseEvent and friends
    where u set the values manually.

    Dragging a slider cannot be done with just mouse click. there will be mutiple events involved.

    I have never tried this but in theory u can store the needed events and re-create it for same effect.

    I made a info function when checking out drag & drop ,
    it might be usefull for u to get names on the events to see what is being sent.

    I call it like
    qDebug() << "EVENT: " << event->type() << " = " << EventDesc(event->type() );

  • Moderators

    I don't think you should mess with any widgets.
    It seems to be a matter of faking mouse events. You can find a widget under cursor using widgetAt(). If it returns a widget you just create a fake QMouseEvent and call sendEvent. The widget won't even know if the event came from your device or a real mouse, so no modification to it are needed.

Log in to reply