MousePressEvent QWidget is called even though QTabletEvent has been accepted

In a Qwidget class object with an implemented tabletEvent (QTabletEvent *) and mousePressEvent (QMouseEvent *), mousePressEvent is called every time a tabletEvent is called with type TabletEvent :: TabletPress. According to Qt documentation , this shouldn't be:

The QWidget :: tabletEvent () event handler receives the TabletPress, TabletRelease, and TabletMove events. Qt will dispatch the tablet event first and then if it is not received by any widgets it will dispatch the mouse event .

mainwindow.cpp

#include "mainwindow.h"
#include "tabletwidget.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    TabletWidget* tw = new TabletWidget(this);
    setCentralWidget(tw);
}

      

tabletwidget.h

#ifndef TABLETWIDGET_H
#define TABLETWIDGET_H

#include <QWidget>

class TabletWidget : public QWidget
{
    Q_OBJECT
public:
    explicit TabletWidget(QWidget *parent = 0);

protected:
    void tabletEvent(QTabletEvent *event) Q_DECL_OVERRIDE;
    void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;

signals:

public slots:
};

#endif // TABLETWIDGET_H

      

tabletwidget.cpp

#include "tabletwidget.h"
#include <QDebug>
#include <QTabletEvent>

TabletWidget::TabletWidget(QWidget *parent) : QWidget(parent)
{

}

void TabletWidget::tabletEvent(QTabletEvent *event)
{
    event->accept();
    qDebug() << "tabletEvent: " << event->type();
}

void TabletWidget::mousePressEvent(QMouseEvent *event)
{
    qDebug() << "mousePressEvent";
}

      

The output generated if I use the pen tip or press any Wacom Intuos CTH-680S-DEIT button:

tabletEvent:  92
mousePressEvent
tabletEvent:  87
tabletEvent:  87
tabletEvent:  87
tabletEvent:  87
tabletEvent:  93

      

So, callEvent is called first, and although I accept the event, mousePressEvent is still called. Each subsequent tabletEvent is of type QTabletEvent :: TabletMove, and the last is of type QTabletEvent :: TabletRelease. From the Qt documentation :

QEvent::TabletMove 87
QEvent::TabletPress 92
QEvent::TabletRelease 93

      

I tested this on Mac OS 10.10.3 and Windows 7 with the same result. Is this a bug or am I doing it wrong?

This has been tested on Qt 5.4.2.

+3


source to share


1 answer


Indeed, according to the Qt documentation, Qt should not send mouse events when the tablet is in use. But it seems to be all the same (I'm using version 5.5).

One way to get around this is to override the method event()

QApplication

- that's where TabletEnterProximity

and TabletLeaveProximity

; these functions are not dispatched to QWidget

event()

.

So, whenever the application catches events TabletEnterProximity

or TabletLeaveProximity

, you can send a signal to yours TabletWidget

to change the private bool variable _deviceActive

. Then inside TabletWidget

you add a check for each MousePressEvent

(s MouseReleaseEvent

) to see if the value is _deviceActive

true or not; and only implement the event if the flag is false.

To illustrate, the inherited TabletApplication

would look like this:

class TabletApplication : public QApplication {
    Q_OBJECT
public:
    TabletApplication(int& argv, char** argc): QApplication(argv,argc){}
    bool event(QEvent* event){
        if (event->type() == QEvent::TabletEnterProximity || event->type() == QEvent::TabletLeaveProximity) {
            bool active = event->type() == QEvent::TabletEnterProximity? 1:0;
            emit sendTabletDevice(active);
            return true; 
        }
        return QApplication::event(event);
}
signals:
    void sendTabletActive(bool active);
};

      

And additional parts inside tabletwidget.h

:



class TabletWidget : public QWidget {
// ...
public slots:
     void setTabletDeviceActive(bool active){
          _deviceActive = active;
     }
// ...
private:
     bool _deviceActive;
};

      

Then you check inside mouse events if the device is active:

void TabletWidget::mousePressEvent(QMouseEvent *event)
{
     if (!_deviceActive) 
         qDebug() << "mousePressEvent";
}

      

Of course, don't forget to connect the appropriate signal to the slot. Hope it helps.

Ref: TabletApplication from Qt Tablet Example

+3


source







All Articles