Important: Please read the Qt Code of Conduct -

Determining the source of a QMouseEvent

  • My QML/C++ GUI receives touch screen and mouse inputs. All events come through as QMouseEvents, but I need to handle them differently depending if the source device is the touch screen or if it is the mouse (or touchpad mouse). Is there a way to look up the source device of a QMouseEvent?


  • Why do you want to distinguish between the two? As far as the OS is concerned, both devices are pointer devices and therefore they probably get the same treatment.

  • Basically, there are two ways of interacting with my GUI: Either a traditional touch screen, or by using a mouse (which is actually a track ball mouse) as a remote control from a couple feet away. It's difficult to use flicking and navigate efficiently with the mouse, so I have some special QML MouseArea's that simplify scrolling with the mouse just by entering the area and moving the mouse up or down. I use some C++ tricks ala QCursor::setPos() to scroll the entire length of a QML ListView without having to drag the mouse to the bottom of the screen, then grab, and scroll to the top.

    The problem is that the touch screen QMouseEvent's get handled by my mouse-only QML MouseArea's. If I could tell the difference between a touch screen mouse event and a normal mouse event, then I could ignore touches inside those mouse-only QML MouseArea's.

    Also, I hide the cursor when touch screen events come in, and show it when mouse events happen.

  • I'm not aware of any way of differentiating between one or the other input device. I guess, though, that mouse gestures may be interesting for your case. It will give you a slightly different user experience, but none of that differentiating stuff. It's just a thought.

  • I've been experimenting with reading raw linux input events from my mouse. I can interpret the event and post a QMouseEvent to my QDeclarativeView::viewport() widget. Those events make it to my QML. It would be awesome if I could set a keyboard modifier on my QMouseEvent so the QML could distinguish the mouse event from a touch event.

    In my Mouse Reader:
    QMouseEvent m(QEvent::MouseMove, QCursor::pos(), view->mapToGlobal(QCursor::pos()),
    Qt::NoButton, Qt::NoButton, Qt::MetaModifier);

    // Why does Qt discard this event?
    QApplication::sendEvent(view->viewport(), &m);

    In my QML:
    MouseArea {
    anchors.fill: parent
    hoverEnabled: true

        onPositionChanged: {
            if(mouse.modifiers & Qt.MetaModifier) {
                // mouse.modifiers is NEVER set
                console.log("++ Mouse Move!" + " " + mouse.button + " " + mouse.modifiers + " " + mouse.buttons)
            } else {
                console.log("++ Touch Move! " + " " + mouse.button + " " + mouse.modifiers + " " + mouse.buttons)


    But I never get Mouse Moves, only Touch Moves. Qt seems to discard a QMouseEvent(QEvent::MouseMove) with the keyboard modifier.

    This scheme works for my mouse press/release. I can distinguish those using mouse.modifiers.

    Does anyone know why Qt discards MouseMove's when the keyboard modifier is set?


Log in to reply